2017-04-02 65 views
0

我是套接字编程的新手,但我有一个麻烦,我有两台服务器在端口3500和5000上的本地主机上启动。现在我想让我的客户端连接到端口3500执行一些操作,然后从3500断开连接,而服务器将只运行客户端将从3500断开连接,并且它将连接到端口5000,并执行一些操作。在套接字中连接两个服务器

我使用下面的代码要做到这一点,但得到的错误: -

import socket 

s=socket.socket() 
s.connect(('127.0.0.1',3500)) 
print("connectd to 3500") 
print("hello friends") 
s.close() 
print("disconnect from 3500") 
s.connect(('127.0.0.1',5000)) 
print("connected to 5000") 
s.close() 

但它并不能够连接到即端口5000.It第二连接而连接被连接到端口3500,但成功到5000它会抛出一个错误。 请任何一个短了我的错误

错误: -

File "C:\Users\Lal rishav\Desktop\HubPort\test.py", line 9, in <module> 
s.connect(('127.0.0.1',5000)) 
File "C:\Python27\lib\socket.py", line 228, in meth 
return getattr(self._sock,name)(*args) 
File "C:\Python27\lib\socket.py", line 174, in _dummy 
raise error(EBADF, 'Bad file descriptor') 
+0

在服务器运行correctly.The第二服务器始终抛出错误要么是端口3500或5000端口 –

回答

2

You cannot do any operations on a closed socket,创建一个新的来代替。

s.close() 
# the socket is closed, you can't use it anymore! 

# get another one: 
s2 = socket.socket() 
s2.connect(('127.0.0.1',5000)) 
s2.close() 
# now s2 is closed, you can't use it anymore! 
+0

它的工作感谢名单了很多朋友 –

+0

@lalrishav,欢迎您! – ForceBru

相关问题