2012-07-13 133 views
0

我很抱歉我的英语,但我的软件有一些问题,我需要一些帮助。但首先,一些代码!Python客户端将不会重新连接到服务器

客户端:

if connessione.connect(host, port) == True: 
    connect = True 
    print 'connection granted' 
else: 
    connect = False 
    print 'connection refused' 

while 1: 
    do_some_stuff_with_socket 

    if connect == False: 
     if connessione.connect(host, port) == True: 
      connect = True 

服务器端:(互联网

import socket 
port = 4000 
host = '127.0.0.1' 
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
server_socket.bind((host, port)) 
server_socket.listen(5) 
print "Type 'Q' or 'q' to QUIT" 
print "Server Waiting for client on port ", port 
while 1: 
    client_socket, address = server_socket.accept() 
    print "Connection from ", address 
    while 1: 
     server_data = raw_input("--> server: ") 
     if server_data.lower() == 'q': 
      client_socket.send(server_data) 
      client_socket.close() 
      break 
     else: 
      client_socket.send(server_data) 
     client_data = client_socket.recv(1024) 
     if client_data.lower() == 'q': 
      print "Quit from client" 
      client_socket.close() 
      break 
     else: 
      print "<-- client: ", client_data 
    break 

如果我重新启动/断开服务器上找到,客户端不重新连接。我使用.terminate().close()方法来关闭插座。

+1

当您断开连接时是否出现错误? – 2012-07-13 15:53:30

+0

为什么打印后的“< - client:”,client_data?把1强制外部而不重新运行。 client_socket,address = server_socket.accept()不会再被调用。 – DevPlayer 2012-07-13 16:24:58

+0

@IT忍者:不..没有错误抛出... :( – imAlessandro 2012-07-13 18:40:01

回答

2

我已经通过调用之前.connect(host, port)

这是不优雅.__init__()方法解决我的问题,但它的作品。

感谢所有

相关问题