Linux socket programming address already in use problem

Source: Internet
Author: User

When I write the service-side program, after the end of the server-side program run, start the program again, the BIND function will return address already in using this error, prompting me that the port is already occupied.

Use # NETSTAT–APN | grep [port] command or Lsof-i:[port] command to see the port occupancy, you can find that the previously terminated server-side program process is still listening on the port. The kill command kills and then starts to run normally. Later in this article found the reason

You can use an bind API function to bind an address (an interface and a port) to a socket endpoint. You can use this function in server settings to limit the interfaces that may come with the connection. You can also use this function in client settings to limit the interfaces that should be used for connections that should be made available. The bind most common usage is to associate the port number and server, and use a wildcard address ( INADDR_ANY ), which allows any interface to be used for incoming connections.

bindThe common problem is trying to bind a port that is already in use. The trap is that there may not be an active socket, but it is still forbidden to bind the port ( bind returned EADDRINUSE ), which is caused by the TCP socket state TIME_WAIT . The status is retained for approximately 2-4 minutes after the socket is closed. TIME_WAITafter the status exits, the socket is deleted and the address can be re-bound without problems.

Waiting for TIME_WAIT the end can be annoying, especially if you are developing a socket server, you need to stop the server to make some changes, and then restart. Fortunately, there are ways to avoid the TIME_WAIT state. You can apply SO_REUSEADDR socket options to sockets so that ports can be reused immediately.

  Adding the following code before the BIND function solves the problem.

1 int 1 ; 2 if sizeof 0 )3{           4     perror ("Server setsockopt failed " ); 5     return 1 ; 6 }

Linux socket programming address already in use problem

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: 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.