Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

send(), sendto() - send a message from socket to socket

&pagelevel(3)&pagelevel



#include <sys.types.h>
#include <sys.socket.h>

#include <netinet.in.h> /* AF_INET, AF_INET6 and connectionless operation */


Kernighan-Ritchie-C:
int send(s, msg, len, flags);

int s;
char *msg;
int len, flags;


int sendto(s, msg, len, flags, to, tolen);

int s;
char *msg;
int len, flags;

struct sockaddr_in *to; /* AF_INET */
struct sockaddr_in6 *to; /* AF_INET6 */
int tolen;

ANSI-C:
int send(int s, char* msg, int len, int flags);
int sendto(int s, char* msg, int len, int flags, struct sockaddr* to,int tolen);


Description

The send() and sendto() functions send messages from one socket to another. send() can only be used with a socket on which a connection with connect() has been set up (see the connect() function in section "connect() - initiate a connection on a socket").

sendto() can also be used during connectionless operation. The function call sendto() with to î null pointer and tolen î 0 is only supported for datagrams.

The s parameter designates the socket from which a message is sent. The destination address is passed with to, where tolen specifies the length of the destination address.

The length of the message is specified with len. If the message is too long to be transported completely by the underlying protocol level, error SCEMSGSIZE is returned and the message is not transferred for datagram sockets (i.e. only AF_INET and AF_INET6).

The flags parameter is currently not supported and should be supplied with the value 0. A value not equal to 0 leads to an error, and the errno variable is set to the value EOPNOTSUPP.

If the message cannot be sent immediately, send() blocks if the socket was not set to the non-blocking input/output mode. You can use select() to determine when further data can be sent. 

Return value

>=0:

If successful. The value indicates the number of sent bytes.

-1:

If errors occur. errno is set to indicate the error.

Errors indicated by errno

EBADF

The s parameter is not a valid descriptor.

EFAULT

The length of the area for accepting the address is too small, or the length of the area for the message is too small.

EIO

I/O error. The message could not be passed to the transport system.

EMSGSIZE

The message is too long to be sent in one piece.

ENETDOWN

The connection to the network is down.

ENOTCONN

No connection exists for the socket. A read/write attempt was rejected.

EOPNOTSUPP

  • The flags parameter was specified with a non-zero value, and this is not supported.

or

  • The socket is not of type SOCK_STREAM, and the operation is supported only for stream sockets.

EPIPE

There is no connection to the partner (only with sockets of type SOCK_STREAM).

EWOULDBLOCK

The socket is marked as non-blocking, and the requested operation would block.

Note

If the connection is established with a non-blocking socket then errno EINPROGRESS may occur on the next function call. This message indicates that the connection is not yet in a state which permits a data transfer phase.

See also

connect(), getsockopt(), recv(), select(), soc_ioctl(), soc_write(), socket()