socket发送数据总大小 以及 发送缓冲区

shouso888 2013-09-29 04:09:06
1.以下句话对么? 要发送的数据大于发送缓冲区就返回错误了??
int send(SOCKET s, const char *buf, int len, int flags);
Socket的send函数的执行流程:send先比较待发送数据的长度len与套接字s的发送缓冲区的长度,如果len大于s的发送缓冲区的长度,则函数返回SOCKET_ERROR——http://blog.sina.com.cn/s/blog_732784f70100t0ti.html

2.《Unix网络编程》P48: 问我要发送个100M的文件,大于发送缓冲区了,就被投入睡眠了?
套接字阻塞状态,如果套接字的write发送缓冲区容不下应用进程的所有数据(或是应用进程的缓冲区大于套
接字的发送缓冲区,或是套接字的发送缓冲区中已有其他数据),该应用进程将被投入睡眠。直到应用进程
缓冲区中的所有数据都复制到套接字缓冲区write调用才返回。
...全文
1168 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-09-30
  • 打赏
  • 举报
回复
引用 14 楼 zhao4zhong1 的回复:
write socket对应线程sleep(suspend),等待对端recv取走待发送数据,当对端取走一定量数据后,write socket对应线程被操作系统wakeup进入待调度队列,当被调度占用CPU时,send返回。 我猜。(有人说“我猜”是我的口头禅之一) 建议参考linux中send相关源代码。(其实我也没看过,只是建议。)
自顶!
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 23 楼 max_min_ 的回复:
检查下返回值,看看errno是什么错误吧!
哦,明白。阻塞与否是我搞混了。谢谢提醒。 那另外个问题,在阻塞模式下,如果要发送的数据大于发送缓冲区大小,如果我不截断发送而是整体发送, 它会一直睡眠状态不返回是么?
max_min_ 2013-09-29
  • 打赏
  • 举报
回复
引用 22 楼 shouso888 的回复:
[quote=引用 21 楼 qq120848369 的回复:] [quote=引用 20 楼 shouso888 的回复:] [quote=引用 17 楼 qq120848369 的回复:] 阻塞模式下就会阻塞到全部写出.
write的数据太大的话,我们要反复多次调用它才能写完。。很明显write即使是阻塞也成功返回了, 返回值是写入的部分数据大小。 所以,不是阻塞到全部写出。我理解有误么[/quote] 永远检查返回值,谢谢.[/quote] 这什么和什么啊。 你说阻塞模式下会阻塞到全部写出;我说阻塞模式下可能只写了一部分就函数退出了,没有再阻塞直到全部写出。 返回值一直检查,谢谢。[/quote] 那说明写没有成功!出错退出了的!阻塞模式下的话,是要写完数据的! 检查下返回值,看看errno是什么错误吧!
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 21 楼 qq120848369 的回复:
[quote=引用 20 楼 shouso888 的回复:] [quote=引用 17 楼 qq120848369 的回复:] 阻塞模式下就会阻塞到全部写出.
write的数据太大的话,我们要反复多次调用它才能写完。。很明显write即使是阻塞也成功返回了, 返回值是写入的部分数据大小。 所以,不是阻塞到全部写出。我理解有误么[/quote] 永远检查返回值,谢谢.[/quote] 这什么和什么啊。 你说阻塞模式下会阻塞到全部写出;我说阻塞模式下可能只写了一部分就函数退出了,没有再阻塞直到全部写出。 返回值一直检查,谢谢。
qq120848369 2013-09-29
  • 打赏
  • 举报
回复
引用 20 楼 shouso888 的回复:
[quote=引用 17 楼 qq120848369 的回复:] 阻塞模式下就会阻塞到全部写出.
write的数据太大的话,我们要反复多次调用它才能写完。。很明显write即使是阻塞也成功返回了, 返回值是写入的部分数据大小。 所以,不是阻塞到全部写出。我理解有误么[/quote] 永远检查返回值,谢谢.
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 17 楼 qq120848369 的回复:
阻塞模式下就会阻塞到全部写出.
write的数据太大的话,我们要反复多次调用它才能写完。。很明显write即使是阻塞也成功返回了, 返回值是写入的部分数据大小。 所以,不是阻塞到全部写出。我理解有误么
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 16 楼 zhao4zhong1 的回复:
GETSOCKOPT Section: POSIX Programmer's Manual (P) Updated: 2003 --------------------------------------------------------------------------------
哥们,能不能不要这么暴力啊。。。你拷贝这么多东西,看帖的人很吃力啊,找下一个贴要翻半天。 我换个问法:为什么write一个大数据时,一次write返回值只会等于写进去了的数据。这个一次能成功 写多少是怎么个流程的,内部实现
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 17 楼 qq120848369 的回复:
阻塞模式下就会阻塞到全部写出.
怎么说,不用拆分成小部分然后发送。直接write 个大数据量是可以的是吧
qq120848369 2013-09-29
  • 打赏
  • 举报
回复
阻塞模式下就会阻塞到全部写出.
赵4老师 2013-09-29
  • 打赏
  • 举报
回复
GETSOCKOPT Section: POSIX Programmer's Manual (P) Updated: 2003 -------------------------------------------------------------------------------- NAME getsockopt - get the socket options SYNOPSIS #include <sys/socket.h> int getsockopt(int socket, int level, int option_name, void *restrict option_value, socklen_t *restrict option_len); DESCRIPTION The getsockopt() function manipulates options associated with a socket. The getsockopt() function shall retrieve the value for the option specified by the option_name argument for the socket specified by the socket argument. If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value. The level argument specifies the protocol level at which the option resides. To retrieve options at the socket level, specify the level argument as SOL_SOCKET. To retrieve options at other levels, supply the appropriate level identifier for the protocol controlling the option. For example, to indicate that an option is interpreted by the TCP (Transmission Control Protocol), set level to IPPROTO_TCP as defined in the <netinet/in.h> header. The socket in use may require the process to have appropriate privileges to use the getsockopt() function. The option_name argument specifies a single option to be retrieved. It can be one of the following values defined in <sys/socket.h>: SO_DEBUG Reports whether debugging information is being recorded. This option shall store an int value. This is a Boolean option. SO_ACCEPTCONN Reports whether socket listening is enabled. This option shall store an int value. This is a Boolean option. SO_BROADCAST Reports whether transmission of broadcast messages is supported, if this is supported by the protocol. This option shall store an int value. This is a Boolean option. SO_REUSEADDR Reports whether the rules used in validating addresses supplied to bind() should allow reuse of local addresses, if this is supported by the protocol. This option shall store an int value. This is a Boolean option. SO_KEEPALIVE Reports whether connections are kept active with periodic transmission of messages, if this is supported by the protocol. If the connected socket fails to respond to these messages, the connection shall be broken and threads writing to that socket shall be notified with a SIGPIPE signal. This option shall store an int value. This is a Boolean option. SO_LINGER Reports whether the socket lingers on close() if data is present. If SO_LINGER is set, the system blocks the process during close() until it can transmit the data or until the end of the interval indicated by the l_linger member, whichever comes first. If SO_LINGER is not specified, and close() is issued, the system handles the call in a way that allows the process to continue as quickly as possible. This option shall store a linger structure. SO_OOBINLINE Reports whether the socket leaves received out-of-band data (data marked urgent) inline. This option shall store an int value. This is a Boolean option. SO_SNDBUF Reports send buffer size information. This option shall store an int value. SO_RCVBUF Reports receive buffer size information. This option shall store an int value. SO_ERROR Reports information about error status and clears it. This option shall store an int value. SO_TYPE Reports the socket type. This option shall store an int value. Socket types are described in Socket Types . SO_DONTROUTE Reports whether outgoing messages bypass the standard routing facilities. The destination shall be on a directly-connected network, and messages are directed to the appropriate network interface according to the destination address. The effect, if any, of this option depends on what protocol is in use. This option shall store an int value. This is a Boolean option. SO_RCVLOWAT Reports the minimum number of bytes to process for socket input operations. The default value for SO_RCVLOWAT is 1. If SO_RCVLOWAT is set to a larger value, blocking receive calls normally wait until they have received the smaller of the low water mark value or the requested amount. (They may return less than the low water mark if an error occurs, a signal is caught, or the type of data next in the receive queue is different from that returned; for example, out-of-band data.) This option shall store an int value. Note that not all implementations allow this option to be retrieved. SO_RCVTIMEO Reports the timeout value for input operations. This option shall store a timeval structure with the number of seconds and microseconds specifying the limit on how long to wait for an input operation to complete. If a receive operation has blocked for this much time without receiving additional data, it shall return with a partial count or errno set to [EAGAIN] or [EWOULDBLOCK] if no data was received. The default for this option is zero, which indicates that a receive operation shall not time out. Note that not all implementations allow this option to be retrieved. SO_SNDLOWAT Reports the minimum number of bytes to process for socket output operations. Non-blocking output operations shall process no data if flow control does not allow the smaller of the send low water mark value or the entire request to be processed. This option shall store an int value. Note that not all implementations allow this option to be retrieved. SO_SNDTIMEO Reports the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. If a send operation has blocked for this time, it shall return with a partial count or with errno set to [EAGAIN] or [EWOULDBLOCK] if no data was sent. The default for this option is zero, which indicates that a send operation shall not time out. The option shall store a timeval structure. Note that not all implementations allow this option to be retrieved. For Boolean options, a zero value indicates that the option is disabled and a non-zero value indicates that the option is enabled. RETURN VALUE Upon successful completion, getsockopt() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error. ERRORS The getsockopt() function shall fail if: EBADF The socket argument is not a valid file descriptor. EINVAL The specified option is invalid at the specified socket level. ENOPROTOOPT The option is not supported by the protocol. ENOTSOCK The socket argument does not refer to a socket. The getsockopt() function may fail if: EACCES The calling process does not have the appropriate privileges. EINVAL The socket has been shut down. ENOBUFS Insufficient resources are available in the system to complete the function. The following sections are informative. EXAMPLES None. APPLICATION USAGE None. RATIONALE None. FUTURE DIRECTIONS None. SEE ALSO bind() , close() , endprotoent() , setsockopt() , socket() , the Base Definitions volume of IEEE Std 1003.1-2001, <sys/socket.h>, <netinet/in.h> COPYRIGHT Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . --------------------------------------------------------------------------------
赵4老师 2013-09-29
  • 打赏
  • 举报
回复
getsockopt The Windows Sockets getsockopt function retrieves a socket option. int getsockopt ( SOCKET s, int level, int optname, char FAR* optval, int FAR* optlen ); Parameters s [in] A descriptor identifying a socket. level [in] The level at which the option is defined; the supported levels include SOL_SOCKET and IPPROTO_TCP. See the Windows Sockets 2 Protocol-Specific Annex (a separate document included with the Platform SDK) for more information on protocol-specific levels. optname [in] The socket option for which the value is to be retrieved. optval [out] A pointer to the buffer in which the value for the requested option is to be returned. optlen [in/out] A pointer to the size of the optval buffer. Remarks The getsockopt function retrieves the current value for a socket option associated with a socket of any type, in any state, and stores the result in optval. Options can exist at multiple protocol levels, but they are always present at the uppermost "socket'' level. Options affect socket operations, such as the packet routing and out-of-band data transfer. The value associated with the selected option is returned in the buffer optval. The integer pointed to by optlen should originally contain the size of this buffer; on return, it will be set to the size of the value returned. For SO_LINGER, this will be the size of a LINGER structure. For most other options, it will be the size of an integer. The application is responsible for allocating any memory space pointed to directly or indirectly by any of the parameters it specified. If the option was never set with setsockopt, then getsockopt returns the default value for the option. The following options are supported for getsockopt. The Type column identifies the type of data addressed by optval. level = SOL_SOCKET Value Type Meaning SO_ACCEPTCONN BOOL Socket is listening. SO_BROADCAST BOOL Socket is configured for the transmission of broadcast messages. SO_DEBUG BOOL Debugging is enabled. SO_DONTLINGER BOOL If true, the SO_LINGER option is disabled. SO_DONTROUTE BOOL Routing is disabled. SO_ERROR int Retrieve error status and clear. SO_GROUP_ID GROUP The identifier of the group to which this socket belongs. SO_GROUP_PRIORITY int The relative priority for sockets that are part of a socket group. SO_KEEPALIVE BOOL Keepalives are being sent. SO_LINGER struct LINGER Returns the current linger options. SO_MAX_MSG_SIZE unsigned int Maximum size of a message for message-oriented socket types (for example, SOCK_DGRAM). Has no meaning for stream oriented sockets. SO_OOBINLINE BOOL Out-of-band data is being received in the normal data stream. (See section Windows Sockets 1.1 Blocking Routines & EINPROGRESS for a discussion of this topic.) SO_PROTOCOL_INFO WSAPROTOCOL_INFO Description of protocol info for protocol that is bound to this socket. SO_RCVBUF int Buffer size for receives SO_REUSEADDR BOOL The socket can be bound to an address which is already in use. SO_SNDBUF int Buffer size for sends SO_TYPE int The type of the socket (for example, SOCK_STREAM). PVD_CONFIG Service Provider Dependent An "opaque" data structure object from the service provider associated with socket s. This object stores the current configuration information of the service provider. The exact format of this data structure is service provider specific. level = IPPROTO_TCP TCP_NODELAY BOOL Disables the Nagle algorithm for send coalescing. BSD options not supported for getsockopt are: Value Type Meaning SO_RCVLOWAT int Receive low water mark SO_RCVTIMEO int Receive time-out SO_SNDLOWAT int Send low water mark SO_SNDTIMEO int Send time-out TCP_MAXSEG int Get TCP maximum segment size Calling getsockopt with an unsupported option will result in an error code of WSAENOPROTOOPT being returned from WSAGetLastError. ……
赵4老师 2013-09-29
  • 打赏
  • 举报
回复
write socket对应线程sleep(suspend),等待对端recv取走待发送数据,当对端取走一定量数据后,write socket对应线程被操作系统wakeup进入待调度队列,当被调度占用CPU时,send返回。 我猜。(有人说“我猜”是我的口头禅之一) 建议参考linux中send相关源代码。(其实我也没看过,只是建议。)
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 10 楼 zhao4zhong1 的回复:
你咋不发送整个硬盘给对端呢? 点击此处,你将下载整个Internet!?
我是假设发送那么大数据,因为我不知道socket的发送缓冲多大。 你说说为什么write socket 睡眠了,这句话应该是不对的吧
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
Section: Linux Programmer's Manual (2)
不要这么暴力吧。 让我man下就好了
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 9 楼 spirit008 的回复:
缓冲区最大就64K,你要一次发多少?看我回复的问题1的部分
“缓冲区最大 64K” 什么的缓冲区? 应用进程缓冲区还是socket发送缓冲区? 还是其他,大小可调整么 谢谢你第一部分的回答。我是linux的send,我man看看
赵4老师 2013-09-29
  • 打赏
  • 举报
回复
你咋不发送整个硬盘给对端呢? 点击此处,你将下载整个Internet!?
木头菇 2013-09-29
  • 打赏
  • 举报
回复
引用 7 楼 shouso888 的回复:
[quote=引用 3 楼 spirit008 的回复:] 1.不对,那个帖子写的东西也不都对。 If no error occurs, send returns the total number of bytes sent, which can be less than the number requested to be sent in the len parameter. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError. 就这么简单。 2.别想着直接调用send 100M,一次调用send别超过16K最好,8K一次的发吧,经验值
那到底为什么要截断发送啊?? 经验值有原理不。。我还看到有测试,多次发送大数据量,如果发送间隔太短就出错,等待一阵发送就正确了。 不懂[/quote] 缓冲区最大就64K,你要一次发多少?看我回复的问题1的部分
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 6 楼 fans1991 的回复:
[quote=引用 5 楼 shouso888 的回复:] [quote=引用 4 楼 fans1991 的回复:] 大数据分开发送。。。效果较好!!!
为么?? 分开发送为什么好[/quote] 数据过大,大局考虑,开线程去跑之类也行。。。不过看总体情况!!![/quote] 数据大了会引起什么?
shouso888 2013-09-29
  • 打赏
  • 举报
回复
引用 3 楼 spirit008 的回复:
1.不对,那个帖子写的东西也不都对。 If no error occurs, send returns the total number of bytes sent, which can be less than the number requested to be sent in the len parameter. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError. 就这么简单。 2.别想着直接调用send 100M,一次调用send别超过16K最好,8K一次的发吧,经验值
那到底为什么要截断发送啊?? 经验值有原理不。。我还看到有测试,多次发送大数据量,如果发送间隔太短就出错,等待一阵发送就正确了。 不懂
osbornZ 2013-09-29
  • 打赏
  • 举报
回复
引用 5 楼 shouso888 的回复:
[quote=引用 4 楼 fans1991 的回复:] 大数据分开发送。。。效果较好!!!
为么?? 分开发送为什么好[/quote] 数据过大,大局考虑,开线程去跑之类也行。。。不过看总体情况!!!
加载更多回复(5)

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧