Lecture 22 Log into Linux. Copy directory /home/hwang/cs375/lecture22 Reminder: Project 6 due on Tuesday Questions? Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 1 Outline TCP and UDP ports Sockets Client sockets Server sockets Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 2 Client/Server Architecture Most network applications are divided into client and server parts. A particular client usually communicates with a single server. While a server communicates with several clients. client client server client Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 3 TCP and UDP Ports Multiple processes can use TCP or UDP simultaneously. 16-bit integer port numbers are used to distinguish processes. Servers listen for connections on well-known ports (e.g., ftp - port 21, ssh - port 22, telnet port 23, http - port 80). A list can be found in the /etc/services file. (Ports 0-1023 are reserved ports and can only be allocated by processes with superuser privileges.) Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 4 TCP and UDP Ports Clients use ephemeral or short-lived ports. These port numbers are automatically assigned by TCP or UDP to the client. The routines getservbyname( ) and getservbyport( ) can be used to translate between port names and numbers. The program getsrv.cpp illustrates the use of these two routines. Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 5 Sockets A socket provides yet another mechanism for IPC . Although we have been focusing on TCP/IP networks, a socket can be used for IPC across other networks (e.g., IPX, AppleTalk) and even for local communication. The different transport mechanisms are known as domains. We will concentrate on local (or UNIX) domain communication via sockets today and return to network sockets next time. Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 6 Sockets The client uses the socket( ) and connect( ) routines for establishing connections. The server uses the socket( ), bind( ) and listen( ) routines to prepare for an incoming connection. The accept( ) routine is then used to wait for client connections. Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 7 Sockets There is a useful analogy for establishing a TCP connection and using a telephone: CLIENT socket( ) connect( ) Purchase a telephone. Dial the server. socket( ) bind( ) listen( ) accept( ) Purchase a telephone. Publish your number. Turn on the ringer. Answer the phone. SERVER Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 8 Client Sockets To create a socket use the socket( ) routine: clifd = socket(domain, type, protocol); A file descriptor is returned. The domain parameter selects the protocol. It is PF_UNIX for a local (filesystem) socket. (See “man 7 unix”.) Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 9 Client Sockets The type is either SOCK_STREAM for a stream-oriented (TCP) socket or SOCK_DGRAM for a datagram-oriented (UDP) socket that preserves message boundaries. (Linux also supports SOCK_SEQPACKET for a connection-oriented socket that preserves message boundaries.) We will only discuss SOCK_STREAM types today. We can use the standard read( ) and write( ) routines for socket I/O. Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 10 Client Sockets The protocol parameter is used to select a subprotocol within the protocol family and is 0 for PF_UNIX. A client uses the connect( ) routine to establish a connection to a listening server. res = connect(clifd, addr, sizeof(addr)); Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 11 Client Sockets The address parameter is a pointer to a server address structure. Different protocols have different structs cast to struct sockaddr. Here is example code for setting up an address for a local socket: struct sockaddr_un unaddr; struct sockaddr *address = (struct sockaddr *)(&unaddr); unaddr.sun_family = AF_UNIX; strcpy(unaddr.sun_path, "/tmp/tmpsck"); int len = sizeof(struct sockaddr_un); res = connect(fd, address, len); Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 12 Server Sockets The server will call socket( ), bind( ), listen( ), and accept( ). First call socket( ) to create the socket: srvfd = socket(domain, type, protocol); The domain, type and protocol parameters are the same as for the client (PF_UNIX, SOCK_STREAM, and 0). (Note: AF_UNIX and PF_UNIX are the same constant. Originally there were to be constants for address families and protocol families.) Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 13 Server Sockets bind( ) is used to assign the local address to the socket: struct sockaddr_un unaddr; struct sockaddr *address = (struct sockaddr *)(&unaddr); unaddr.sun_family = AF_UNIX; strcpy(unaddr.sun_path, "/tmp/tmpsck"); int len = sizeof(struct sockaddr_un); bind(srvfd, address, len); Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 14 Server Sockets The listen( ) routine sets up a queue for client connections: res = listen(srvfd, qlength); A queue length of 5 is typical. Sockets are designed for client/server communication. There may be several clients trying to connect to a single server. They are held in the queue until accepted by the server. Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 15 Server Sockets Finally, the server calls the accept( ) routine to wait for a client connection: newfd = accept(sockfd, ptr_addr, &len); accept( ) blocks until a connection is made. A new socket file descriptor is returned. The server uses this file descriptor to communicate with the client. Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 16 Socket Routines The socket file descriptors are bidirectional. The standard read( ) and write( ) routines can be used for I/O via the socket. Linux also supports UNIX domain anonymous sockets that do not need to be associated with a filename. See the unix(7) man page. Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 17 In-class Exercise The files local_client.cpp and local_server.cpp contain client and server programs, respectively. A makefile is provided. Complete the server and client so that they can communicate with each other. Note that after the server is started, a socket "file" may be seen in the current directory. Thursday, April 3 CS 375 UNIX System Programming - Lecture 22 18