Linux Socket Bind Error Address Already In Use
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about address already in use linux Stack Overflow the company Business Learn more about hiring developers or posting ads
How To Use So_reuseaddr
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow
Bind Failed Address Already In Use Iperf
is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error: Address already in use while binding socket with address but the port
Address Already In Use Socket
number is shown free by `netstat` up vote 30 down vote favorite 9 I tried to bind my socket(server socket) at port number 8000. It worked and did the job for me. At the end of the code I close the socket as well. The very next instant I run my code again and it shows me that the address is already in use. I have printed the meaning of address already in use python error values strerror(errno); to see if my code working properly at each point. To check if the port is free I checked it using netstat but it shows that port number 8000 is free. It has happened with me a lot of times. Every time I then wait for a few more secs and then it starts working again. I am using c language. So what is he reason for this behavior by my OS. After a few more secs I run the code and then it works. anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./a.out Socket Creation: Success File open: Success Socket Bind: Address already in use Socket Listen: Address already in use ^C anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1348/lighttpd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 984/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1131/cupsd tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1211/mysqld tcp6 0 0 :::22 :::* LISTEN 984/sshd tcp6 0 0 ::1:631 :::* LISTEN 1131/cupsd anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./a.out Socket Creation: Success File open: Success Socket Bind: Address already in use Socket Listen: Address already in use ^C anirudh@anirudh-Aspire-5920:~/Desktop/testing$ c linux sockets share|improve this question edited Dec 18 '15 at 20:40
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this address already in use flask site About Us Learn more about Stack Overflow the company Business Learn bind address already in use ssh more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question bind address already in use docker x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up socket.error: http://stackoverflow.com/questions/5106674/error-address-already-in-use-while-binding-socket-with-address-but-the-port-num [Errno 48] Address already in use up vote 61 down vote favorite 34 I'm trying to set up a server with python from mac terminal. I navigate to folder location an use: python -m SimpleHTTPServer But this gives me error: socket.error: [Errno 48] Address already in use I had previously open a connection using the same command for a different website in a different http://stackoverflow.com/questions/19071512/socket-error-errno-48-address-already-in-use location in my machine. python simplehttpserver share|improve this question edited Feb 18 '15 at 0:19 asked Sep 28 '13 at 20:48 irm 49031225 1 Kill the other process or run this one with a different port: python -m SimpleHTTPServer 8081 –Blender Sep 28 '13 at 20:50 add a comment| 4 Answers 4 active oldest votes up vote 131 down vote accepted You already have a process bound to the default port (8000). If you already ran the same module before, it is most likely that process still bound to the port. Try and locate the other process first: $ ps -fA | grep python 501 81651 12648 0 9:53PM ttys000 0:00.16 python -m SimpleHTTPServer The command arguments are included, so you can spot the one running SimpleHTTPServer if more than one python process is active. You may want to test if http://localhost:8000/ still shows a directory listing for local files. The second number is the process number; stop the server by sending it a signal: kill 81651 Alternatively, run the server on a different port, by specifying the alternative port on the command line: $ python -m SimpleHTTP
and both ends must ACK (acknowledge) each other's FIN packets. The FIN packets are initiated by the application performing a close(), a shutdown(), or an exit(). The ACKs are handled by the http://hea-www.harvard.edu/~fine/Tech/addrinuse.html kernel after the close() has completed. Because of this, it is possible for the process to complete before the kernel has released the associated network resource, and this port cannot be bound to another process until the https://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-4.html kernel has decided that it is done. Figure 1 Figure 1 shows all of the possible states that can occur during a normal closure, depending on the order in which things happen. Note that if you address already initiate closure, there is a TIME_WAIT state that is absent from the other side. This TIME_WAIT is necessary in case the ACK you sent wasn't received, or in case spurious packets show up for other reasons. I'm really not sure why this state isn't necessary on the other side, when the remote end initiates closure, but this is definitely the case. TIME_WAIT is the state that typically ties up the port for several address already in minutes after the process has completed. The length of the associated timeout varies on different operating systems, and may be dynamic on some operating systems, however typical values are in the range of one to four minutes. If both ends send a FIN before either end receives it, both ends will have to go through TIME_WAIT. Normal Closure of Listen Sockets A socket which is listening for connections can be closed immediately if there are no connections pending, and the state proceeds directly to CLOSED. If connections are pending however, FIN_WAIT_1 is entered, and a TIME_WAIT is inevitable. Note that it is impossible to completely guarantee a clean closure here. While you can check the connections using a select() call before closure, a tiny but real possibility exists that a connection could arrive after the select() but before the close(). Abnormal Closure If the remote application dies unexpectedly while the connection is established, the local end will have to initiate closure. In this case TIME_WAIT is unavoidable. If the remote end disappears due to a network failure, or the remote machine reboots (both are rare), the local port will be tied up until each state times out. Worse, some older operating systems do not implement a timeout for FIN_WAIT_2, and it is possible to get stuck there foreve
for this is that you have stopped your server, and then re-started it right away. The sockets that were used by the first incarnation of the server are still active. This is further explained in 2.7 Please explain the TIME_WAIT state., and 2.5 How do I properly close a socket?. 4.2 Why don't my sockets close? When you issue the close() system call, you are closing your interface to the socket, not the socket itself. It is up to the kernel to close the socket. Sometimes, for really technical reasons, the socket is kept alive for a few minutes after you close it. It is normal, for example for the socket to go into a TIME_WAIT state, on the server side, for a few minutes. People have reported ranges from 20 seconds to 4 minutes to me. The official standard says that it should be 4 minutes. On my Linux system it is about 2 minutes. This is explained in great detail in 2.7 Please explain the TIME_WAIT state.. 4.3 How can I make my server a daemon? There are two approaches you can take here. The first is to use inetd to do all the hard work for you. The second is to do all the hard work yourself. If you use inetd, you simply use stdin, stdout, or stderr for your socket. (These three are all created with dup() from the real socket) You can use these as you would a socket in your code. The inetd process will even close the socket for you when you are done. If you wish to write your own server, there is a detailed explanation in "Unix Network Programming" by Richard Stevens (see 1.5 Where can I get source code for the book [book title]?). I also picked up this posting from comp.unix.programmer, by Nikhil Nair ( nn201@cus.cam.ac.uk):
I worked all this lot out from the GNU C Library Manual (on-line documentation). Here's some code I wrote - you can adapt it as necessary: #include#include #include #include #include #include #include /* Global variables */ ... volatile sig_atomic_t keep_going = 1; /*