Send data out over a socket
These functions send data to a socket. Generally speaking, send() is used for TCPSOCK_STREAM connected sockets, andsendto() is used for UDPSOCK_DGRAM unconnected datagram sockets. With the unconnected sockets, you must specify the destination of a packet each time you send one, and that's why the last parameters ofsendto() define where the packet is going.
With both send() and sendto(), the parameters is the socket,buf is a pointer to the data you want to send,len is the number of bytes you want to send, andflags allows you to specify more information about how the data is to be sent. Setflags to zero if you want it to be "normal" data. Here are some of the commonly used flags, but check your localsend() man pages for more details:
MSG_OOB |
Send as "out of band" data. TCP supports this, and it's a way to tell the receiving system that this data has a higher priority than the normal data. The receiver will receive the signalSIGURG and it can then receive this data without first receiving all the rest of the normal data in the queue. |
MSG_DONTROUTE |
Don't send this data over a router, just keep it local. |
MSG_DONTWAIT |
If send() would block because outbound traffic is clogged, have it returnEAGAIN. This is like a "enable non-blocking just for this send." See the section onblocking for more details. |
MSG_NOSIGNAL |
If you send() to a remote host which is no longer recv()ing, you'll typically get the signal SIGPIPE. Adding this flag prevents that signal from being raised. |
Returns the number of bytes actually sent, or -1 on error (and errno will be set accordingly.) Note that the number of bytes actually sent might be less than the number you asked it to send! See the section onhandling partialsend()s for a helper function to get around this.
Also, if the socket has been closed by either side, the process calling send() will get the signal SIGPIPE. (Unless send() was called with theMSG_NOSIGNAL flag.)
转自:http://xuxiaozhao163.blog.163.com/blog/static/37935922008101825456738/
在Socket编程中,发送数据报文可供使用的API函数有send,sendto和sendmsg,下面是关于前两个系统调用的原型:
#include <sys/socket.h>
ssize_t send( int socket, const void *buffer, size_t length, int flags );
请注意它的返回值的类型ssize_t,其含义是signed size。从内核代码中,我们可以看到,在32位系统上,它是int,在64位系统上,它是long。它常用于表示在某一次操作后,缓冲区中可以被读或写的字节数量。相对应的,还有一个数据类型size_t,其含义是unsigned size。常用于表示对象本身的大小,操作sizeof的返回值就是该类型,malloc,memcpy等函数的参数中用该类型表示对象的大小,在32位系统上,它是unsigned int,在64位系统上,它是unsigned long。
sys_sendto构建完这些后,调用sock_sendmsg继续执行发送流程,传入参数为struct msghdr和数据的长度。忽略中间的一些不重要的细节,sock_sendmsg继续调用__sock_sendmsg,__sock_sendmsg最后调用struct socket->ops->sendmsg,即对应套接字类型的sendmsg函数,所有的套接字类型的sendmsg函数都是inet_sendmsg,该函数首先检查本地端口是否已绑定,无绑定则执行自动绑定,而后调用具体协议的sendmsg函数。
下面再来看sendmsg系统调用:转自:http://hi.baidu.com/%B2%AB%CA%CB/blog/item/47a2293885dacc1abaa167ef.html
Linux系统调用之send/sendto/sendmsg函数解析
功能描述:
发送消息。send只可用于基于连接的套接字,send 和 write唯一的不同点是标志的存在,当标志为0时,send等同于write。
sendto 和 sendmsg既可用于无连接的套接字,也可用于基于连接的套接字。除了套接字设置为非阻塞模式,调用将会阻塞直到数据被发送完。
用法:
#include <sys/types.h>
#include <sys/socket.h>
ssize_t send(int sock, const void *buf, size_t len, int flags);
ssize_t sendto(int sock, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
ssize_t sendmsg(int sock, const struct msghdr *msg, int flags);
参数:
sock:索引将要从其发送数据的套接字。
buf:指向将要发送数据的缓冲区。
len:以上缓冲区的长度。
flags:是以下零个或者多个标志的组合体,可通过or操作连在一起
MSG_DONTROUTE:不要使用网关来发送封包,只发送到直接联网的主机。这个标志主要用于诊断或者路由程序。
MSG_DONTWAIT:操作不会被阻塞。
MSG_EOR:终止一个记录。
MSG_MORE:调用者有更多的数据需要发送。
MSG_NOSIGNAL:当另一端终止连接时,请求在基于流的错误套接字上不要发送SIGPIPE信号。
MSG_OOB:发送out-of-band数据(需要优先处理的数据),同时现行协议必须支持此种操作。
to:指向存放接收端地址的区域,可以为NULL。
tolen:以上内存区的长度,可以为0。
msg:指向存放发送消息头的内存缓冲,结构形态如下
struct msghdr {
void *msg_name;
socklen_t msg_namelen;
struct iovec *msg_iov;
size_t msg_iovlen;
void *msg_control;
socklen_t msg_controllen;
int msg_flags;
};
可能用到的数据结构有
struct cmsghdr {
socklen_t cmsg_len;
int cmsg_level;
int cmsg_type;
};
返回说明:
成功执行时,返回已发送的字节数。失败返回-1,errno被设为以下的某个值
EACCES:对于Unix域套接字,不允许对目标套接字文件进行写,或者路径前驱的一个目录节点不可搜索
EAGAIN,EWOULDBLOCK: 套接字已标记为非阻塞,而发送操作被阻塞
EBADF:sock不是有效的描述词
ECONNRESET:连接被用户重置
EDESTADDRREQ:套接字不处于连接模式,没有指定对端地址
EFAULT:内存空间访问出错
EINTR:操作被信号中断
EINVAL:参数无效
EISCONN:基于连接的套接字已被连接上,同时指定接收对象
EMSGSIZE:消息太大
ENOMEM:内存不足
ENOTCONN:套接字尚未连接,目标没有给出
ENOTSOCK:sock索引的不是套接字
EPIPE:本地连接已关闭
转自:http://www.linuxidc.com/Linux/2007-11/9434p2.htm