DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Developing applications over TCP/IP using Internet sockets

Elementary system calls

This section describes, in the context of the client-server model, the socket system calls needed to create and connect a pair of sockets and transmit data.

A socket created with the socket call is identified only by its address family. It has no name. Without a name, processes cannot reference it and no communication can take place. Communication processes are bound by an association. In the Internet domain, as mentioned earlier, an association consists of a local address and port number, and a foreign address and port number. The bind(SSC) system call allows a process to specify the local half of this association by binding an address to the local end of a socket.

The call takes the form:

bind(s, name, namelen);

Arguments to bind include the socket, a pointer to the address (name), and the length of the address (namelen). With this information, a process is able to rendezvous with an unrelated process. The operation is asymmetric, with one process taking the role of server and the other of client.

Server (Internet domain)

Server sockets listen passively for incoming connection requests rather than actively initiating them. To create a server socket, call listen(SSC) after the socket is bound.

listen(s,backlog);

The listen call identifies the socket that receives the connections and marks it as accepting connections. The maximum queue length (backlog) that listen can specify is five.

A call to listen does not complete the connection; it only indicates a willingness on the part of the server process to listen for incoming connection requests. Applications must call accept(SSC) to establish an actual connection:

accept(s, addr, addrlen)

The addr and addrlen arguments return the address of the connected client process, and accept automatically creates a new socket descriptor. Assuming the server is concurrent, the process immediately forks and the child process handles the connection while the parent continues to listen for more connection requests.

Client (Internet domain)

Client sockets actively initiate calls, rather than listening passively for incoming connection requests. Active sockets use the connect(SSC) function to establish connections. To create a socket to rendezvous with a server, the socket call is used as before. With streams sockets, it is not necessary to bind an address to the client (see table below). This is because with a connection-oriented protocol the server's address must be known, but the client's address is unimportant because no other process will try to access it. With connectionless sockets, however, each datagram must contain the destination address of the client and an address bound to it with the bind system call.

A client process issues the connect call in an attempt to establish a connection with a listening server process. A connection is a mechanism that avoids the need to transmit the identity of the sending socket each time data is sent.
The connect call provides for the exchange of the identities of each endpoint just once, prior to the transmission of data. Successful completion of connect results in a connection between two sockets.

connect(s, name, namelen)

connect does not return until a connection is established. s is the socket descriptor of the client process. name and namelen are pointers to the address family and size of the other socket.

Establishing a connection

Client Server  
socket() socket()  
  bind()  
  listen()  
connect() accept()  

Data transfer (Internet domain)

Once a pair of sockets is connected, data can flow from one to the other. The read(S) and write(C) functions may be used to do this, just as they are for normal files.

read(fildes, buf, nbyte)
write(fildes, buf, nbyte)

fildes refers to the socket descriptor returned by the socket call in the client, and by accept in the server. The write system call attempts to write nbyte bytes from the buffer pointed to by buf to the socket associated with fildes.

The send(SSC) and recv(SSC) calls are almost identical to read and write, except that they provide a flags argument.

send(s, msg, len, flags)
recv(s, msg, len, flags)

Applications using TCP use flags to signal the presence of urgent data. The flags argument may be specified as non-zero if one or more of the following constants is required:

MSG_OOB out-of-band data
MSG_PEEK look at data without reading
MSG_DONTROUTE send data without routing

Out-of-band data is a logically independent transmission channel associated with each pair of connected stream sockets. Out-of-band data is delivered to the user independent from normal data, along with a signal. For a more complete discussion of out-of-band data, see the SCO TCP/IP Programmer's Guide.

The MSG_PEEK flag lets the caller look at the data without discarding it as read. When specified in a recv call, any data present is returned to the user but treated as though still unread. The next read or recv call applied to the socket will return the data previously previewed.

The MSG_DONTROUTE option specifies that normal routing mechanisms are bypassed and the message is directed to the network interface specified in the network portion of the destination address.

The close(S) system call is used to discard the end of a socket connection when it is no longer needed. If one end of a socket is closed and the other tries to write to it, the write will return an error. s is the socket descriptor being closed in:

close(s)

If the socket is a stream socket, the system will continue to attempt to transfer data. The shutdown(SSC) call causes all or part of a full-duplex connection to be terminated:

shutdown(s, how)

If argument how is 0, further receives are disabled. When how is 1, further sends are disallowed, while 2 disallows additional sends and receives.

Byte order

Because of differences between various computer architectures and network protocols, the byte-swapping routines can be used to simplify manipulation of names and addresses:

htonl converts 32-bit host to network long integer
htons converts 16-bit host to network short integer
ntohl converts 32-bit network to host long integer
ntohs converts 16-bit network to host short integer

For example, to convert an integer before sending it through a socket:

   i=htonl(i);
   write_data(s, &i, sizeof(i));
After reading data, it can be converted back:
   read_data(s, &i, sizeof(i));
   i=ntohl(i);

See the bstring(S) manual page in the SCO TCP/IP Programmer's Reference for further information on byte ordering routines.


Next topic: Code samples (Internet domain)
Previous topic: Address Structures and Routines (Internet domain)

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003