linux的send命令
linux下的send命令可用于基于連接的套接字,下面由學習啦小編為大家整理了linux的send命令的相關知識,希望對大家有幫助!
linux的send命令詳解
功能描述:發(fā)送消息,send只可用于基于連接的套接字,send 和 write唯一的不同點是標志的存在,當標志為0時,send等同于write。sendto 和 sendmsg既可用于無連接的套接字,也可用于基于連接的套接字。除了套接字設置為非阻塞模式,調用將會阻塞直到數(shù)據(jù)被發(fā)送完。
用法:
1
2
3
4
5
6
|
#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); |
linux的send命令參數(shù)
sock:索引將要從其發(fā)送數(shù)據(jù)的套接字。
buf:指向將要發(fā)送數(shù)據(jù)的緩沖區(qū)。
len:以上緩沖區(qū)的長度。
flags:是以下零個或者多個標志的組合體,可通過or操作連在一起
MSG_DONTROUTE:不要使用網(wǎng)關來發(fā)送封包,只發(fā)送到直接聯(lián)網(wǎng)的主機。這個標志主要用于診斷或者路由程序。
MSG_DONTWAIT:操作不會被阻塞。
MSG_EOR:終止一個記錄。
MSG_MORE:調用者有更多的數(shù)據(jù)需要發(fā)送。
MSG_NOSIGNAL:當另一端終止連接時,請求在基于流的錯誤套接字上不要發(fā)送SIGPIPE信號。
MSG_OOB:發(fā)送out-of-band數(shù)據(jù)(需要優(yōu)先處理的數(shù)據(jù)),同時現(xiàn)行協(xié)議必須支持此種操作。
to:指向存放接收端地址的區(qū)域,可以為NULL。
tolen:以上內存區(qū)的長度,可以為0。
msg:指向存放發(fā)送消息頭的內存緩沖,結構形態(tài)如下
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;
};
可能用到的數(shù)據(jù)結構有
struct cmsghdr {
socklen_t cmsg_len;
int cmsg_level;
int cmsg_type;
};
linux的send命令
![](http://lhpay.gzcl999.com/static/doc/images/pc/icon_star.png)
![](http://lhpay.gzcl999.com/static/doc/images/pc/icon_star.png)
![](http://lhpay.gzcl999.com/static/doc/images/pc/icon_star.png)
![](http://lhpay.gzcl999.com/static/doc/images/pc/icon_star.png)
![](http://lhpay.gzcl999.com/static/doc/images/pc/icon_star.png)