send |
Prototype: |
#include <sys/socket.h>
#include <resolv.h>
int send(int sockfd, char *buffer, int msg_len,
int options);
|
General Description: |
Send a message to the connected peer, client or server. This call is like the write() system call, but permits you to define additional options for channel control. |
Return Value: |
Like write(), the call returns the number of bytes written. The byte count may be less that msg_len. If call does not succeed in writing all required bytes, you may use a loop for successive writes. If negative, the call stores the error detail in errno. |
Parameters |
sockfd |
The socket channel. This may be a connected SOCK_DGRAM or SOCK_STREAM socket. |
buffer |
The data to send. |
msg_len |
The number of bytes to send. |
options |
A set of flags to enable special message handling:
- MSG_OOB - Send message out-of-band (urgent).
- MSG_DONTROUTE - Send the message bypassing all routers. If unsuccessful, the network sends an error back.
- MSG_DONTWAIT - Don't allow blocking. Similar to the fcntl() option call, but only applies to this call. If the call would block, the call returns with EWOULDBLOCK in errno.
- MSG_NOSIGNAL - If the peer severs the connection, don't raise a SIGPIPE signal locally.
|
Possible Errors |
EBADF |
An invalid descriptor was specified. |
ENOTSOCK |
The argument sockfd is not a socket. |
EFAULT |
An invalid user space address was specified for buffer. |
EMSGSIZE |
The call can't complete, because the socket requires the message be sent atomically, and the size of the message to be sent has made this impossible. |
EAGAIN |
The socket is marked non-blocking and the requested operation would block. |
ENOBUFS |
The system was unable to allocate an internal memory block. The operation may succeed when buffers become available. |
EINTR |
A signal occurred. |
ENOMEM |
No memory available. |
EINVAL |
Invalid argument passed. |
EPIPE |
The local end has been shut down on a connected socket. In this case the process will also receive a SIGPIPE unless MSG_NOSIGNAL is set. |
Examples |
int sockfd;
int bytes, bytes_wrote=0;
/*--- Create socket, connect to server/peer ---*/
while ( (bytes = send(sockfd, buffer, msg_len, 0)) > 0 )
if ( (bytes_wrote += bytes) >= msg_len )
break;
if ( bytes < 0 )
perror("send");
|
int sockfd;
int bytes, bytes_wrote=0;
/*--- Create socket, connect to server ---*/
if ( send(sockfd, buffer, 1, MSG_OOB) != 1 )
perror("Urgent message");
|