套接字recv(1)似乎阻止而不是返回文件结尾。 [英] Socket recv(1) seems to block instead of returning end of file.

查看:58
本文介绍了套接字recv(1)似乎阻止而不是返回文件结尾。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在执行netstring实现的过程中,我注意到如果你使用socket'的recv(1)构建一个记录,那么当你关闭

遥控器结束,recv(1)挂起,尽管在0.1套时间内有短暂的

时间。


但是,如果你尝试接收多个字符,(我用3测试过,

没试过2),然后当你关闭远程端时你不要

得到一个超时,但是空字符串 - 文件的正常结束,我想。


有没有人看到过这种行为?


传输一边似乎给出了一个破损的管道错误,这很好。


我使用的是标准的SuSe 10,Python 2.4,开箱即用。


- 亨德里克

解决方案

2007-08-23,Hendrik van Rooyen< ma ** @microcorp.co.zawrote:


在进行网络字符串实现时,我注意到如果你是

使用socket'的recv(1)构建一个记录,然后当你关闭

远程结束时,recv(1)挂起,



我没有看到Gentoo上运行2.4的行为。


尽管短时间超出了0.1套。



多长时间?套接字的recv方法不会超时。


但是,如果你尝试接收多个char,(我测试了

3,没试过2),然后当你关闭遥控器结束时

你没有时间,但空字符串 - 正常

我想是文件的结尾。


有没有人看到过这种行为?



编号recv(1)对我来说很好(Gentoo下的Python 2.4)。

也许你可以发一个最小的例子你工作了吗?
你呢?


----------------------- -------------------------------------------------

#!/ usr / bin / python

#reader

进口插座


HOST =''''#符号名称表示本地主机

PORT = 8765#任意非特权端口

s = socket.socket(socket.AF_INET,socket。 SOCK_STREAM)

s.bind((HOST,PORT))

s.listen(1)

conn,addr = s.accept( )

打印''连接'',addr

而1:

data = conn.recv(1)

打印rx:,len(数据)

如果不是数据:break

conn.close()

- -------------------------------------------------- ---------------------

--------------------- ----------------- ----------------------------------

#!/ usr / bin / python


#jrite

导入套接字,随机


PORT = 8765#任意非特权端口

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect ((主持人,港口))

打印''连接到'',((主持人,港口))

for i in range(10):

data =" abcdefghijklmnopqrstuvwxyz" [:random.randint(1,20)]

s.send(data)

print" tx:" ,len(数据)

conn.close()

------------------------ ------------------------------------------------ <无线电通信/>

-

格兰特爱德华兹格兰特哇! Loni Anderson的头发

at应该是LEGALIZED !!

visi.com


On Thu,23 2007年8月11:54:01 +0200,Hendrik van Rooyen写道:


>

在执行netstring实现时我发现如果你用

使用socket'的recv(1)建立一个记录,然后当你关闭远程端的
时,recv(1)挂起,尽管有一个短期

超过0.1套的时间。


但是,如果你尝试收到多个字符,(我测试了3,

没有尝试2),然后当你关闭远程端时,你没有获得超时时间,但是空字符串 - 我认为是文件的正常结束。


有没有人看到过这种行为?


传输方似乎会出现管道错误,这很好。

我使用的是标准的SuSe 10,Python 2.4,开箱即用。


- Hendrik



您使用的是sock.settimeout()吗?

我总是使用select在python中完成超时套接字; IINM,

settimeout方法是一个新的补充。


我同意Grant的意见 - 发布一小段代码

复制问题会帮助我们帮助你。事实上,它可能对你有所帮助

帮助自己:)


2007-08-23,Dan Stromberg - Datallegro< ds ** ******@datallegro.comwrote:


您使用的是sock.settimeout()吗?



嘿,当我没有看到
时,有人偷偷进入套接字模块......


我同意Grant的意见 - 发布一小段代码,

复制问题将有助于我们帮助你。事实上,它可能对你有所帮助

帮助你自己:)



是的,根据我的经验,我试图产生一个最小的失败

例子通常会显示出错了什么。


-

格兰特爱德华兹格兰特哇!我在超市的EXTENDERS看到了这些EGG



visi.com ...我已经阅读了

说明......



While doing a netstring implementation I noticed that if you
build a record up using socket''s recv(1), then when you close
the remote end down, the recv(1) hangs, despite having a short
time out of 0.1 set.

If however, you try to receive more than one char, (I tested with 3,
did not try 2), then when you shut the remote end down you do not
get a time out, but an empty string - the normal end of file, I suppose.

Has anybody else seen this behaviour?

The transmit side seems to give a broken pipe error, which is fine.

I am using stock standard SuSe 10, Python 2.4, out of the box.

- Hendrik

解决方案

On 2007-08-23, Hendrik van Rooyen <ma**@microcorp.co.zawrote:

While doing a netstring implementation I noticed that if you
build a record up using socket''s recv(1), then when you close
the remote end down, the recv(1) hangs,

I don''t see that behavior running 2.4 on Gentoo.

despite having a short time out of 0.1 set.

What time out? A socket''s recv method doesn''t do timeouts.

If however, you try to receive more than one char, (I tested
with 3, did not try 2), then when you shut the remote end down
you do not get a time out, but an empty string - the normal
end of file, I suppose.

Has anybody else seen this behaviour?

No. recv(1) works fine for me (Python 2.4 under Gentoo).
Perhaps you could post a minimal example that doesn''t work for
you?

------------------------------------------------------------------------
#!/usr/bin/python

#reader
import socket

HOST = '''' # Symbolic name meaning the local host
PORT = 8765 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ''Connected by'', addr
while 1:
data = conn.recv(1)
print "rx:",len(data)
if not data: break
conn.close()
------------------------------------------------------------------------
------------------------------------------------------------------------
#!/usr/bin/python

# writer
import socket,random

HOST = '''' # Symbolic name meaning the local host
PORT = 8765 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
print ''Connected to'',((HOST,PORT))
for i in range(10):
data = "abcdefghijklmnopqrstuvwxyz"[:random.randint(1,20)]
s.send(data)
print "tx:",len(data)
conn.close()
------------------------------------------------------------------------

--
Grant Edwards grante Yow! Loni Anderson''s hair
at should be LEGALIZED!!
visi.com


On Thu, 23 Aug 2007 11:54:01 +0200, Hendrik van Rooyen wrote:

>
While doing a netstring implementation I noticed that if you
build a record up using socket''s recv(1), then when you close
the remote end down, the recv(1) hangs, despite having a short
time out of 0.1 set.

If however, you try to receive more than one char, (I tested with 3,
did not try 2), then when you shut the remote end down you do not
get a time out, but an empty string - the normal end of file, I suppose.

Has anybody else seen this behaviour?

The transmit side seems to give a broken pipe error, which is fine.

I am using stock standard SuSe 10, Python 2.4, out of the box.

- Hendrik

Are you using sock.settimeout()?

I''ve always done timed-out sockets in python using select; IINM, the
settimeout method is a new addition.

I agree with Grant though - posting a minimal snippet of code that
replicates the problem would help us help you. In fact, it might help you
help yourself :)


On 2007-08-23, Dan Stromberg - Datallegro <ds********@datallegro.comwrote:

Are you using sock.settimeout()?

Hey, somebody snuck timeouts into the socket module when I wasn''t
looking...

I agree with Grant though - posting a minimal snippet of code that
replicates the problem would help us help you. In fact, it might help you
help yourself :)

Yup, in my experience attempting to produce a minimally failing
example will usually reveal what''s wrong.

--
Grant Edwards grante Yow! I have seen these EGG
at EXTENDERS in my Supermarket
visi.com ... I have read the
INSTRUCTIONS ...


这篇关于套接字recv(1)似乎阻止而不是返回文件结尾。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆