日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

send函数详解

發布時間:2023/12/8 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 send函数详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

send函數詳解

        • send
        • sendto
        • sendmsg

說明:本文主要是對man 幫助文檔的翻譯,若有錯誤,歡迎指正。

send

send:是一個系統調用函數,用來發送消息到一個套接字中,和sendto,sendmsg功能相似。

概要:

#include <sys/types.h> #include <sys/socket.h>ssize_t send(int sockfd, const void *buf, size_t len, int flags);

說明:

send()函數只能在套接字處于連接狀態的時候才能使用。(只有這樣才知道接受者是誰)

send和write的唯一區別就是最后一個參數:flags的存在,當我們設置flags為0時,send和wirte是同等的。

即:

// 下面的兩個函數等效: write(sockfd,buf,le); send(sockfd,buf,len,0)

當消息不適合套接字的發送緩沖區時,send通常會阻塞,除非套接字在事先設置為非阻塞的模式,那樣他不會阻塞,而是返回EAGAIN或者EWOULDBLOCK錯誤,此時可以調用select函數來監視何時可以發送數據。

參數描述

ssize_t類型在之前的read/write中描述符哦,相當于long。

sockfd:接收消息的套接字的文件描述符。

buf:要發送的消息。

len:要發送的字節數。

flags:flags參數表示下列標志中的0個或多個

MSG_CONFIRM :用來告訴鏈路層,

MSG_DONTROUTE:不要使用網關來發送數據,只發送到直接連接的主機上。通常只有診斷或者路由程序會使用,這只針對路由的協議族定義的,數據包的套接字沒有。

MSG_DONTWAIT :啟用非阻塞操作,如果操作阻塞,就返回EAGAIN或EWOULDBLOCK

MSG_EOR :當支持SOCK_SEQPACKET時,終止記錄。

MSG_MORE :調用方有更多的數據要發送。這個標志與TCP或者udp套接字一起使用

MSG_NOSIGNAL :當另一端中斷連接時,請求不向流定向套接字上的錯誤發送SIGPIPE ,EPIPE 錯誤仍然返回。

MSG_OOB:在支持此概念的套接字上發送帶外數據(例如,SOCK_STREAM類型);底層協議還必須支持帶外數據

返回值

在網絡中的傳遞錯誤對send來說是無法檢測的,能檢測到某些發送錯,則會返回-1;

否則返回發送的字節數。

EACCES (For UNIX domain sockets, which are identified by pathname) Write permission is denied on the destination socket file, or search per‐mission is denied for one of the directories the path prefix. (See path_resolution(7).)EAGAIN or EWOULDBLOCKThe socket is marked nonblocking and the requested operation would block. POSIX.1-2001 allows either error to be returned for thiscase, and does not require these constants to have the same value, so a portable application should check for both possibilities.EBADF An invalid descriptor was specified.ECONNRESETConnection reset by peer.EDESTADDRREQThe socket is not connection-mode, and no peer address is set.EFAULT An invalid user space address was specified for an argument.EINTR A signal occurred before any data was transmitted; see signal(7).EINVAL Invalid argument passed.EISCONNThe connection-mode socket was connected already but a recipient was specified. (Now either this error is returned, or the recipientspecification is ignored.)EMSGSIZEThe socket type requires that message be sent atomically, and the size of the message to be sent made this impossible.ENOBUFSThe output queue for a network interface was full. This generally indicates that the interface has stopped sending, but may be causedby transient congestion. (Normally, this does not occur in Linux. Packets are just silently dropped when a device queue overflows.)ENOMEM No memory available.ENOTCONNThe socket is not connected, and no target has been given.ENOTSOCKThe argument sockfd is not a socket.EOPNOTSUPPSome bit in the flags argument is inappropriate for the socket type.EPIPE The local end has been shut down on a connection oriented socket. In this case the p

sendto

#include <sys/types.h> #include <sys/socket.h> ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,const struct sockaddr *dest_addr, socklen_t addrlen);

sendto函數如果在連接狀態(SOCK_STREAM, SOCK_SEQPACKET)下使用,后面的兩個參數dest_addr和addrlen被忽略的,如果不把他們設置成null和0,會返回錯誤EISCONN,如果把他們設置成NULL和0,發現實際上不是連接狀態,會返回錯誤EISCONN。正常連接下,它和send同等。

//下面的兩個也是等效的: send(sockfd, buf, len, flags); sendto(sockfd, buf, len, flags, NULL, 0);

其他情況下,目標地址是由dest_addr給出,addrlen指定其大小。

sendmsg

#include <sys/types.h> #include <sys/socket.h> ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);struct msghdr {void *msg_name; /* optional address */socklen_t msg_namelen; /* size of address */struct iovec *msg_iov; /* scatter/gather array */size_t msg_iovlen; /* # elements in msg_iov */void *msg_control; /* ancillary data, see below */size_t msg_controllen; /* ancillary data buffer len */int msg_flags; /* flags on received message */};

對于sendmsg,目標地址是由msg的成員 msg.msg_name,來給出, msg.msg_namelen 指定大小。send和sendto發送消息的是buf,而且有它的指定長度len,但是在sendmsg中是由msg.msg_iov數組來決定的。所以sendmsg允許發送輔助信息(也稱為控制信息)。

如果發送的消息太長,不能以原子形式通過底層協議,則返回EMSGSIZE。并且不發送這個消息。

測試代碼

總結

以上是生活随笔為你收集整理的send函数详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。