Client and server communicate using send() and recv()
as shown below:
num = send(s, addr_of_data, len_of_data, 0);
num = recv(s, addr_of_buffer, len_of_buffer, 0);
The send()
and recv() calls specify:
- The socket s on which to communicate
- The address in storage of the buffer that contains, or will contain,
the data (addr_of_data, addr_of_buffer)
- The size of this buffer (len_of_data, len_of_buffer)
- A flag that tells how the data is to be sent
Flag 0 tells TCP/IP to transfer the data normally. The server
uses the socket that is returned from the accept() call.
These functions return the amount of data that was sent
or received. Because stream sockets send and receive information in
streams of data, it can take more than one send() or recv() to transfer
all of the data. It is up to the client and the server to agree on
some mechanism to signal that all of the data has been transferred.
When the conversation is over, both the client and the
server call close() to end the connection. Close() also deallocates
the socket, freeing its space in the table of connections. To end
a connection with a specific client, the server closes the socket
returned by accept(). If the server closes its original socket, it
can no longer accept new connections, but it can still converse with
the clients to which it is connected. The close() call is represented
as follows:
close(s);
If you are writing a client application, you might want to verify
the processes the server will use. Both client applications and the
servers with which they communicate must be aware of the sequence
of events each will follow.