send 中文man頁面
NAME
send, sendto, sendmsg - 從套接字發(fā)送消息
概述
#include <sys/types.h>
#include <sys/socket.h>
int send(int s, const void *msg, size_t len, int flags);
int sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
int sendmsg(int s, const struct msghdr *msg, int flags);
描述
Send, sendto, 和 sendmsg 用于向另一個套接字傳遞消息. Send 僅僅用于連接套接字,而 sendto 和 sendmsg 可用于任何情況下.
目標(biāo)地址用 to 指定, tolen 定義其長度.消息的長度用 len 指定. 如果消息太長不能通過下層協(xié)議,函數(shù)將返回 EMSGSIZE 錯誤,消息也不會被送出.
在數(shù)據(jù)傳送過程中所產(chǎn)生的錯誤不會返回給 send. 如果發(fā)生本地錯誤,則返回-1.
當(dāng)要發(fā)送的消息長度大于套接字當(dāng)前可用緩沖區(qū)時, send 將阻塞,除非在套接字上設(shè)置了非阻塞式輸入輸出模式. 對于非阻塞模式,這種情況下將返回 EAGAIN 錯誤. The 系統(tǒng)調(diào)用 select(2) 可以用來檢測何時可以發(fā)送更多的數(shù)據(jù).
參數(shù) flags 是一個標(biāo)志字,可以包含下列標(biāo)志:
- 對于支持帶外數(shù)據(jù)的套接字,
- MSG_OOB 將送出 out-of-band (帶外)數(shù)據(jù)(比如, SOCK_STREAM 類型的套接字); 下層協(xié)議也必須支持. 帶外 數(shù)據(jù).
- MSG_DONTROUTE
- 在送出分組時不使用網(wǎng)關(guān).只有直接連接在網(wǎng)絡(luò)上的主機(jī)才能接收到數(shù)據(jù).這個標(biāo)志通常僅用于診斷和路由程序. 可路由的協(xié)議族才能使用這個標(biāo)志;包套接字不可以.
- MSG_DONTWAIT
- 使用非阻塞式操作;如果操作需要阻塞,將返回 EAGAIN 錯誤(也可以用 F_SETFL fcntl(2) 設(shè)置 O_NONBLOCK 實現(xiàn)這個功能.)
- MSG_NOSIGNAL
- 當(dāng)流式套接字的另一端中斷連接時不發(fā)送 SIGPIPE 信號,但仍然返回 EPIPE 錯誤.
- MSG_CONFIRM (僅用于Linux 2.3以上版本)
- 通知鏈路層發(fā)生了轉(zhuǎn)發(fā)過程:得到了另一端的成功應(yīng)答. 如果鏈路層沒有收到通知,它將按照常規(guī)探測網(wǎng)絡(luò)上的相鄰主機(jī)(比如通過免費arp). 只能用于 SOCK_DGRAM 和 SOCK_RAW 類型的套接字,且僅對IPv4和IPv6有效.詳情參見 arp(7)
結(jié)構(gòu)體 msghdr 的定義如下.詳情參見 recv(2) 和下文.
-
struct msghdr { void * msg_name; /*地址選項*/ socklen_t msg_namelen; /*地址長度*/ struct iovec * msg_iov; /*消息數(shù)組*/ size_t msg_iovlen; /*msg_iov中的元素個數(shù)*/ void * msg_control; /*輔助信息,見下文*/ socklen_t msg_controllen; /*輔助數(shù)據(jù)緩沖區(qū)長度*/ int msg_flags; /*接收消息標(biāo)志*/ };
-
可以使用 msg_control 和 msg_controllen 成員發(fā)送任何控制信息.內(nèi)核所能處理的最大控制消息緩沖區(qū)長度由 net.core.optmem_max sysctl對每個套接字進(jìn)行限定;參見 socket(7).
返回值
成功時返回發(fā)送的字符個數(shù),否則返回-1.
錯誤代碼
其中一些是套接字層產(chǎn)生的標(biāo)準(zhǔn)錯誤.其他的是下層協(xié)議模塊產(chǎn)生的;參見各自的man手冊.
- EBADF
- 指定了非法描述符.
- ENOTSOCK
- 參數(shù) s 不是一個套接字.
- EFAULT
- 參數(shù)指定的用戶地址空間非法.
- EMSGSIZE
- 消息長度越界.
- EAGAIN或者EWOULDBLOCK
- 套接字設(shè)置為非阻塞式,但所請求的操作需要阻塞.
- ENOBUFS
- 網(wǎng)絡(luò)接口輸出隊列已滿.這通常表明接口已停止發(fā)送,也有可能是暫時性的擁擠(這不會發(fā)生在linux下,當(dāng)設(shè)備隊列溢出時數(shù)據(jù)報只是被簡單丟棄.
- EINTR
- 接收到信號.
- ENOMEM
- 沒有可用內(nèi)存.
- EINVAL
- 傳遞的參數(shù)非法.
- EPIPE
- 連接套接字的本地端已關(guān)閉.這種情況下進(jìn)程還會接收到 SIGPIPE 信號,除非設(shè)置了 MSG_NOSIGNAL
兼容于
4.4BSD,SVr4,POSIX1003.1g草案(這些系統(tǒng)調(diào)用首次出現(xiàn)于4.2BSD). MSG_CONFIRM 是Linux所做的擴(kuò)展.
注意
上面給出的函數(shù)原型遵循Single Unix Specification, glibc2也是這么做的; flags 參數(shù)在BSD4.*中是`int',但在libc4和libc5中是`unsigned int'; 參數(shù) len 在BSD4.*和libc4中是`int',但在libc5中是'size_t'; 參數(shù) tolen 在BSD4.*,libc4和libc5中都是`int'. 參見 accept(2).
SEE ALSO
fcntl(2), recv(2), select(2), getsockopt(2), sendfile(2), socket(2), write(2), socket(7), ip(7), tcp(7), udp(7)
#p#
NAME
send, sendto, sendmsg - send a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
ssize_t send(int s, const void *buf, size_t len, int flags);
ssize_t sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
ssize_t sendmsg(int s, const struct msghdr *msg, int flags);
DESCRIPTION
The system calls send, sendto, and sendmsg are used to transmit a message to another socket.
The send call may be used only when the socket is in a connected state (so that the intended recipient is known). The only difference between send and write is the presence of flags. With zero flags parameter, send is equivalent to write. Also, send(s,buf,len) is equivalent to sendto(s,buf,len,NULL,0).
The parameter s is the file descriptor of the sending socket.
If sendto is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET) socket, the parameters to and tolen are ignored (and the error EISCONN may be returned when they are not NULL and 0), and the error ENOTCONN is returned when the socket was not actually connected. Otherwise, the address of the target is given by to with tolen specifying its size. For sendmsg, the address of the target is given by msg.msg_name, with msg.msg_namelen specifying its size.
For send and sendto, the message is found in buf and has length len. For sendmsg, the message is pointed to by the elements of the array msg.msg_iov. The sendmsg call also allows sending ancillary data (also known as control information).
If the message is too long to pass atomically through the underlying protocol, the error EMSGSIZE is returned, and the message is not transmitted.
No indication of failure to deliver is implicit in a send. Locally detected errors are indicated by a return value of -1.
When the message does not fit into the send buffer of the socket, send normally blocks, unless the socket has been placed in non-blocking I/O mode. In non-blocking mode it would return EAGAIN in this case. The select(2) call may be used to determine when it is possible to send more data.
The flags parameter is the bitwise OR of zero or more of the following flags.
- MSG_OOB
- Sends out-of-band data on sockets that support this notion (e.g. of type SOCK_STREAM); the underlying protocol must also support out-of-band data.
- MSG_EOR
- Terminates a record (when this notion is supported, as for sockets of type SOCK_SEQPACKET).
- MSG_DONTROUTE
- Don't use a gateway to send out the packet, only send to hosts on directly connected networks. This is usually used only by diagnostic or routing programs. This is only defined for protocol families that route; packet sockets don't.
- MSG_DONTWAIT
- Enables non-blocking operation; if the operation would block, EAGAIN is returned (this can also be enabled using the O_NONBLOCK with the F_SETFL fcntl(2)).
- MSG_NOSIGNAL
- Requests not to send SIGPIPE on errors on stream oriented sockets when the other end breaks the connection. The EPIPE error is still returned.
- MSG_CONFIRM (Linux 2.3+ only)
- Tell the link layer that forward progress happened: you got a successful reply from the other side. If the link layer doesn't get this it'll regularly reprobe the neighbour (e.g. via a unicast ARP). Only valid on SOCK_DGRAM and SOCK_RAW sockets and currently only implemented for IPv4 and IPv6. See arp(7) for details.
The definition of the msghdr structure follows. See recv(2) and below for an exact description of its fields.
-
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 */ socklen_t msg_controllen; /* ancillary data buffer len */ int msg_flags; /* flags on received message */ };
-
You may send control information using the msg_control and msg_controllen members. The maximum control buffer length the kernel can process is limited per socket by the net.core.optmem_max sysctl; see socket(7).
RETURN VALUE
The calls return the number of characters sent, or -1 if an error occurred.
ERRORS
These are some standard errors generated by the socket layer. Additional errors may be generated and returned from the underlying protocol modules; see their respective manual pages.
- EAGAIN or EWOULDBLOCK
- The socket is marked non-blocking and the requested operation would block.
- EBADF
- An invalid descriptor was specified.
- ECONNRESET
- Connection reset by peer.
- EDESTADDRREQ
- The socket is not connection-mode, and no peer address is set.
- EFAULT
- An invalid user space address was specified for a parameter.
- EINTR
- A signal occurred before any data was transmitted.
- EINVAL
- Invalid argument passed.
- EISCONN
- The connection-mode socket was connected already but a recipient was specified. (Now either this error is returned, or the recipient specification is ignored.)
- EMSGSIZE
- The socket type requires that message be sent atomically, and the size of the message to be sent made this impossible.
- ENOBUFS
- The output queue for a network interface was full. This generally indicates that the interface has stopped sending, but may be caused by transient congestion. (Normally, this does not occur in Linux. Packets are just silently dropped when a device queue overflows.)
- ENOMEM
- No memory available.
- ENOTCONN
- The socket is not connected, and no target has been given.
- ENOTSOCK
- The argument s is not a socket.
- EOPNOTSUPP
- Some 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 process will also receive a SIGPIPE unless MSG_NOSIGNAL is set.
CONFORMING TO
4.4BSD, SVr4, POSIX 1003.1-2001. These function calls appeared in 4.2BSD.
POSIX only describes the MSG_OOB and MSG_EOR flags. The MSG_CONFIRM flag is a Linux extension.
NOTE
The prototypes given above follow the Single Unix Specification, as glibc2 also does; the flags argument was `int' in BSD 4.*, but `unsigned int' in libc4 and libc5; the len argument was `int' in BSD 4.* and libc4, but `size_t' in libc5; the tolen argument was `int' in BSD 4.* and libc4 and libc5. See also accept(2).
BUGS
Linux may return EPIPE instead of ENOTCONN.
SEE ALSO
fcntl(2), recv(2), select(2), getsockopt(2), sendfile(2), socket(2), write(2), socket(7), ip(7), tcp(7), udp(7)