Python socket.error: [Errno 98] address already in use reason and resolution

Source: Internet
Author: User
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) ')

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.