Socket写失败的解决方案

borgvardt 2006-01-15 11:10:42
原先Socket数据流量小时,TClientSocket.Socket.Sendbuf(Buf, BufLength)不会出现什么问题,但是现在数据流量大了,出现了写失败的情况,返回的值出现-1和0两种情况。
为了防止写失败,我使用以下语句解决了写失败的情况。

while True do
begin
if FClientSocket.Socket.SendBuf(Buf, BufLength) = BufLength then Break;
Sleep(1);
end;

但是总觉得有问题,各位有什么好方法么?
...全文
233 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
borgvardt 2006-01-21
  • 打赏
  • 举报
回复
顶起!
borgvardt 2006-01-18
  • 打赏
  • 举报
回复
这段话我看过,有什么好的解决方案么?
就是Sleep?

或者能否提高系统的SendBuf?
halfdream 2006-01-18
  • 打赏
  • 举报
回复
就算DELPHI自带的帮助很精略,但也明确说明了该怎么办:

.....
For non-blocking sockets, the data is sent to the WinSock DLL which has it's own internal buffers. If the WinSock can accept additional data, SendBuf returns immediately with the number of bytes queued. If the WinSock internal buffer space is not able to accept the buffer being sent, SendBuf returns -1 and no data is queued at all. In this case, wait a bit for the WinSock to have a chance to send out already-queued data; then try again.
.....


ZyxIp 2006-01-17
  • 打赏
  • 举报
回复
正和我想问的问题关不多。

我的服务器端用的是TServerSocket 客户端是TClientSocket,局域网,在一个客户端的情况下
每10毫秒发送一个400字节的数据包时,不会有问题。

如果客户端有10个,则服务器在收到3000个左右是就会造成服务器程序无任何提示的退出。

当然这是在我的测试条件下,实际情况不会达到这样的速度。

但这是什么原因呢?为什么会错。SOCKET 的底层是如何处理收到的数据的?如何才能防止出错呢????

借楼主宝地一问
borgvardt 2006-01-17
  • 打赏
  • 举报
回复
继续顶起!
borgvardt 2006-01-17
  • 打赏
  • 举报
回复
这个函数只返回3个值,-1,0和BufLength

继续顶!
kushu0318 2006-01-16
  • 打赏
  • 举报
回复
分包传送
定义一个数组0..1023,然后循环发送
borgvardt 2006-01-16
  • 打赏
  • 举报
回复
顶一下
国风 2006-01-16
  • 打赏
  • 举报
回复
不清楚你具体的问题,系统返回的错误信息是什么?
borgvardt 2006-01-16
  • 打赏
  • 举报
回复
我的包很小,在200个byte左右

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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