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()
方法來關閉插座。
當您斷開連接時是否出現錯誤? – 2012-07-13 15:53:30
爲什麼打印後的「< - client:」,client_data?把1強制外部而不重新運行。 client_socket,address = server_socket.accept()不會再被調用。 – DevPlayer 2012-07-13 16:24:58
@IT忍者:不..沒有錯誤拋出... :( – imAlessandro 2012-07-13 18:40:01