[PYTHON] The story of trying to reconnect the client

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")

problem

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.

solution(?)

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()

Other stumbling blocks

-When I ran python, I just made it a local network Since it has been set, I have to review the firewall settings

The page I referred to when creating the server / client:

[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

The story of trying to reconnect the client
The story of adding MeCab to ubuntu 16.04
The story of trying deep3d and losing
The story of pep8 changing to pycodestyle
The story of sys.path.append ()
Story of trying to use tensorboard with pytorch
The story of trying Sourcetrail × macOS × VS Code
The story of moving from Pipenv to Poetry
The story of trying to push SSH_AUTH_SOCK obsolete on screen with LD_PRELOAD
The story of building Zabbix 4.4
[Apache] The story of prefork
Story of trying competitive programming 2
The story of wanting to buy Ring Fit Adventure
The story of using circleci to build manylinux wheels
A story of a high school graduate technician trying to predict the survival of the Titanic
I can't find the clocksource tsc! ?? The story of trying to write a kernel patch
The story of trying to contribute to COVID-19 analysis with AWS free tier and failing
A story about trying to introduce Linter in the middle of a Python (Flask) project
The story of introducing jedi (python auto-completion package) to emacs
The story of copying data from S3 to Google's TeamDrive
Technology that supports jupyter: traitlets (story of trying to decipher)
After all, the story of returning from Linux to Windows
The story of Python and the story of NaN
The story of participating in AtCoder
The story of the "hole" in the file
Story of trying competitive programming Part 1
The story of remounting the application server
Supplement to the explanation of vscode
The story of writing a program
A story that struggled to handle the Python package of PocketSphinx
[Pythonista] The story of making an action to copy selected text
The story of failing to update "calendar.day_abbr" on the admin screen of django
The story of the algorithm drawing a ridiculous conclusion when trying to solve the traveling salesman problem properly
The story of an error in PyOCR
Script to change the description of fasta
10 methods to improve the accuracy of BERT
How to check the version of Django
The story of verifying the open data of COVID-19
The story of making Python an exe
The story of making an immutable mold
The story of manipulating python global variables
The story of deciphering Keras' LSTM model.predict
The story of blackjack A processing (python)
zoom I tried to quantify the degree of excitement of the story at the meeting
When you want to change the HTTP headers of Flask's test client
A story of a deep learning beginner trying to classify guitars on CNN
I tried to extract and illustrate the stage of the story using COTOHA
I tried the common story of using Deep Learning to predict the Nikkei 225
A story about trying to improve the testing process of a system written in C language for 20 years
How to calculate the volatility of a brand
The story of doing deep learning with TPU
How to find the area of the Voronoi diagram
The story of low learning costs for Python
Combinatorial optimization to find the hand of "Millijan"
The story of IPv6 address that I want to keep at a minimum
Setting to output the log of cron execution
The inaccuracy of Tensorflow was due to log (0)
Set the range of active strips to the preview range
The story of making the Mel Icon Generator version2
Twitter streaming client to enjoy in the terminal
I tried to touch the API of ebay