Sockets
description
Transcript of Sockets
![Page 1: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/1.jpg)
Sockets
Intro to Network Programming
![Page 2: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/2.jpg)
Before the internet... Early computers were entirely isolated
No Internet No network No model No external communications whatsoever
![Page 3: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/3.jpg)
Before the internet... However, early computers did have
many concepts we have already studied: File I/O Console I/O Shared memory Inner-process communication
Solution: extend existing concepts to allow for remote communication
![Page 4: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/4.jpg)
Before the internet... Solution: sockets
A socket is nothing more than an end-point of a bidirectional inner-process communication flow.
A socket, in a Linux operation system, is a file descriptor – the exact same thing you get form a call to open().
![Page 5: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/5.jpg)
Sockets Sockets are often described as a door,
gateway, or portal. The sender sends information through the
portal. The receiver receives the information.
To a programmer, the mechanism to get the information from the sender to receiver is abstracted away (TCP/IP, UDP/IP, etc).
![Page 6: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/6.jpg)
Sockets Potential Problem: Many
programs could possibly be communicating externally at once. Web browser loading a page E-mail client checking e-mail Streaming radio/music in the
background …and a download going on.
![Page 7: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/7.jpg)
Sockets Solution: UDP and TCP both provide
ports. A port is a 16-bit number to describe
which program on a computer the information is intended to be received by.
Example: By default, web servers always listen for
connections form clients on port 80. Written as: cs.illinois.edu:80
![Page 8: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/8.jpg)
SocketsBobAlice
client server
I am Bobport 80.
I am acceptingconnections.
Talk to Bobport 80.
I will speak toAlice port 87.
resulting TCP connection identified by(Alice:87, Bob:46)
![Page 9: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/9.jpg)
Programming with sockets… To create a socket in C, you make a
call to socket(). Definition: int socket(int domain, int type, int protocol);
Alternatively, accept() will return a socket file descriptor (we’ll see this later).
![Page 10: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/10.jpg)
Programming with sockets… Definition: int socket(int domain,
int type, int protocol);
domain: Specifies a communication domain (eg: IPv4 v. IPv6).
type: Specifies what mechanism of communication is used (eg: stream v. datagram).
protocol: Specifies what protocol should be used (eg: TCP v. UDP).
![Page 11: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/11.jpg)
Programming with sockets… If you wish to allow for others to
connect to your socket, you will need to bind() it to a port on the operating system. int bind(int socket, const struct
sockaddr *address, socklen_t address_len);
![Page 12: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/12.jpg)
Programming with sockets… Now that you’ve created your
socket and bind()’d it to a port, you can listen for incoming connections! int listen(int socket, int backlog);
![Page 13: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/13.jpg)
Programming with sockets… Now that you’ve created your socket
and bind()’d it to a port, you can listen for incoming connections! int listen(int socket, int backlog);
backlog: defines the maximum length for the queue of pending connections.
For most programs that will be constantly accept()’ing connections, a smallish value (5 or so) works great.
![Page 14: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/14.jpg)
Programming with sockets… When you begin to listen() on your
socket: …you’ve told the operating system to
allow remote systems to connect at the specified port using the specified protocol.
…you haven’t had a way to actually get a socket to communicate with that remote system.
![Page 15: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/15.jpg)
Programming with sockets… To receive a socket file descriptor
for an incoming connection, you accept() the incoming connection. int accept(int sockfd, struct sockaddr
*addr, socklen_t *addrlen);
![Page 16: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/16.jpg)
Programming with sockets… To receive a socket file descriptor
for an incoming connection, you accept() the incoming connection. int accept(int sockfd, struct sockaddr
*addr, socklen_t *addrlen);
addr: The address of the system connect()ing to your system is written to the struct you pass in.
![Page 17: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/17.jpg)
Programming with sockets… To receive a socket file descriptor for an
incoming connection, you accept() the incoming connection. int accept(int sockfd, struct sockaddr
*addr, socklen_t *addrlen);
Return value: A new socket file descriptor to communicate with the remote system. Your original socket is still listening and more connections can be accept()’d.
![Page 18: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/18.jpg)
Programming with sockets… If you’ve created a socket that you
want to connect() rather than listen(), you simply create the socket and connect(). int connect(int sockfd, const struct
sockaddr *serv_addr, socklen_t addrlen);
(If you’re connect()’ing, you won’t want to listen() or bind() on that socket.)
![Page 19: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/19.jpg)
A Simple Client-Server Program
bind()
listen()
accept()
read()
write()
close()
write()
read()
connect()
socket()
socket()
close()
Client
Server
![Page 20: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/20.jpg)
A Simple Client-Server Program In the diagram:
read() write() close()
…you’ll notice you’ve seen these calls before. These file I/O calls are socket I/O calls, too.
![Page 21: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/21.jpg)
Programming with sockets… There are a few calls that are socket
specific… ssize_t send(int socket, const void
*buffer, size_t length, int flags);
flags: Provides access to socket-specific parameters. A call to write() is equivalent to send() with no flags (eg: flags == 0).
![Page 22: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/22.jpg)
Programming with sockets… For completeness, there are a
whole set of send()/recv() calls: send(), sendto(), sendmsg() recv(), recvfrom(), recvmsg()
(In CS 241, we won’t need any of the flag parameters.)
![Page 23: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/23.jpg)
Server Initialization
OS
1. socket()
Web Server
2. bind(80)3. listen()
80
Listenqueue
![Page 24: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/24.jpg)
Connecting to the Server
OS
1. socket()
Web Server
2. bind(80)3. listen()
80
Listenqueue
Client
connect()
Requestfrom (IP, port)
![Page 25: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/25.jpg)
Busy Server Operation
OS
Web Server
80
Listenqueue
Client 1 Client 3Client 2
Client requests get queued-up in the listen queue First-come first-served
![Page 26: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/26.jpg)
The accept() Call
OS
Web Server
80
Listenqueue
Client 1 Client 3Client 2
Client requests get queued-up in the listen queue First-come first-served
accept()Connectedsocket
![Page 27: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/27.jpg)
A final note... You now know the calls to use
sockets, but you’ll find you need one more set of calls…
![Page 28: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/28.jpg)
A final note... Various different architectures store
16-bit and 32-bit numbers differently (big-endian v. little-endian).
When configuring port numbers and IP addresses, we need to talk in an architecture-independent way.
![Page 29: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/29.jpg)
A final note... In C, functions are provided to
convert between your local architecture and network architecture. htonl(): Converts a long from host to
network byte order. htons(): Converts a short from host to
network byte order. …and the inverse: ntohl() and ntohs().
![Page 30: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/30.jpg)
A final note... You’ll often find code:
ip.offset = ntohs(ip.offset);ip.length = ntohs(ip.length);ip.id = ntohs(ip.id);
![Page 31: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/31.jpg)
Speaking of code... We’ll take a look at actual C code
to run a client/server in section tomorrow. Tomorrow will be the very last section
of CS 241 (next Thursday is reading week).
![Page 32: Sockets](https://reader036.fdocuments.in/reader036/viewer/2022070405/56813d09550346895da6b5ed/html5/thumbnails/32.jpg)
CS 241 At this point, we’ve covered every topic
in CS 241! Congratulations!
Remember: Prof. Caccamo will be back on Monday HW #2: Due IN-CLASS @11:00am Monday
Solutions will be discussed during Monday’s lecture MP #7: Due on SVN on Wednesday Final Exam: Dec. 18th @8:00am