Address already in use

Source: Internet
Author: User
Tags set socket socket error

 

When the client is connected to the server, the server is disconnected and the server is started, the following error occurs: address already in USR.
You can use netstat-anp | more to see that the client is still connected to the server (the port of the Server Bind is still used ). This is because the client does not execute close, and the connection will wait for the client's Fin package for a while. The solution is to use setsockopt so that the socket can be reused, which is the most common Server programming point. The specific method is to add a socket setting between the socket call and the bind Call:
Int opt = 1;
Setsockopt (socket_fd, sol_socket, so_reuseaddr, & OPT, sizeof (OPT ));
Appendix: setsockopt usage.

Setsockopt (set socket status)
Related functions
Getsockopt
Header file
# Include <sys/types. h>;
# Include <sys/socket. h>;
Define functions
Int setsockopt (int s, int level, int optname, const void * optval, socklen_toptlen );
Function Description
Setsockopt () is used to set the socket status specified by parameter S. The level parameter indicates the network layer to be set. It is generally set to sol_socket to access the socket layer. The optname parameter indicates the option to be set, which has the following values:
So_debug enable or disable the troubleshooting Mode
So_reuseaddr allows repeated use of local addresses during the BIND () Process
So_type returns the socket format.
So_error: the cause of the socket error is returned.
Do not transmit data packets sent by so_dontroute using routing devices.
So_broadcast is transmitted through broadcast.
So_sndbuf
So_rcvbuf: set the size of the temporary storage area for receiving
So_keepalive regularly determines whether the connection is terminated.
So_oobinline is immediately sent to the standard input device when OOB data is received
So_linger ensures secure and reliable data transmission.
Parameters
Optval indicates the value to be set, and optlen indicates the length of optval.
Return Value
If the call succeeds, 0 is returned. If an error exists,-1 is returned. The error cause is stored in errno.
Additional instructions
The ebadf parameter S is not a valid socket processing code.
The enotsock parameter S is a file description, not a socket
The option specified by the enoprotoopt parameter optname is incorrect.
The efault parameter optval Pointer Points to the memory space that cannot be accessed.

 

From:Http://dev.firnow.com/course/6_system/linux/Linuxjs/2008630/129368.html

Www.firnow.com time: 2008-06-30 Author: anonymous name Edit:Http://www.firnow.com/

 

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.