问题描述
lo那里,
i有一个简单的应用程序连接到套接字以从服务器获取信息
i看起来像这样
serverhost =''xxx.xxx.xxx.xxx''
serverport = 9520
aeris_sockobj = socket.socket(socket.AF_INET,socket。 SOCK_STREAM)
aeris_sockobj.connect((serverhost,serverport))
而1:
用套接字做这个或那个,
发送和接收信息。
yadda yadda yadda
效果很好,但有时服务器会断开连接。
所以,我需要的东西会让我知道如果连接
仍然可以,如果没有将重新连接。
我的想法,因为它只允许你在一个特定的端口连接一个
a时间,
我可以尝试使用 - 除非连接每次,如果它不能
connect(因为它已经是)然后我会继续。但如果
没有连接,它会重新连接。
这就是我带来的地方。看起来它会起作用,但是有更好的方式吗?这有点似乎是一个肮脏的黑客。
任何意见?
谢谢。
lo there,
i have a simple app that connects to a socket to get info from a server
i looks like this
serverhost = ''xxx.xxx.xxx.xxx''
serverport = 9520
aeris_sockobj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
aeris_sockobj.connect((serverhost,serverport))
while 1:
do this or that with socket,
send and receive info.
yadda yadda yadda
works well, but sometimes the server drops the connection.
so, what i need is something that will let me know if the connection
is still ok, if not will reconnect.
what i thought, since it only lets you connect on a certain port one at
a time,
that i could use a try-except to connect every time, if it could not
connect (because it already is) then i would just continue on. But if
it is not connected, it would reconnect.
that is what brings me here. Seems like it would work, but is there a
better way ? this kinda seems like a dirty hack.
any opinions ?
thanks.
推荐答案
如果服务器已关闭连接,那么
套接字上的recv()将返回一个空字符串",
套接字上的send()会引发异常。
If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.
我不明白为什么普通的send()和recv()语义不足以让b
足够。
-
格兰特爱德华兹格兰特哇!我是东区
在TYPE ...
visi.com
I don''t see why the normal send() and recv() semantics aren''t
sufficient.
--
Grant Edwards grante Yow! I''m an East Side
at TYPE...
visi.com
如果服务器已关闭连接,那么
套接字上的recv()将返回一个空字符串",
套接字上的send()会引发异常。
If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.
我不明白为什么普通的send()和recv()语义不足以让b $ b足够。
-
格兰特爱德华兹格兰特哇!我是东区
在TYPE ...
visi.com
I don''t see why the normal send() and recv() semantics aren''t
sufficient.
--
Grant Edwards grante Yow! I''m an East Side
at TYPE...
visi.com
这样吗?
databack = aeris_sockobj.recv(2048)
如果databack:
view_msg =''抓住请求确认%s字节\\ \\ n'%
len(databack)
else:
view_msg =''未能从aeris server \ n'接收数据'
然后把重新连接放在else:block?
谢谢
谢谢
like this ?
databack = aeris_sockobj.recv(2048)
if databack:
view_msg = ''caught request acknowlage %s bytes \n'' %
len(databack)
else:
view_msg = ''fail to recieve data from aeris server\n''
then put the reconnect in the else: block ?
thanks
thanks
是的,如果问题是主机关闭连接,那么应该有效的
。模压破损的缩进和
换行。 ;)
-
格兰特爱德华兹格兰特哇!我的意思是土豆
现场......
visi.com
Yes, if the problem is that the host closes the connection,
that should work. Modulo the broken indentation and
line-wrapping. ;)
--
Grant Edwards grante Yow! My mind is a potato
at field...
visi.com
这篇关于如何知道socket是否仍然连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!