Python socket.error: [Errno] Address already the reason for use and resolution _python

Source: Internet
Author: User
Tags ack in python

A brief analysis of the reasons

Today in writing a python and HTML5 Websocket instance, the stop run rerun script always prompts the address already exists and is used! Query related documents just know in the socket programming, when the client to the server to send a message, shut down the connection, then if you run the server-side program immediately, will prompt this error:

Copy Code code as follows:

Socket.error: [Errno] Address already

This is because in the TCP/IP termination connection four times, when the final ACK reply is issued, there is a 2MSL time to wait, MSL refers to a fragment in the network the largest survival time, this time is generally 30 seconds, so basically after 60 seconds can reconnect!
Why wait for 2MSL? is because at the end of the ACK reply, the sender can not confirm whether the ACK is normal to the other end, if the other end did not receive an ACK reply, will be sent in 1MSL after the fin fragment. So said the sender wait 2MSL time, that is, it sent an ACK and the other send the fin time, if the time is not again received fin fragments, the sender assumes that the other party has received the ACK reply normally, at this time it will normally close the connection!

Ii. Solutions

If a socket-bound address in Python is in use, an error often occurs.

Under Linux:

Copy Code code as follows:

"Socket.error: [Errno] Address already" is displayed.

Under Windows:
Copy Code code as follows:

"Socket.error: [Errno 10048] is usually used only once per socket address (Protocol/network address/port)."

This is because the socket does not support address reuse by default, and if you want to reuse the need to display settings, that is, call the Setsockop function T before binding allows the socket to allow address reuse: socket.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)

For example:

Copy Code code 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)

Related Article

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.