By chance, I had to communicate between the server and the client. Therefore, even if the communication fails on the client side, I want to keep trying to connect again without terminating the execution! I thought, but I stumbled there ...
I thought I should recreate the socket, so I assigned it to an existing variable again! I couldn't do it ...
The program I used as a reference [excerpt from 2]
import socket
host = "xxx.xxx.xxx.xxx"
port = xxxx
client = socket.socket(socket.AF_INET,
socket.SOCK_STREAM) #Below, socket.Call it socket
client.connect((host, port)) #Below, socket.Call connect
client.send("from nadechin")
Actually, I think that it is often turned while while, but when the server goes down, the following cannot be done.
client = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
client.connect((host, port))
client.send("from nadechin")
=====Server goes down here=====
client = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
client.connect((host, port))
client.send("from nadechin")
Here, if I lost socket.socket or used other methods of socket such as close, shutdown, timeout, it didn't work.
Obviously, once I dropped the client, it worked. So, I thought it would be good if the socket that had already been created was destroyed, so I took this measure by creating the following class. (Error handling omitted for simplicity-try :)
class client(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#try:
self.client.connect((self.host, self.port))
def send(self):
#try:
self.client.send("ok")
def execute(self):
while 1:
self.__init__(self.host, self.port)
self.send()
client(host, port).execute()
When I wrote it as above, it worked.
It was strange that the following did not work. why···
class client(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#try:
self.client.connect((self.host, self.port))
def send(self):
#try:
self.client.send("ok")
while 1:
client(host, port).execute()
-When I ran python, I just made it a local network Since it has been set, I have to review the firewall settings
[1] Multithreaded server http://stackoverflow.com/questions/23828264/how-to-make-a-simple-multithreaded-socket-server-in-python-that-remembers-client [2] Client http://qiita.com/nadechin/items/28fc8970d93dbf16e81b [3] Error https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
It was very helpful. Thank you very much. I searched for error countermeasures on stack overflow, but there wasn't a very good one. For the time being, I am doing error 10053, 10054 (also 10056, 10057) by the method described above. Other errors continue with socket.connect with continue.
Click here for implementation? (Maybe there is) https://github.com/jackee777/server_and_client/tree/master/simple_server
Recommended Posts