Python client won't reconnect to server
-
20-06-2021 - |
Question
I'm sorry for my English, but I've some problems with my software and I need some help. But first of all, some code!
Client side:
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
Server side:(found on the internet)
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
If I reboot/disconnect the server, the client does not reconnect. I used the .terminate()
and .close()
methods to close the socket.
Solution
i have solved my problem by calling .__init__()
method before the .connect(host, port)
It is not elegant, but it works.
Thanks to all
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow