While True: Try: Print (" Connecting to the server @ % s: % d... "% (SERVER_IP SERVER_PORT)) Socket_tcp. Connect (server_addr) Break Except the socket. Error: Print (" Can 't connect to the server, try it latter!" ) Time. Sleep (1) The continue Print (" identifiers package... ") # socket connection is successful, waiting for receiving information
While True: Try: Number=socket_tcp. Send (" hello world ") Print the number Time. Sleep (5) Except the socket. Error: Print (" socket error ") Except: Other print (" error ") Except: Pass
You take a look at the code, the code I mainly as a client socket, link to a remote server, every 5 seconds to the remote server sends a "hello world", in the process of sending, if there is a socket link error, is output on the screen "socket error", Now, so I use python debugger PyCharm on desktop computers to run the program, after normal link socket, assuming that the network error first, such as disconnecting or have no network, then by definition the socket should be disconnected, if I pull the cable after about 30 seconds, PyCharm only output "socket error", Based on the computer side, I copy the same program to "" raspberry pie" "(raspberries pie is own raspbian) system to run on the same case, such as disconnecting, passes through more than 20 minutes to output" socket error ", I want now is to at the time of the socket connection errors, such as pull the cable, network error caused by such as socket connection error, able to quickly detect the socket connection error, then the socket connection, the big question now is do not know how to monitor the socket state of link,, every brother please give advice or comments! ~
CodePudding user response:
I also come across this problem, did not delve into, directly into a short connection
CodePudding user response:
Use settimeout (5) If 5 seconds no reaction, obtaining an exception is thrown, prompt,