First, the reason analysis
Today is writing a python and HTML5 Websocket instance, the end run rerun script always prompt the address already exists and is used! Query the relevant documents only know in the socket programming, when the client sends a message to the server side, after the connection is closed, if you run the server side program immediately, will prompt this error:
The code is as follows:
Socket.error: [Errno 98] Address already in use
This is because in the TCP/IP termination of the four handshake, when the last ACK reply issued, there is a 2MSL time to wait, MSL refers to a fragment in the network maximum survival time, this time is generally 30 seconds, so basically after 60 seconds to reconnect!
Why wait for 2MSL? Because after the last ACK reply, the sender cannot confirm that the ACK is received by the other end, and if the other end does not receive an ACK, the fin fragment will be sent again after 1MSL. So the sender waits for 2MSL time, that is, the time it sends an ACK reply and the other side send fin fragment, if there is no fin fragment received again, the sender assumes that the other party has received the ACK reply normally, then it will normally close the connection!
Second, the solution
If the address of the socket binding in Python is in use, an error often occurs.
Under Linux:
The code is as follows:
The "Socket.error: [Errno 98] Address already in use" is displayed
Under Windows:
The code is as follows:
The "Socket.error: [Errno 10048] is usually allowed to be used only once per socket address (Protocol/network address/Port)"
This is because the socket default does not support address reuse, if you want to reuse the need to display settings, that is, before binding call Setsockop function T let the socket allow address reuse: socket.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)
For example:
The code is as follows:
Self.recsocket = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)
Self.recSocket.settimeout (Check_timeout)
Self.recSocket.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)
Self.recSocket.bind ((' ', Udp_port) ')