Download - Sockets - Bard College at Simon's Rock
![Page 1: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/1.jpg)
SocketsLecture 08.01
https://src-code.simons-rock.edu/git/mbarsky/socket_demo.git
![Page 2: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/2.jpg)
Inter-process communication
• Wait for exit status (report when done)• Only short integer status
• Pipe (always open for communication)• Only between related processes
• Signals (send when you want, handle or ignore)• Just a poke
![Page 3: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/3.jpg)
Inter-process communication
• Wait for exit status (report when done)• Pipe (always open for communication)• Signals (send when you want, handle or ignore)• Sockets (open connection with the world)
![Page 4: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/4.jpg)
Sockets
• We want two unrelated processes to talk with each other:
• Created by different shells
• Created by different users
• Running on different machines
• Sockets are communication points on the same or different computers to exchange data
• Sockets are supported by Unix, Windows, Mac, and many other operating systems
• Now they are also supported by all modern browsers
![Page 5: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/5.jpg)
Sockets use file descriptors to talk
• Every I/O action is done by writing or reading to/from a stream using file descriptor
• To a programmer, a socket looks and behaves much like a low-level file descriptor: has read(), write(), close()
• Sockets are full-duplex (2 way) – as if opening a stream for both reading and writing
• The only difference – how we set up the socket
![Page 6: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/6.jpg)
If 2 processes are unrelated – we need a protocol for communication
• What to say
• In what context
• When
![Page 7: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/7.jpg)
Communication protocols
• TCP protocol – how to transfer and receive byte streams
• IP protocol – how to locate and connect to a machine on the internet
• HTTP protocol establishes rules of communication between browser and web server
• Application-level protocols: FTP, SMTP, and POP3
![Page 8: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/8.jpg)
Socket protocols
• The two most common socket protocols:
• TCP (Transmission Control Protocol)
• UDP (User Datagram Protocol)
![Page 9: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/9.jpg)
Stream sockets (TCP)
• Message delivery is guaranteed. If delivery is impossible, the sender receives an error indicator
• If you send three items "A, B, C", they will arrive in the same order − "A, B, C"
• Data records do not have any boundaries
![Page 10: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/10.jpg)
Datagram sockets (UDP)
• Order is not guaranteed
• Connectionless: you don't need to have an open connection as in Stream Sockets − you build a packet with the destination information and send it out
• Delivery is not guaranteed
![Page 11: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/11.jpg)
Server
• A server is a program that performs some functions on request from a client
• Server serves as a major switch in the phone company
• It is responsible for taking incoming calls from clients and then creating personal connection between a pair of file descriptors: one in the client and one in the server process
![Page 12: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/12.jpg)
0
1
2
3
4
How does it work
Server
Entry pointClient 1
0
1
2
Contacts server process, asks for connection
![Page 13: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/13.jpg)
0
1
2
3
4 Socket end for C1
How does it work
Server
Entry point
Client 1
0
1
2
3 Socket end
Connection established, can talk now
![Page 14: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/14.jpg)
0
1
2
3
4 Socket end for C1
How does it work
Server
Entry point
Client 1
0
1
2
3 Socket end
Client 2
0
1
2
![Page 15: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/15.jpg)
0
1
2
3
4 Socket end for C1
5 Socket end for C2
How does it work
Server
Entry point
Client 1
0
1
2
3 Socket endClient 2
0
1
2
3 Socket end
Connection established, can talk now
![Page 16: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/16.jpg)
0
1
2
3
4 Socket end for C1
5 Socket end for C2
How does it work
Server
Entry point
Client 1
0
1
2
3 Socket end
Client 2
0
1
2
3 Socket end
Client 3
0
1
2
ETC.
![Page 17: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/17.jpg)
Unix domain socket server
code in server.c
Data communications endpoints for exchanging data between processes executing on the same Unix host system
![Page 18: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/18.jpg)
Unix domain sockets
server process
Finds server through path in
file system: /tmp/something
# Data Stream
0 …
1 …
2 …
3 entry_fd
# Data Stream
0 …
1 …
2 …
client process
Full-duplex communication between two unrelated processes through Unix inode
![Page 19: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/19.jpg)
Unix domain sockets
server process
Our own buffer
# Data Stream
0 …
1 …
2 …
3 entry_fd
4 client1_fd
# Data Stream
0 …
1 …
2 …
3 my_socket_fd
client process
Full-duplex communication between two unrelated processes through Unix inode
![Page 20: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/20.jpg)
int serv_fd;
if ((serv_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror (“socket”);
exit (1);
}
Define socket (like installing a phone socket)
socket() call does not specify where data will be coming from, nor where it will be going to –it just creates the socket prototype of a certain type and assigns it to the file descriptor - serv_fd
Socket type –Unix socket
Protocol type –TCP
Protocol id –can be multiple protocols, but here only one
12 3
![Page 21: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/21.jpg)
Assign address to socket (like assigning phone number)• Socket descriptor prototype from the call to socket needs to
be assigned an address
• For Unix sockets this address is a file of a special type in a file system
• Different processes can access these “files” as file system inodes, so two processes can establish communication
![Page 22: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/22.jpg)
Define server socket address
struct sockaddr_un server_addr;
memset(&server_addr, '\0', sizeof (server_addr));
server_addr.sun_family = AF_UNIX;
strcpy(server_addr.sun_path, "/tmp/something");
unlink(server_addr.sun_path);
Declare variable of type sockaddr_un
Clear all bytes to zero
Setup address family
Set file name through which server can be contacted
Delete file with this name if already exists
![Page 23: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/23.jpg)
3 steps of socket server setup: BLA1. Bind
2. Listen
3. Accept
![Page 24: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/24.jpg)
1. Bind
• Bind socket to a connection resource - in this case the socket inode (a new kind of "special file") – to create an entry point
struct sockaddr_un serv_addr; //all set up – see above
if (bind(serv_fd, (struct sockaddr *)&serv_addr, sizeof (serv_addr))) {
perror("bind"); return(1);
}
fd returned from socket()
1
Address to bind to and its size
2
Returns zero on success
![Page 25: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/25.jpg)
Some sort of “polymorphism”
• Because bind is designed to work with all kinds of sockets, and the size of the addresses may be different, the second argument of bind() is of a general type struct sockaddr*
• We need to cast our Unix socket address of type sockaddr_un to this general type
• Third parameter tells how much space to consider for reading an actual address from a given memory location (different types of address structs have different length)
struct sockaddr_un serv_addr; //all set up above
bind(serv_fd, (struct sockaddr *)&serv_addr, sizeof (serv_addr)) ;
![Page 26: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/26.jpg)
2. Listen
• Listen — wait for incoming connections
• Also specifies the length of the queue for connections which have not yet been "accepted“
• It is not a limit on the number of people you can talk to - it's just how many can do a connect() before you accept() them
if (listen(serv_fd, 5)) {
perror("listen");
return(1);
}
Backlog for incoming connections
1fd returned from socket()
2
![Page 27: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/27.jpg)
3. Accept
• Accept processes client requests (usually in a loop)
• It returns a new socket file descriptor for talking to that particular client
struct sockaddr_un client _addr;
int len = sizeof (client_addr);
if ((client_fd = accept(serv_fd, (struct sockaddr *)&client_addr,&len)) < 0) {
perror("accept"); return(1);
}
1fd returned from socket()
2
Address of a client and the length of this address
![Page 28: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/28.jpg)
Client address is recorded into variable client_addr• When accept() returns, the client_addr variable will be filled with the
remote side's struct sockaddr_un, and len will be set to its length
• The new file descriptor client_fd is created, and is ready for sending and receiving data for this particular client
struct sockaddr_un client _addr;
int len = sizeof (client_addr);
if ((client_fd = accept(serv_fd, (struct sockaddr *)&client_addr, &len)) < 0) {
perror("accept");
return(1);
}
![Page 29: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/29.jpg)
Read data from a client: example
char buf[BUF_SIZE+1];
if ((len = read(client_fd, buf, BUF_SIZE))) < 0) {
perror("read");
return(1);
}
// The read is raw bytes. This turns it into a C string.
buf[BUF_SIZE] = '\0';
printf("The other side said: %s\n", buf);
![Page 30: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/30.jpg)
Write data to a client: example
//echo data back
if (write(client_fd, buf, strlen(buf)) != strlen(buf) ) {
perror("write");
return(1);
}
![Page 31: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/31.jpg)
Close
• Closing the client_fd makes the other side see that the connection is closed
close(client_fd);
• Unix domain socket binding is reclaimed upon process exit, but the inode is not. You have to explicitly unlink (delete) it
close(server_fd);
unlink("/tmp/something");
![Page 32: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/32.jpg)
• If you run the server, you can connect to it in a different terminal using a general client program netcat (nc):
nc –U /tmp/something
• You can see all open unix sockets using netstat:
netstat –lxp
• You can see that socket in Unix sockets is a ‘file’:
ls -l /tmp/something
![Page 33: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/33.jpg)
Unix domain socket clientcode in client.c
![Page 34: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/34.jpg)
Client program: socket setup
• Create a socket interface of type Unix domain socket:
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket");
return(1);
}
Client has only one file descriptor used to connect to a remote process and if successfully connected – this will be the fd of the communication
![Page 35: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/35.jpg)
Connect to known address
• The client does connect(), the server does accept()
• Fill-in fields of server address – to which to connect:
struct sockaddr_un serv_addr;
memset(&serv_addr, '\0', sizeof (serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy (serv_addr.sun_path, "/tmp/something");
if (connect(fd, (struct sockaddr *)&serv_addr, sizeof (serv_addr))) {
perror("connect");
return(1);
}
Descriptor created with socket()
![Page 36: Sockets - Bard College at Simon's Rock](https://reader031.vdocuments.mx/reader031/viewer/2022020620/61e39506ee084671eb197238/html5/thumbnails/36.jpg)
Now client can write and read
if ((len = write(fd, "Hello", 5)) != 5) {
perror("write");
return(1);
}
if ((len = read(fd, buf, MAX_LINE)) < 0) {
perror("write");
return(1);
}
buf[MAX_LINE] = '\0';