CIS 307: Unix IV: Sockets
Online references:
[Introduction], [Client-Server Architecture], [Summary on Socket Functions], [Socket Functions], [Examples]
A problem in communication is how to identify interlocutors. In the case of phones we have telephone numbers, for mail we have addresses. For communicating between sockets we [usually, since within a single computer we could use file names] identify an interlocutor with a pair: IP address and port. [In reality there is a third component, the protocol, but that will not be relevant to us.] This represents the address or name of the interlocutor. IP addresses (things like 155.247.207.190) are 32 bit unsigned integers (155, 247, 207, 190 are the bytes). IP addresses are more easily rememberer as host names (things like snowhite.cis.temple.edu). [You may find about IP to host conversions with the nslookup command and by looking in the /etc/hosts file.] Ports are 16 bit unsigned integers. (The first 1024 port numbers are reserved for things like http, 80. You can look in the files /etc/services and /etc/inetd.conf to see standard uses (ftp, telnet, finger, ..) of these ports. From 1024 to 5000 the ports are reserved for allocation by the kernel on demand. Over 5000 the ports can be chosen directly by the users. For instance I use 8080 for my httpd server. The port 0 is used as a wild card, to request the kernel to find a port for us, we do not care which. A special IP is used to refer to the local host, 127.0.0.1, the loopback localhost. Another IP address, 0.0.0.0, is called INADDR_ANY and is tied to all the IP addresses of this machine. Another special IP is used for broadcast to all hosts.
An address, host+port, can be used for multiplexing more than one communication channel. So one server can communicate simultaneously with more than one client. Each communication channel on the server will have its own socket bound to the same address.
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol)
domain is either AF_UNIX, AF_INET, or AF_OSI, or ..
AF_UNIX is the Unix domain, it is used for communication within a
single computer system.
AF_INET is for communication on the internet to IP addresses.
We will only use AF_INET.
type is either SOCK_STREAM (TCP, connection oriented, reliable),
or SOCK_DGRAM (UDP, datagram, unreliable), or SOCK_RAW (IP level).
It is the name of a file if the domain is AF_UNIX.
protocol specifies the protocol used. It is usually 0
to say we want to use the default protocol for the chosen
domain and type. We always use 0.
It returns, if successful, a socket descriptor which is an int.
It is -1 in case of failure.
Here is a typical call to socket:
if ((sd = socket(AF_INET, SOCK_DGRAM, 0) < 0) {
perror("socket"); exit(1);}
struct in_addr {
u_long s_addr;
};
struct sockaddr_in {
u_short sin_family; /*protocol identifier; usually AF_INET */
u_short sin_port; /*port number. 0 means let kernel choose */
struct in_addr sin_addr; /*the IP address. INADDR_ANY refers */
/*IP addresses of the current host.*/
char sin_zero[8];}; /*Unused, always zero */
In order to use struct sockaddr_in you need to include in your program
#include <netinet/in.h>
The following structure sockaddr is more generic than but compatible
with sockaddr_in (both 16 bytes starting with the same field).
In the Unix domain we have a different address, sockaddr_un, which is
also compatible with sockaddr. In order to use sockaddr_un you need to
include in your program
#include <sys/un.h>
struct sockaddr {
u_short sa_family;
char sa_dat[14];};
#include <sys/types.h>
#include <sys/socket.h>
int bind(int sd, struct sockaddr *addr, int addrlen)
sd: File descriptor of local socket, as created by the socket
function.
addr: Pointer to protocol address of this socket.
It usually is INADDR_ANY . The port is usually 0 to request
the kernel to provide a port.
addrlen: Length in bytes of addr.
It returns an integer, the return code (0=success, -1=failure)
Here is a typical call to bind:
struct sockaddr_in name;
.....
bzero((char *) &name, sizeof(name)); /*zeroes out sizeof(name) characters*/
name.sin_family = AF_INET; /*use internet domain*/
name.sin_port = htons(0); /*ask kernel to provide a port*/
name.sin_addr.s_addr = htonl(INADDR_ANY); /*use all IPs of host*/
if (bind(sd, (struct sockaddr *)&name, sizeof(name)) < 0) {
perror("bind"); exit(1);}
A call to bind is optional on the client side, required on the server side.
We need to understand the reasons for the calls to htons and htonl.
Numbers on different machines may be represented differently (big-endian
machines and little-endian machines). So we need to make sure that
the right representation is used on each machine. We use functions to convert
from host to network form before transmission (htons for short
integers, and htonl for long integers), and from network to host
form after reception (ntohs for short integers, and ntohl
for long integers).
The functions bzero zeroes out a buffer of specified length. It is one of a group of functions for dealing with arrays of bytes. bcopy copies a specified number of bytes from a source to a target buffer. bcmp compares a specified number of bytes of two byte buffers.
#include <sys/types.h>
#include <sys/socket.h>
int connect(int sd, struct sockaddr *addr, int addrlen)
sd file descriptor of local socket
addr pointer to protocol address of other socket
addrlen length in bytes of address
It returns an integer (0=success, -1=failure)
Here is a typical call to connect:
#define SERV_NAME ... /* say, "snowhite.cis.temple.edu */
#define SERV_PORT ... /* say, 8001 */
struct sockaddr_in servaddr;
struct hostent *hp; /* Here we store information about host*/
int sd; /* File descriptor for socket */
.......
/* initialize servaddr */
bzero((char *)&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
hp = gethostbyname(SERV_NAME);
if (hp == 0) {
fprintf(stderr, "failure to address of %s\n", SERV_NAME); exit(1);}
bcopy(hp->h_addr_list[0], (caddr_t)&servaddr.sin_addr, hp->h_length);
if (connect(sd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("connect"); exit(1);}
The function gethostbyname is described below.
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* null terminated list of aliases*/
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* null terminated list of addresses */
/* from name server */
#define h_addr h_addr_list[0] /*address,for backward compatibility*/};
In this structure h_addr_list[0] is the first IP address associated with
the host. In order to use this structure you must include in your program:
#include <netdb.h>
Other functions help us find out things about hosts, services, protocols, networks:
gethostbyaddr,
getprotobyname, getprotobynumber, getprotoent,
getservbyname, getservbyport, getservent,
getnetbyname, getnetbynumber, getnetent.
int listen(int fd, int qlen)
fd file descriptor of a socket that has already been bound
qlen specifies the maximum number of messages that
can wait to be processed by the server
It returns an integer (0=success, -1=failure)
Here is a typical call to listen:
if (listen(sd, 3) < 0) {
{perror("listen"); exit(1);}
#include <sys/types.h>
#include <sys/socket.h>
int accept(int fd, struct sockaddr *addressp, int *addrlen)
fd is an int, the file descriptor of the socket the
server was listening on
addressp points to an address. It will be filled with
address of the calling client
addrlen is an integer that will contain the actual length
of address of client
It returns an integer representing a new socket (-1 in case of failure).
It is the socket that the server will use from now on to communicate
with the client that requested connection.
Here is a typical call to accept:
struct sockaddr_in client_addr;
int ssd, csd, length;
...........
if ((cfd = accept(ssd, (struct sockaddr *)&client_addr, &length) < 0) {
perror("accept"); exit(1);}
/* here we give the new socket to a thread or a process that will */
/* handle communication with this client. */
#include <sys/types.h>
#include <sys/socket.h>
int sendto(int sd, char *buff, int len, int flags,
struct sockaddr *addressp, int addrlen)
sd, socket file descriptor
buff, address of buffer with the information to be sent
len, size of the message
flags, usually 0; could be used for priority messages, etc.
addressp, address of process we are sending message to
addrlen, length of message
It returns number of characters sent. It is -1 in case of failure.
#include <sys/types.h>
#include <sys/socket.h>
int recvfrom (int sd, char *buff, int len, int flags,
struct sockaddr *addressp, int *addrlen)
sd, socket file descriptor
buff, address of buffer where message will be stored
len, size of buffer
flags, usually 0; used for priority messages, peeking etc.
addressp, buffer that will receive address of process that
sent message
addrlen, contains size of addressp buffer; it will contain
the size of the address
It returns number of characters received. It is -1 in case of failure.
int shutdown(int sd, int action)
sd is a socket descriptor
action is (0 = close for reads) (1 = close for writes)
(2 = close for both reads and writes)
It returns an integer (0=success, -1=failure)
int getsockname(int sd, struct sockaddr *addrp, int *addrlen)
sd is the socket descriptor of a bound socket.
addrp points to a buffer. After the call it will have the
address associated to the socket.
addrlen gives the size of the buffer. After the call gives
size of address.
It returns an integer (0=success, -1=failure)
int getpeername(int sd, struct sockaddr *addrp, int *addrlen)
sd is the socket descriptor of a bound socket.
addrp points to a buffer. After the call it will have the
address associated to peer of socket.
addrlen gives the size of the buffer. After the call gives
size of address.
It returns an integer (0=success, -1=failure)
Here is an example of use of getpeername:
struct sockaddr_in name;
in namelen = sizeof(name);
.......
if (getpeername(sd, (struct sockaddr *)&name, &namelen) < 0) {
perror("getpeername"); exit(1);}
printf("Connection from %s\n", inet_ntoa(name.sin_addr));
We see here a new function inet_ntoa: Translate an internet
integer address into a dot.formatted character string.
It requires the include files:
#include <netinet/in.h>
#include <arpa/inet.h>
Example 2 (Datagram communication): a client and a server. In a loop, the client sends the current time to the server, waits for the reply, prints it out, and sleeps for a while. The server receives messages from clients and prints them out. It replies with its own current time. No provision is made to cope with the unreliability of the communication channel.
Example 3: (Datagram communication) a client and a server. The client is invoked with three parameters: the name of a user, of a host, and a port. It sends the user name to the server and prints out the response. The server when it receives a user name checks if the user is currently logged on the host. It replies with an appropriate response. No provision is made to cope with the unreliability of the communication channel.
Example 4: (Datagram communication): a client and a server. A client in a loop sends a message to the server and waits with timeout for reply. The server receives messages and gives them to threads to respond to.
Example 5: Threaded Server from the Threads Primer book.
ingargiola.cis.temple.edu