Lecture 23 ● ● ● ● ● Log into Linux Copy directory /home/hwang/cs375/lecture23 In-class exercise to submit today Reminder: Project 7 due on Thursday Questions? Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 1 Outline ● TCP and UDP ports ● Sockets ● Client sockets ● Server sockets Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 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 Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 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.) Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 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. This data is stored in the /etc/services file. The program getsrv.cpp illustrates the use of these routines. Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 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 class. Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 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. Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 7 Sockets ● There is a useful analogy for establishing a TCP connection and using a telephone: ● ● CLIENT ● socket( ) ● connect( ) SERVER ● socket( ) ● bind( ) ● listen( ) ● accept( ) Tuesday, November 17 Purchase a telephone. Dial the server. Purchase a telephone. Publish your number. Turn on the ringer. Answer the phone. CS 375 UNIX System Programming - Lecture 23 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”.) Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 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. Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 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)); Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 11 Client Sockets ● ● The address parameter is a pointer to an address structure. As noted last time, different protocols have different structs cast to/from struct sockaddr. The struct for a local (UNIX) socket is struct sockaddr_un { sa_family_t sun_family; // always AF_UNIX char sun_path[UNIX_PATH_MAX]; // pathname }; Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 12 Client Sockets ● 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(clifd, address, len); Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 13 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.) Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 14 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); Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 15 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. Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 16 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. Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 17 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. Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 18 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. When you are finished, submit a tarfile or zipfile containing the two source files and the Makefile under entry 13-IN4. Tuesday, November 17 CS 375 UNIX System Programming - Lecture 23 19