WSASend中的字符缓冲区问题

sangwf 2006-11-11 05:12:08
void Send()
{
char buffer[1024];
Databuf.len = 1024;
Databuf.buf = buffer;
WSASend(...);
}
这样会不会出现问题,执行完毕后,buffer会不会被清除掉,而出现部分待传输的数据被破坏?

还是要写为:
char* buffer= new char[1024];
Databuf.len = 1024;
Databuf.buf = buffer;
WSASend(...);
delete []buffer;
谢谢!
...全文
409 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
soulless 2006-11-21
  • 打赏
  • 举报
回复
Given the same buffer situation and a blocking socket,WSASend will return with only part of the application's buffers having been consumed.

注意前提是假定是阻塞SOCKET
ProgrameMan 2006-11-21
  • 打赏
  • 举报
回复
第一种方式没问题
sangwf 2006-11-18
  • 打赏
  • 举报
回复
第一种方式在异步模式下会不会有问题呢?谢谢!
sangwf 2006-11-18
  • 打赏
  • 举报
回复
谢谢楼上的,那这句话怎么理解?
WSASend will return with only part of the application's buffers having been consumed.

这个所谓的消费不是放到缓存区么?
soulless 2006-11-17
  • 打赏
  • 举报
回复
http://msdn2.microsoft.com/en-gb/library/ms742203.aspx
For nonoverlapped sockets, the last two parameters (lpOverlapped, lpCompletionRoutine) are ignored and WSASend adopts the same blocking semantics as send. Data is copied from the buffer(s) into the transport's buffer. If the socket is nonblocking and stream oriented, and there is not sufficient space in the transport's buffer, WSASend will return with only part of the application's buffers having been consumed. Given the same buffer situation and a blocking socket, WSASend will block until all of the application buffer contents have been consumed.
----------------------------------------------------------------------------
楼上的理解错误了吧?

这个似乎没有说非阻塞模式下会拷贝数据吧。。。

看看这段吧。。
A pointer to an array of WSABUF structures. Each WSABUF structure contains a pointer to a buffer and the length of the buffer. This array must remain valid for the duration of the send operation.

很明显 第二种方式在异步模式下会有问题的.
sangwf 2006-11-16
  • 打赏
  • 举报
回复
谢谢!,还是想明白第二种行不行。。
ProgrameMan 2006-11-16
  • 打赏
  • 举报
回复
你的第一种方式没问题
第二种方式尽量别用,(我个人把它理解为不可以)
sangwf 2006-11-16
  • 打赏
  • 举报
回复
哪位大侠解释一下,谢谢!
sangwf 2006-11-15
  • 打赏
  • 举报
回复
呵呵,所以这句话并不能指导我到底该怎么处理buffer啊。
ProgrameMan 2006-11-15
  • 打赏
  • 举报
回复

“允许你在客厅的沙发上睡觉”,你怎么理解这句话呢,你说这句话告诉你可以上床睡还是不可以上床睡?
sangwf 2006-11-14
  • 打赏
  • 举报
回复
This enables applications to build stack-based WSABUF arrays.

这里只是说明WSABUF可以声明局部变量,又没说WSABUF.buf所指内容的情况啊,谢谢!
ProgrameMan 2006-11-14
  • 打赏
  • 举报
回复
The array of WSABUF structures pointed to by the lpBuffers parameter is transient. If this operation is completed in an overlapped manner, it is the service provider's responsibility to capture these WSABUF structures before returning from this call. This enables applications to build stack-based WSABUF arrays.
Note The successful completion of a WSASend does not indicate that the data was successfully delivered.
sangwf 2006-11-13
  • 打赏
  • 举报
回复
不好意思,还是理解错了,这段英文还是没有说明传不完的时候数据本身是怎么处理的?

http://msdn2.microsoft.com/en-gb/library/ms742203.aspx
For nonoverlapped sockets, the last two parameters (lpOverlapped, lpCompletionRoutine) are ignored and WSASend adopts the same blocking semantics as send. Data is copied from the buffer(s) into the transport's buffer. If the socket is nonblocking and stream oriented, and there is not sufficient space in the transport's buffer, WSASend will return with only part of the application's buffers having been consumed. Given the same buffer situation and a blocking socket, WSASend will block until all of the application buffer contents have been consumed.
sangwf 2006-11-13
  • 打赏
  • 举报
回复
不好意思,我现在还不是很明白什么时候是同步,什么时候是异步。

我使用WSAAsynSelect模型,是不是就把socket设为了异步模式,对该socket所进行的操作就是异步模式?而不管是否使用重叠?

那如果这个算异步,我只能动态申请内存,并且在发送彻底成功了才能删除了?另外,我怎么判断彻底发送成功了呢?
sangwf 2006-11-13
  • 打赏
  • 举报
回复
这样看来,在变成阻塞模式之后,所要传输的数据还是要从lpBuf中复制到传输缓存区吧?
sangwf 2006-11-13
  • 打赏
  • 举报
回复
谢谢楼上的,算是明白了,当传输缓冲区满时,socket会变为阻塞模式,直到数据传送完为止。不是以前有些人认为的锁定内存。。
rickerliang 2006-11-13
  • 打赏
  • 举报
回复
对不起,刚查了一下msdn,原来WSASend在非阻塞模式下会把buffer缓存起来,所以立即delete是没问题的
http://msdn2.microsoft.com/en-gb/library/ms742203.aspx
For nonoverlapped sockets, the last two parameters (lpOverlapped, lpCompletionRoutine) are ignored and WSASend adopts the same blocking semantics as send. Data is copied from the buffer(s) into the transport's buffer. If the socket is nonblocking and stream oriented, and there is not sufficient space in the transport's buffer, WSASend will return with only part of the application's buffers having been consumed. Given the same buffer situation and a blocking socket, WSASend will block until all of the application buffer contents have been consumed.



另外,如果你使用WSAAsynSelect模型,socket自动被设置为非阻塞模式
rickerliang 2006-11-12
  • 打赏
  • 举报
回复
WSASend是否是异步
如果是,WSASend之后你就立刻delete了,会出问题的
sangwf 2006-11-11
  • 打赏
  • 举报
回复
那像我第二种方式会不会出现问题?
rickerliang 2006-11-11
  • 打赏
  • 举报
回复
异步一般在heap上分配,而且要等待异步io完成后才能释放heap
加载更多回复(2)

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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