HPlogo HP 9000 Networking: BSD Sockets Interface Programmer's Guide > Chapter 7 Using UNIX Domain 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 with datagram sockets.

Sending Messages

Use sendto or sendmsg to send messages. sendmsg is similar to sendto, except sendmsg allows the send data to be gathered from several buffers. sendto and its parameters are described in the following table.

Include files:

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

System call:

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

Parameter

Description of Contents

INPUT Value

s

socket descriptor of local socket

socket descriptor of socket that is sending the 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 if sendto succeeds, -1 if sendto call fails.

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

When to Send Data

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

Receiving Messages

Use recvfrom or recvmsg to receive messages. recvmsg is similar to recvfrom, except 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, the two calls are identical. recvfrom and its parameters are described in the following table.

Include files:

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

System call:

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

Parameter

Contents

INPUT Value

OUTPUT Value

s

socket descriptor of local socket

socket descriptor of socket receiving the message

unchanged

msg

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 (no options are supported

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 if recvfrom succeeds, -1 if recvfrom call fails.

Example:
struct  sockaddr_un     fromaddr;
int fromlen;
...
count = recvfrom(s, msg, sizeof(msg), 0, &fromaddr, &fromlen);

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 receive 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. Refer to the recv(2) man page for more information on recvfrom and recvmsg.

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