Sending and receiving data can be done with any one of several calls. The calls vary according to the amount of information to be transmitted and received and the state of the socket being used to perform the operation.
While the send() and recv() calls are virtually identical to the readv() and writev() calls, the extra flags argument in the send() and recv() calls is important. The flags, defined in the <SYS\SOCKET.H> file, can be defined as a nonzero value if the application program requires one or more of the following:
MSG_OOB
Out-of-band data is specific to stream sockets. The option to have data sent without routing applied to the outgoing packets is currently used only by the routing table management process, and is unlikely to be of interest to the casual user. The ability to preview data is, however, of general interest. When the MSG_PEEK flag is specified with a recv() call, any data present is returned to the user, but treated as still unread. That is, the next readv() or recv() call applied to the socket returns the data previously previewed.
The following example shows an application sending data on a connected socket
and receiving data in response. The flags field can be used to specify
additional options to send() or recv(), such as sending out-of-band data.
For additional information, see send() and
recv(). AnApplicationUsingthesend ( )andrecv
( )Calls
int bytes_sent; int bytes_received; char data_sent[256] = "data to be sent on connected socket"; char data_received[256]; int s; ... bytes_sent = send(s, data_sent, sizeof(data_sent), 0); ... bytes_received = recv(s, data_received, sizeof(data_received), 0);
An example of an application sending from a connected socket and receiving
data in response is:
An Application Using the sendto() and recvfrom() Call
int bytes_sent; int bytes_received; char data_sent[256] = "data to be sent using sendto()"; char data_received[256]; struct sockaddr_in to; struct sockaddr from; int addrlen; int s; ... memset(&to, 0, sizeof(to)); to.sin_len = sizeof(to); to.sin_family = AF_INET; to.sin_addr.s_addr = inet_addr("129.5.24.1"); to.sin_port = htons(1024); ... bytes_sent = sendto(s, data_sent, sizeof(data_sent), 0, (struct sockaddr *) &to, sizeof(to)); ... addrlen = sizeof(from); /* must be initialized */ bytes_received = recvfrom(s, data_received, sizeof(data_received), 0, &from, &addrlen);
The sendto() and recvfrom() calls take additional parameters that allow the caller to specify the recipient of the data or to be notified of the sender of the data. See recvfrom(), and sendto(), for more information about these additional parameters.
A list of the data transfer calls and a summary of some of their characteristics follows:
Data Transfer Calls
┌────────────────────────────────────────────────────────────────────┐ │Data Transfer Buffers Option Flags? Sockets Used Server │ │Call With Address │ │ Required? │ ├────────────────────────────────────────────────────────────────────┤ │send() Single Yes Connected No │ │ only │ ├────────────────────────────────────────────────────────────────────┤ │recv() Single Yes Connected No │ │ only │ ├────────────────────────────────────────────────────────────────────┤ │sendto() Single Yes Any socket Yes │ ├────────────────────────────────────────────────────────────────────┤ │recvfrom() Single Yes Any socket Yes │ ├────────────────────────────────────────────────────────────────────┤ │writev() Multiple No Connected No │ │ only │ ├────────────────────────────────────────────────────────────────────┤ │readv() Multiple No Connected No │ │ only │ ├────────────────────────────────────────────────────────────────────┤ │sendmsg() Multiple Yes Any socket Yes │ ├────────────────────────────────────────────────────────────────────┤ │recvmsg() Multiple Yes Any socket Yes │ └────────────────────────────────────────────────────────────────────┘
For additional information that you may need when obtaining or setting socket options, see: