我很抱歉我的英语,但我的软件有些问题,我需要一些帮助。但首先,一些代码!
客户方:
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()
方法关闭套接字。
答案 0 :(得分:2)
我通过在.__init__()
.connect(host, port)
方法解决了我的问题
它不优雅,但它有效。
感谢所有