HPlogo HP 9000 Networking: BSD Sockets Interface Programmer's Guide > Chapter 4 Using Internet Datagram Sockets

Sending and Receiving Messages

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Glossary

 » Index

The sendto and recvfrom (or sendmsg and recvmsg) system calls are usually used to transmit and receive messages.

Sending Messages

Use sendto or sendmsg to send messages. sendmsg allows the send data to be gathered from several buffers.

If you have declared a default address, you can use send , sendto, or sendmsg to send messages. If you use sendto or sendmsg in this special case, be sure you specify 0 as the address value or an error will occur.

send is described in the "Sending Data" section in the "BSD Sockets: Using Internet Stream Sockets" chapter of this guide and in the send(2) man page. sendto and its parameters are described in the following table.

Include files:

#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

System call:

count = sendto(s,msg,len,flags,to,tolen) int s
char *msg;
int len, flags;
struct sockaddr *to
int tolen;

Parameter

Description of Contents

INPUT Value

s

socket descriptor of local socket

socket descriptor of socket sending message

msg

pointer to data buffer

pointer to data to be sent

len

size of data buffer

size of msg

flags

settings for optional flags

0 (no options are currently supported)

to

address of recipient socket

pointer to the socket address that message should be sent to

tolen

size of to

length of address structure that to points to

Function result:

Number of bytes actually sent, -1 in the event of an error.

Example:
count = sendto(s,argv[2],strlen(argv[2]),0,servaddr,sizeof(struct 
sockaddr_in));

The largest message size for this implementation is 32767 bytes.

You should not count on receiving error messages when using datagram sockets. The protocol is unreliable, meaning that messages may or may not reach their destination. However, if a message reaches its destination, the contents of the message are guaranteed to be intact.

If you need reliable message transfer, you must build it into your application programs or resend a message if the expected response does not occur.

When to Send Data

The client or server process should send data after sockets are bound. Refer to the send(2) man page for more information on sendto and sendmsg.

Receiving Messages

Use recvfrom or recvmsg to receive messages. recvmsg allows the read data to be scattered into buffers.

recv can also be used if you do not need to know what socket sent the message. However, if you want to send a response to the message, you must know where it came from. Except for the extra information returned by recvfrom and recvmsg, the three calls are identical.

recv is described in the "Receiving Data" section of the "BSD Sockets: Using Internet Stream Sockets" chapter in this guide and in the recv(2) man page. recvfrom and its parameters are described in the following table.

Include files:

#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

System call:

count = recvfrom(s,buf,len,flags,from,fromlen) int s;
char *buf;
int len, flags;
struct sockaddr *from
int *fromlen;

Parameter

Contents

INPUT Value

OUTPUT Value

s

socket descriptor of local socket

socket descriptor of socket receiving message

unchanged

buf

pointer to data buffer

pointer to buffer that is to receive data

pointer to received data

len

maximum number of bytes that should be received

size of data buffer

unchanged

flags

settings for optional flags

0 or MSG_PEEK

unchanged

from

address of socket that sent message

pointer to address structure, not used for input

pointer to socket address of socket that sent the message

fromlen

pointer to the size of from

pointer to size of from

pointer to the actual size of address returned

Function result:

Number of bytes actually received, -1 if an error occurs.

Example:
addrlen = sizeof(sockaddr_in);
...
count = recvfrom(s, buffer, BUFFERSIZE, 0, clientaddr, &addrlen);

recvfrom blocks until there is a message to be received.

No more than len bytes of data are returned. The entire message is read in one recvfrom, recvmsg, recv or read operation. If the message is too long for the allocated buffer, the excess data are discarded. Because only one message can be returned in a recvfrom call, if a second message is in the queue, it is not affected. Therefore, the best technique is to receive as much as possible on each call.

The host does not wait for len bytes to be available; if less than len bytes are available, that number of bytes are returned.

Flag Options

The flag options are:

  • 0 for no options.

  • MSG_PEEK for a nondestructive read.

Use the MSG_PEEK option to preview an incoming message. If this option is set on a recvfrom, any message returned remains in the data buffer as though it had not been read yet. The next recvfrom will return the same message.

When to Receive Data

The client or server process should receive data after sockets are bound. Refer to the recv(2) man page for more information on recvfrom and recvmsg.

© 1997 Hewlett-Packard Development Company, L.P.