免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8518 | 回复: 4
打印 上一主题 下一主题

请教:使用socket.recv(1024)时出现阻塞,怎么处理啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-26 14:42 |只看该作者 |倒序浏览
写了一个简单的socket的client程序,连接服务器,在使用socket.recv(1024)接收服务器返回的消息时,
出现了阻塞,程序就一直停在那不往下走了,怎么办啊??


import socket
host = '192.168.1.101'
port = 4567
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(host,port)
s.send("1111111111")

buf= s.recv(1024)

论坛徽章:
0
2 [报告]
发表于 2011-12-29 22:56 |只看该作者
只有结束程序了,要不就等到有返回

论坛徽章:
0
3 [报告]
发表于 2011-12-30 01:09 |只看该作者
开个线程

论坛徽章:
0
4 [报告]
发表于 2011-12-30 01:23 |只看该作者
回复 1# dongdong82CU

设个超时时间用settimeout,或者线程, select什么的都可以


   

论坛徽章:
0
5 [报告]
发表于 2012-01-02 00:11 |只看该作者
socket.setblocking(flag)
Set blocking or non-blocking mode of the socket: if flag is 0, the socket is set to non-blocking, else to blocking mode. Initially all sockets are in blocking mode. In non-blocking mode, if a recv() call doesn’t find any data, or if a send() call can’t immediately dispose of the data, a error exception is raised; in blocking mode, the calls block until they can proceed. s.setblocking(0) is equivalent to s.settimeout(0.0); s.setblocking(1) is equivalent to s.settimeout(None).

这个是你需要的么
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP