send: Send data on a connected socket

The send socket function sends packets on the socket with descriptor s. The send function applies to all connected sockets.

Last updated

  • Changed for PUT14.
  • Added for PUT00.

Format

#include    <sys/socket.h>
int         send(int s,
                 char *msg,
                 int len
                 int flags);
s
The socket descriptor.
msg
Pointer to the buffer containing the message to transmit.
len
Length of the message pointed to by the msg parameter.
Note:
  1. The maximum send buffer size is 1,048,576 bytes.
  2. The default value of the SO_SNDBUF option is 32767.
  3. For a TCP socket, the maximum length that you can specify is 1 GB.
  4. For a UDP or RAW socket, the maximum length that you can specify is the smaller of the following values:
    • 65,527 bytes (for a UDP socket) or 32,767 bytes (for a RAW socket).
    • The send buffer size defined by the SO_SNDBUF option.
flags
Must be set to 0 or one or more of the following flags. If you specify more than one flag, use the logical OR operator (|) to separate them:
MSG_OOB
Sends out-of-band data on sockets that support it.
MSG_DONTROUTE
The SO_DONTROUTE option is turned on for the duration of the operation.

Normal return

No indication of failure to deliver is implicit in a send routine. However, if it succeeds, the number of characters sent is returned.

Error return

A return code equal to -1 indicates an error. You can get the specific error code by calling sock_errno. See z/TPF Transmission Control Protocol/Internet Protocol for more information about socket errors.
SOCINVAL
The value of the len parameter is not valid, the MSG_OOB option was specified for a socket that is not a stream socket, or the MSG_OOB option was specified, but out-of-band data is queued inline for this socket.
SOCNOBUFS
Buffer space is not available to send the message.
SOCNOTSOCK
The s parameter is not a valid socket descriptor.
SOCWOULDBLOCK
The s parameter is in nonblocking mode and no buffer space is available to hold the message to be sent.
SOCMSGSIZE
The message was too large to be sent.
SOCNOTCONN
The socket is not connected.
E1052STATE
The socket was closed because the system was in or cycling down to 1052 state.
ESYSTEMERROR
A system error has occurred and closed the socket.
SOCTIMEDOUT
The operation timed out. The socket is still available.

Programming considerations

  • If buffer space is not available at the socket to hold the message to be sent, the send function normally blocks, unless the socket is in nonblocking mode. See ioctl: Perform special operations on socket for a description of how to set nonblocking mode.
  • Use the select function to determine when to send more data.
  • The send timeout value (the SO_SNDTIMEO setsockopt option) determines how long to wait for space to become available in the send buffer before the send function times out.
  • For TCP sockets, if the value you specify for the len parameter is less than or equal to the send buffer size of the socket, the send process will be atomic; that is, either all of the data will be sent or none of it will be sent. If all of the data is sent, the return code is set to the value of the len parameter. If none of the data is sent, the return code is set to -1.
  • For TCP sockets, if the value you specify for the len parameter is greater than the send buffer size of the socket, the z/TPF system will take as much data as possible and return to the application indicating that only part of the data was processed. The application must issue more send calls for the remaining data and the application must serialize the send calls if the socket is being shared by multiple ECBs. If the send call is successful, the return code is set to a value from 1 to the value of the len parameter, which indicates how much data was sent.

Examples

The following example sends 256 bytes. No flag is used.
#include <sys/socket.h>
⋮
int bytes_sent;
int server_sock;
char data_sent[256];
⋮
bytes_sent = send(server_sock, data_sent, sizeof(data_sent), 0);
The following example sends 64000 bytes. No flag is used.
#include <sys/socket.h>
⋮
#define MESSAGE_SIZE  64000

int bytes_sent;
int server_sock;
int send_left;
int send_rc;
char *message_ptr;
⋮
message_ptr = malloc (MESSAGE_SIZE);
send_left = MESSAGE_SIZE;

while (send_left > 0)
      {
       send_rc = send(server_sock, message_ptr, send_left, 0);
       if send_rc == -1
          break;

       send_left -= send_rc;
       message_ptr += send_rc;
       }   /* End While Loop */