如何知道socket是否仍然连接 [英] how to know if socket is still connected

查看:101
本文介绍了如何知道socket是否仍然连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

2006-07-16, ne ***** @ xit.net < ne ***** @ xit.netwrote:
On 2006-07-16, ne*****@xit.net <ne*****@xit.netwrote:

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


运行良好,但有时服务器会断开连接。所以,

我需要的东西会让我知道如果

连接仍然可以,如果没有将重新连接。
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.



如果服务器已关闭连接,那么

套接字上的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.


我的想法,因为它只允许你连接某个

一次一个端口,我可以尝试使用 - 每次连接

,如果它无法连接(因为它已经是)

然后我会继续。但是如果它没有连接,它将重新连接
。这就是我在这里的原因。好像它好像b $ b会起作用,但还有更好的方法吗?
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?



我不明白为什么普通的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




Grant Edwards写道:

Grant Edwards wrote:

2006-07-16, ne*****@xit.net < ne ***** @ xit.netwrote:
On 2006-07-16, ne*****@xit.net <ne*****@xit.netwrote:

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


效果很好,但有时服务器会断开连接。所以,

我需要的东西会让我知道如果

连接仍然可以,如果没有将重新连接。
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.



如果服务器已关闭连接,那么

套接字上的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.


我的想法,因为它只允许你连接某个

一次一个端口,我可以尝试使用 - 每次连接

,如果它无法连接(因为它已经是)

然后我会继续。但是如果它没有连接,它将重新连接
。这就是我在这里的原因。好像它好像b $ b会起作用,但还有更好的方法吗?
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?



我不明白为什么普通的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


2006-07-16, ne*****@xit.net < ne ***** @ xit.netwrote:
On 2006-07-16, ne*****@xit.net <ne*****@xit.netwrote:

>如果服务器已关闭连接,然后
套接字上的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.


是这样的吗?

databack = aeris_sockobj.recv(2048)

如果数据包:

view_msg =''捕获请求确认%s字节\ n''%

len(数据包)

else:

view_msg =''未能从aeris服务器接收数据\ 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 ?



是的,如果问题是主机关闭连接,那么应该有效的
。模压破损的缩进和

换行。 ;)


-

格兰特爱德华兹格兰特哇!我的意思是土豆

现场......

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是否仍然连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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