Add Book to My BookshelfPurchase This Book Online

Chapter 14 - Networking with Sockets

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Client-Side Functions
To communicate with a server process, a client process needs to call, in order, the functions described below.
Connecting to a Server
To connect to a server using a stream-based socket, the client program calls the connect function:
    #include <sys/types.h>
    #include <sys/socket.h>
    int connect(int s, struct sockaddr *name, int addrlen);
This function connects the socket referenced by s to the server at the address described by name. The addrlen parameter specifies the length of the address in name. If the connection is completed, connect returns 0. Otherwise, it returns -1 and places the reason for failure in errno.
A client may also use connect to connect a datagram socket to the server. This is not strictly necessary, and does not actually establish a connection. However, it does enable the client to send datagrams on the socket without having to specify the destination address for each datagram.
Transferring Data
To transfer data on a stream-based connection, the client and server may simply use read and write. However, there are also two functions specifically used with stream-based sockets:
    #include <sys/types.h>
    #include <sys/socket.h>
    int recv(int s, char *buf, int len, int flags);
    int send(int s, const char *buf, int len, int flags);
These functions are exactly identical to read and write, except that they have a fourth argument. This argument allows the program to specify flags that affect how the data is sent or received. The flags are:
MSG_DONTROUTE
If specified in a call to send, this flag disables network routing of the data. It is only used by diagnostic and routing programs.
MSG_OOB
If specified in a call to send, the data is sent as out-of-band data. This data “jumps over” any other data that has been sent and not received. It is used, for example, to handle interrupt characters in a remote login session. If specified in a call to recv, any pending out-of-band data will be returned instead of “regular” data.
MSG_PEEK
If specified in a call to recv, the data is copied into buf as usual, but it is not consumed. Another call to recv will return the same data. This allows a program to “peek” at the data before reading it, to decide how it should be handled.
When using datagram-based sockets, the server does not call listen or accept, and the client (generally) does not call connect. Thus, there is no way for the operating system to automatically figure out where data on these sockets is to be sent. Instead, the sender must tell the operating system each time where to deliver the data, and the receiver must ask where it came from. To do this, two other functions are defined:
    #include <sys/types.h>
    #include <sys/socket.h>
    int recvfrom(int s, char *buf, int len, int flags,
            struct sockaddr *from, int *fromlen);
    int sendto(int s, const char *buf, int len, int flags,
            struct sockaddr *to, int tolen);
The sendto function sends len bytes from buf via the socket referenced by s to the server located at the address given in to. The tolen parameter specifies the length of the address. The number of bytes actually transferred is returned, or -1 if an error occurred. There is no indication whether or not the data actually reaches its destination. The recvfrom function receives up to len bytes of data from the socket referenced by s and stores them in buf. The address from which the data came is stored in from, and fromlen is modified to indicate the length of the address. The number of bytes received is returned, or -1 if an error occurs.
Destroying the Communications Channel
Sockets may be closed with the close function, with the side effect that if the socket refers to a stream-based socket, the close will block until all data has been transmitted.
The shutdown function may also be used to shut down the communications channel:
    #include <sys/types.h>
    #include <sys/socket.h>
    int shutdown(int s, int how);
This function shuts down either one or both sides of the communications channel referenced by s, depending on the value of how. If how is 0, the socket is shut down for reading; all further reads from the socket return end-of-file. If how is 1, the socket is shut down for writing; all further writes to the socket will fail. This also informs the operating system that no effort need be made to deliver any outstanding data on the socket. If how is 2, then both sides of the socket are shut down and it becomes essentially useless.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback