170 likes | 337 Views
Socket Programming. Lab 1. Berkeley Unix IPC. socket --an interprocess communication endpoint supporting operations similar to those supported by files opened with the open system call. Creating a socket. int s = socket (domain, type, protocol);
E N D
Socket Programming Lab 1 CS 332 - Computer Networks
Berkeley Unix IPC socket--an interprocess communication endpoint supporting operations similar to those supported by files opened with the open system call. CS 332 - Computer Networks
Creating a socket int s = socket (domain, type, protocol); • domain - the “communication domain” to be used by the socket. One of: • AF_INET—The Internet Protocol v4. • AF_INET6 – The Internet Protocol v6. • type -what kind of service the process wants on the socket. Choices are: • SOCK_STREAM--two way byte stream • SOCK_DGRAM--communication consists of individual messages. • protocol --what protocol is to be used for communication over the socket. 0 asks for the default protocol for this protocol family for the requested socket type. CS 332 - Computer Networks
Specifying Addresses • General socket addresses: struct sockaddr { unsigned short sa_family; char sa_data[14]; }; • sa_family is the address family, such as AF_INET, for the Internet address family • sa_data is 14 bytes of family specific address data. CS 332 - Computer Networks
Specifying addresses • There is a special address struct for internet addresses: struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; CS 332 - Computer Networks
Specifying addresses • Note: same size as sockaddr • sin_family – the address family to use, always AF_INET for us • sin_port – the port number for this address • sin_addr – the 32 bit Internet address CS 332 - Computer Networks
Connecting Sockets • In order for some other process to connect to us, it needs to know what machine we are on and also how to specify the process that the socket it is trying to connect to. • We must request a “network visible process address" for our socket. • This address is called a port number, and we get it by doing a bind operation. • Port numbers < 1024 are reserved for system use. • Necessary only for processes acting as a server. CS 332 - Computer Networks
Connecting Sockets status = bind (sock, name, namelen); • sock - a socket descriptor returned by a previous call to the socket system call. • name - a structure describing the port we want to bind to. bind() expects a structure of type sockaddr, but for the AF_INET domain, we use a sockaddr_in structure, as defined in /usr/include/netinet/in.h • namelen -- size of the structure passed in name. CS 332 - Computer Networks
Waiting for connections • Once a socket is created and bound to a port, a server will typically wait for other processes to connect to it using the listen() system call. listen(sock, n); • This notifies the system that we are willing to accept up to n connection requests on sock. If multiple requests are made before we handle them, they are queued. • Listen does not block; it merely notifies the OS that we are ready to begin accepting connection requests. CS 332 - Computer Networks
Accepting connections • In order to wait for connections, we use the accept() system call. new_sock = accept (sock, addr, addrlen); • sock--the socket that we did the listen call on • addr– pointer to information about the connection that is created. Can be NULL if you aren’t interested. • addrlen—pointer to length in bytes of the structure passed in addr. Contains actual len. of struct containing client address info returned in addr. • new_sock--socket created by the accept. The original socket is still available for new connect attempts. CS 332 - Computer Networks
Client connection requests • There is a bit less work involved on the client side. socket() is used to create a socket, then connect() is used to connect to an existing socket at the other end: status = connect (client_sock, name, namelen); • client_sock--a socket created by a call to socket(). • name--structure describing the host and port to connect to. Of type sockaddr_in. The in_addr field can be filled in by using gethostbyname(). • namelen--the length of the structure passed to name. CS 332 - Computer Networks
Host address information host_info = gethostbyname(hostname); • hostname--a character string containing the name of the host to connect to. • host_info--a structure of type hostent (defined in /usr/include/netdb.h). The h_addr field can by copied to the sin_addr field of the sockaddr_in structure passed to connect(). CS 332 - Computer Networks
Sending/Receiving with sockets bytesSent = send( sock, buf, size, flags ); • sock--socket created by an accept or socket/connect. • buf--buffer to hold the incoming bytes. type void * (generic pointer) • size--maximum number of bytes in buf. • flags – can be used to change default behavior of send. 0 says use defaults • bytesSent – number of bytes actually sent. CS 332 - Computer Networks
Sending/Receiving with sockets nbytes = recv (sock, buf, size, flags); • sock--socket created by an accept() or socket()/connect() sequence. • buf--buffer for received bytes. type void * (generic pointer) • size—max number of bytes to put in buf. • flags – can be used to change default behavior of send. 0 says use defaults • nbytes--number of bytes actually received. CS 332 - Computer Networks
Sending/Receiving with sockets • Sockets created with type SOCK_STREAM behave just like files opened with the open() system call. The read() system call can be used to receive bytes, the write() system call can be used to send bytes. nbytes = read(new_sock, buf, size); • new_sock--socket created by an accept or socket/connect. • buf--buffer to hold the incoming bytes. • size--maximum number of bytes to put into buf. • nbytes--number of bytes actually read. CS 332 - Computer Networks
Sending/Receiving with sockets nbytes = write (new_sock, buf, size); • new_sock--socket created by an accept() or socket()/connect() sequence. • buf--buffer containing string to write. • size--number of bytes contained in buf. • nbytes--number of bytes actually written. CS 332 - Computer Networks