Professional Documents
Culture Documents
Vrije Universiteit
4 TCP Sockets
Spring 2008
http://www.cs.vu.nl/~gpierre/courses/np/ 5 I/O Multiplexing
6 Server Structures
Network Programming 2. Programming with sockets 1 / 54 Network Programming 2. Programming with sockets 2 / 54
6 Server Structures
Network Programming 2. Programming with sockets 3 / 54 Network Programming 2. Programming with sockets A Very Brief Introduction to Computer Networks 4 / 54
Introduction Two Different Kinds of Networks
Circuit switching
IOne electrical circuit is assigned to each communication
There is one way computers can communicate together IExample: the phone network
⊕ Guaranteed constant quality of service
I By sending network messages to each other
Waste of resources (periods of silence), fault tolerance
I All other kinds of communications are built from messages
There is one way programs can send/receive network messages Packet switching
I Messages are split into packets, which are transmitted independently
I Through sockets
F Packets can take different routes
I All other communication paradigms are built from sockets F Network infrastructures are shared among users
I Example: the Internet, most computer networks
Network Programming 2. Programming with sockets A Very Brief Introduction to Computer Networks 5 / 54 Network Programming 2. Programming with sockets A Very Brief Introduction to Computer Networks 6 / 54
Network Programming 2. Programming with sockets A Very Brief Introduction to Computer Networks 7 / 54 Network Programming 2. Programming with sockets A Very Brief Introduction to Computer Networks 8 / 54
UDP: User Datagram Protocol TCP: Transmission Control Protocol
UDP is very similar to IP: TCP establishes connections between pairs of machines
I Send/receive packets
I To communicate with a remote host, we must first connect to it.
I No guarantee TCP provides the illusion of a reliable data flow to the users
In UDP, “packets” are called “datagrams”
I Flows are split into packets, but the users doesn’t see it
Each datagrams is sent to an IP address and a port number TCP guarantees that the data sent will not be lost, unordered,
corrupted, etc.
I Example: IP=130.37.193.13 port=1234
I The sender gives numbers to packets so that the receiver can reorder
Ports allow to distinguish between several simultaneous programs on them.
the same machine I The receiver acknowledges received packets so that the sender can
I Program 1 uses port 1234 retransmit lost packets.
I Program 2 uses port 1235
Communication is bi-directional
I When a datagram is received, the operating system known which
program the datagram should be delivered to.
I The same connection can be used to send data in the two directions
I E.g., a request and its response
Network Programming 2. Programming with sockets A Very Brief Introduction to Computer Networks 9 / 54 Network Programming 2. Programming with sockets A Very Brief Introduction to Computer Networks 10 / 54
5 I/O Multiplexing
Transform packets into
electrical signals
Make sure signals Ethernet Ethernet Ethernet
do not interfere with
6 Server Structures
each other
Network Programming 2. Programming with sockets A Very Brief Introduction to Computer Networks 11 / 54 Network Programming 2. Programming with sockets IP Addressing and Name Resolution 12 / 54
Big-endian, Little-endian and Network Ordering IP Address Conversion Functions
Network Programming 2. Programming with sockets IP Addressing and Name Resolution 13 / 54 Network Programming 2. Programming with sockets IP Addressing and Name Resolution 14 / 54
sockaddr in: the Unix Network Address Structure [1/2] sockaddr in: the Unix Network Address Structure [2/2]
Network Programming 2. Programming with sockets IP Addressing and Name Resolution 15 / 54 Network Programming 2. Programming with sockets IP Addressing and Name Resolution 16 / 54
Domain Names Converting Domain Names into IP Addresses
Network Programming 2. Programming with sockets IP Addressing and Name Resolution 17 / 54 Network Programming 2. Programming with sockets IP Addressing and Name Resolution 18 / 54
resolv = gethostbyname(name);
if (resolv==NULL) { 3 UDP Sockets
printf("Address not found for %s\n",name);
return -1;
} 4 TCP Sockets
else {
addr = (struct in_addr*) resolv->h_addr_list[0];
printf("The IP address of %s is %s\n",name,inet_ntoa(*addr)); 5 I/O Multiplexing
return 0;
}
} 6 Server Structures
Network Programming 2. Programming with sockets IP Addressing and Name Resolution 19 / 54 Network Programming 2. Programming with sockets UDP Sockets 20 / 54
UDP Socket Functions socket(): create a socket
close() close()
Network Programming 2. Programming with sockets UDP Sockets 21 / 54 Network Programming 2. Programming with sockets UDP Sockets 22 / 54
bind() is used to specify the address of the socket: IP address and To create a UDP socket on port 1234:
port number.
I If you do not specify them, the system gives them a value int fd, err;
F IP address: the IP address of the running host (this is usually correct) struct sockaddr_in addr;
F Port number: any number (this can be correct or not depending on the
use of the socket) fd = socket(AF_INET,SOCK_DGRAM,0);
if (fd<0) { ... }
I Question: when do you need to specify a port number, and when can
you omit it? addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
#include <sys/types.h> addr.sin_addr.s_addr = htonl(INADDR_ANY);
#include <sys/socket.h>
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen); err = bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
if (err<0) { ... }
Network Programming 2. Programming with sockets UDP Sockets 23 / 54 Network Programming 2. Programming with sockets UDP Sockets 24 / 54
sendto(): Sending a UDP Datagram sendto() example
Network Programming 2. Programming with sockets UDP Sockets 25 / 54 Network Programming 2. Programming with sockets UDP Sockets 26 / 54
Network Programming 2. Programming with sockets UDP Sockets 27 / 54 Network Programming 2. Programming with sockets UDP Sockets 28 / 54
close(): Closing a Socket Table of Contents
Sockets must be closed when they are no longer used 2 IP Addressing and Name Resolution
#include <unistd.h>
int close(int fd); 3 UDP Sockets
6 Server Structures
Network Programming 2. Programming with sockets UDP Sockets 29 / 54 Network Programming 2. Programming with sockets TCP Sockets 30 / 54
Disconnection
close() read()
close()
Network Programming 2. Programming with sockets TCP Sockets 31 / 54 Network Programming 2. Programming with sockets TCP Sockets 32 / 54
listen(): Setting a Socket to Server Mode [1/2] listen(): Setting a Socket to Server Mode [2/2]
Network Programming 2. Programming with sockets TCP Sockets 33 / 54 Network Programming 2. Programming with sockets TCP Sockets 34 / 54
Network Programming 2. Programming with sockets TCP Sockets 35 / 54 Network Programming 2. Programming with sockets TCP Sockets 36 / 54
connect(): Connecting a TCP Socket write(): Sending Data to a Socket
write works the same for sending data to a TCP socket or writing to
Clients initiate connections to servers thanks to connect: a file
#include <sys/types.h> #include <unistd.h>
#include <sys/socket.h> ssize_t write(int fd, const void *buf, size_t count);
int connect(int fd, const struct sockaddr *serv_addr,
socklen_t addrlen);
Ifd: socket descriptor
Ibuf: buffer to be sent
I fd: the socket descriptor I count: size of the buffer
I serv addr: a pointer to a struct sockaddr in containing the + Return value: the number of bytes sent, or -1 in case of an error
address where to connect to
F Obviously, you must specify the destination IP address and port number Attention: when writing to a socket, write may send less bytes than
I addrlen: the size of a struct sockaddr in
requested
I Due to limits in internal kernel buffer space
+ Return value: 0 in case of success, or -1 in case of an error
⇒ Always check the return value of write, and resend the
Question: can you guess conditions which create an error? non-transmitted data
F And/or use the writen function from the Stevens book (page 78)
Network Programming 2. Programming with sockets TCP Sockets 37 / 54 Network Programming 2. Programming with sockets TCP Sockets 38 / 54
read() blocks the program until receiving data from the socket
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
To stop sending data to a socket, use close():
Ifd: socket descriptor #include <unistd.h>
I buf: buffer where to write the read data int close(int fd);
I count: size of buf
+ Return value: the number of bytes read, or -1 in case of an error This sends an end-of-file message to the other party
I When receiving an EOF, read returns 0 byte
Attention: when reading from a socket, read() may read less bytes I Subsequent reads and writes will return errors
than requested
I It delivers the data that have been received
I This does not mean that the stream of data is finished, there may be
more to come
I The end-of-file is notified to the reader by read()ing 0 byte
Network Programming 2. Programming with sockets TCP Sockets 39 / 54 Network Programming 2. Programming with sockets TCP Sockets 40 / 54
shutdown(): Asymmetric Disconnection Table of Contents
Sometimes you may want to tell the other party that you are finished,
but let it finish before closing connection 1 A Very Brief Introduction to Computer Networks
#include <sys/socket.h>
int shutdown(int fd, int how); 2 IP Addressing and Name Resolution
I how: SHUT WR for stopping writing, SHUT RD for stopping reading
I When one party has closed the connection, the other can still write
3 UDP Sockets
data (and then close the connection as well)
4 TCP Sockets
To initiate a disconnection To receive a disconnection
shutdown(fd,SHUT WR) read() receives an EOF 5 I/O Multiplexing
Keep on reading the last data Keep on writing the last data
6 Server Structures
Until receiving an EOF Then close() the socket
close() the socket
Network Programming 2. Programming with sockets TCP Sockets 41 / 54 Network Programming 2. Programming with sockets I/O Multiplexing 42 / 54
How can a program handle multiple file descriptors simultaneously? select() monitors several file descriptors simultaneously
I accept() and read() block programs until something is received
#include <sys/select.h>
I How can you wait for connections/data from multiple sockets? int select(int n, fd_set *readfds, fd_set *writefds,
Several methods: fd_set *exceptfds, struct timeval *timeout);
I Use multiple processes
F Resource consuming, hard to program In: the highest-numbered file descriptor, plus 1
I Use non-blocking I/O Ireadfs: a list of file descriptors to monitor for reading
I writefs: a list of file descriptors to monitor for writing
F It works for read() but not for accept()
I exceptfs: a list of file descriptors to monitor for exceptions
I select() monitors multiple file descriptors
I timeout: a duration after which select() returns anyway. Set it to 0
F It blocks the program until one of them is ready for reading or writing
for no timeout.
I poll() is similar to select() + Return value: the number of (i.e., how many) descriptors ready for
F With additional information about streams I/O, or 0 in case of timeout, or -1 in case of an error
F We won’t discuss it here
Network Programming 2. Programming with sockets I/O Multiplexing 43 / 54 Network Programming 2. Programming with sockets I/O Multiplexing 44 / 54
select(): I/O Multiplexing [2/3] select(): I/O Multiplexing [3/3]
Network Programming 2. Programming with sockets I/O Multiplexing 45 / 54 Network Programming 2. Programming with sockets I/O Multiplexing 46 / 54
1 A Very Brief Introduction to Computer Networks Often, a server accepts connections to one (TCP) socket
But it wants to process several requests simultaneously
2 IP Addressing and Name Resolution I Better use of the server’s resources
I Incoming requests can start being processed immediately after reception
3 UDP Sockets Depending on its nature, a server can receive between 0 and dozens
of thousands of requests per second
4 TCP Sockets Several server structures can be used:
I Iterative (i.e., not concurrent)
I One child per client
5 I/O Multiplexing I Prefork
I Select loop
6 Server Structures I Many other variants. . .
Network Programming 2. Programming with sockets Server Structures 47 / 54 Network Programming 2. Programming with sockets Server Structures 48 / 54
Iterative Servers One Child Per Client [1/2]
An iterative server treats one request after the other A new process is created to handle each connection
int fd, newfd; void sig_chld(int) {
while (1) { while (waitpid(0,NULL,WNOHANG)>0) {}
newfd = accept(fd, ...); signal(SIGCHLD,sig_chld);
treat_request(newfd); }
close(newfd);
} int main() {
int fd, newfd, pid;
I Simple signal(SIGCHLD,sig_chld);
I Potentially low resource utilization while (1) {
F If treat request() does not utilize all the CPU, resources are wasted newfd = accept(fd, ...);
if (newfd<0) continue;
I Potentially long waiting queue of incoming connections waiting to be pid = fork();
accept()ed if (pid==0) { treat_request(newfd); exit(0); }
F Increased request treatment latency else { close(newfd); }
F If the queue increases, the server may start rejecting incoming }
}
connections
Network Programming 2. Programming with sockets Server Structures 49 / 54 Network Programming 2. Programming with sockets Server Structures 50 / 54
Network Programming 2. Programming with sockets Server Structures 51 / 54 Network Programming 2. Programming with sockets Server Structures 52 / 54
Preforked [2/2] Select Loop
Highly loaded servers are often structured as preforked servers A single process manages multiple connections simultaneously thanks
IConcurrent request treatment to select()
I No waisted time to fork() each time a connection is received I This is quite difficult to do correctly
I You can limit the number of concurrent requests I You must split request treatment into a set of non-blocking stages
+ For example, the Apache Web server is structured that way I You must maintain a list of data structure containing the current state
There are variants to this model of each concurrent request
F Which stage it is in
I With a thread pool instead of a process pool F All internal data it needs
Not all systems allow multiple processes to accept() the same
socket simultaneously Implementing a select loop server is left to the students to exercise
I You must synchronize accesses to accept() :-)
+ lock mutex(); accept(); unlock mutex(); For example, the Squid Web cache is implemented as a select loop
Network Programming 2. Programming with sockets Server Structures 53 / 54 Network Programming 2. Programming with sockets Server Structures 54 / 54