How do I remove a CLOSE_WAIT socket connection? - c

I have write a simple program using socket in C that create a connect between X86 running windows and ARM running embedded linux(consist of only Busybox and libc).Suddenly this small program could not connect the windows and linux,then I running "netsta -a" found 3 socket's state is CLOSE_WAIT and PID is NULL.So I try to modify “net.ipv4.tcp_keepalive_×” but because busybox has only the basic functions that I could not using /etc/rc.d/init.d/network restart makes the modify take effect.
So I want to know:
how to make the change take effect with Busybox?
how I using socket can avoid the CLOSE_WAIT problem?

How do I remove a CLOSE_WAIT connection that doesn't belong to any tasks?
As we've established that the process is still running, it does belong to a task. We've also established that the netstat output was a complete red herring.
All you have to do is close the socket. You probably forget to close it after you got the connection failure. It's just a common or garden file/socket descriptor leak.

You might want to check out: https://github.com/rghose/kill-close-wait-connections
What this script does is send out the ACK which the connection was waiting for.
This is what worked for me.

Related

How to avoid CLOSE_WAIT in TCP/ip socket

By using select function handling multiple client connection in RPI using c. If CLOSE_WAIT came code is not working.if it occurs I am trying to kill the socket but code is hanging. Without restart how to resolve this
Please help in this.....
When CLOSE_WAIT will come how to avoid this.
I am thinking it will come if we didn't close the socket but how to close during network loss. Or else How to do read timeout on server side
Call shutdown(fd, 2) before close(fd), otherwise your socket will go into CLOSE_WAIT state to wait for the FIN handshake. This can take quite a while. I believe it is four minutes on Solaris, for example.
If you have a newer unix, you can use SHUT_RDWR instead of 2.

Server continues to function in Client/Server C application even after closing sockets

I am making a Tic Tac Toe application, with client/server. When pressing CTRL+C I have a custom handler that closes the sockets (the listening one as well), and then exits. However, if I try to run the program again, sometimes it gives an error that the port is used so it cannot bind, which lasts from several minutes (or until restart), to some seconds and sometimes it doesn't happen at all. I suspect this is a normal process, however I would like a second opinion. Also, any suggestions on how to avoid this would be appreciated!
EDIT: forgot to mention that, again SOMETIMES, killing the terminal works.
There is a connection (identified by IP address and port number) still in TIME_WAIT state. This state exists because there might be some IP packages around in the net that were sent to this connection. You probably get an error EADDRINUSE when you try to bind the socket.
You can check this with netstat.
Read about socket option SO_REUSEADDR and SO_REUSEPORT.
related:
Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?

How do I not destroy an UDP port when binding a socket?

I am using GCDAsyncUdpSocket to open an UDP socket and then I bind it to a port. The class is just a wrapper around the usual POSIX socket calls like socket, bind, etc.
SCENARIO 1:
MyMacBook: start process A
MyMacBook: open UDP port 23141 => SUCCESS
SomeComputer: send an UDP packet to MyMacBook, port 23141
MyMacBook: the standard OSX firewall asks me if i want to allow incoming network connections, and I agree.
MyMacBook: ignore packet, don't read it. (or at least, i don't see a log message that tells me that i got a packet. either my code is broken, or the CocoaAsyncSocket code is broken, or the OS didn't report the packet to my program.)
MyMacBook: kill process A
MyMacBook: start process A
MyMacBook: open UDP port 23141 => FAIL: Error Domain=NSPOSIXErrorDomain Code=48 "Address already in use" UserInfo=0x100407f30 {NSLocalizedDescription=Address already in use, NSLocalizedFailureReason=Error in bind() function
Why???
netstat -n |grep 2314
udp4 626 0 *.23146 *.*
udp4 1251 0 *.23141 *.*
^^this is how a broken UDP port looks like on the shell. If I ever want to use that port number again, I seem to have to restart my machine :-(
And no, I don't have old processes hanging around that block the port. I checked with ps aux and with lsof -i | grep UDP.
SCENARIO 2:
MyMacBook: start process A
MyMacBook: open UDP port 23143 => SUCCESS
MyMacBook: kill process A
MyMacBook: start process A
MyMacBook: open UDP port 23143 => SUCCESS
MyMacBook: kill process A
MyMacBook: start process A
MyMacBook: open UDP port 23143 => SUCCESS
MyMacBook: kill process A
...
If the port is never used, the system doesn't care if I dont close it nicely. This is how it should be.
My question:
What is wrong here? Of course, in a perfect world, a program wouldn't crash, and sockets are all closed with the POSIX close function. In an imperfect world, I just type Cmd-. in XCode to kill the app I am developing, and close isn't called.
When I am trying to bind my socket to an UDP port, what I am really trying to say to the OS is this: "Please OSX 10.8.5, bind my socket to port 23141. If some other program has it opened currently and is listening, then you may tell me that the port is in use, but if no running program cares about this port, then let me bind it to port 23141!!" Is this an OSX bug? Is it new? Is it a documented known bug, or a so-called "feature"?
This seems to be a bug in OSX.
Related posts:
https://superuser.com/questions/504750/kill-udp-port-that-has-no-process
https://apple.stackexchange.com/questions/71300/how-can-i-unbind-a-udp-port-that-has-no-entry-in-lsof
(#Barmar: thanks for finding these articles)
1) I have noted, that if a UDP port is broken (it is bound, but not bound to a particular process), then you have to restart the computer to use this port again. Logging off, and logging in again doesn't work.
2) I found out, that there is no problem if you disable the OSX Firewall. This means that the problem is a bug in the standard OSX firewall. However, a broken port doen't get unbroken if you disable the firewall, you still have to restart your computer to unbreak it. But: if the firewall is off, no port becomes broken.
There is something we can learn from part 2: the OSX firewall is not a simple packet filter. It seems to hack the socket commands on a kernel level.
Maybe someone wants to write a bug report and send it to Apple... (sounds like a joke, doesn't it?)

Forwarding an established TCP connection to another process on another port?

On a Linux machine, you have a daemon that listens on TCP port A. However, it is usually stopped because it is rarely used and takes away a large amount of system resources. Instead, I want to do something like this:
Code an application that listens on port B and does the following as soon as a connection is established: If the daemon is stopped, start it and wait until it listens on port A. Now the difficult part: Connect the client to the daemon in a completely transparent way, i.e. without the client having to reconnect on port A. Also, but this is irrelevant for this question, the application will shut down the daemon when there are no connections for a certain amount of time.
Of course, I could have my application connect to the daemon and pipe all communication. I do not want that. I want some way to forward the established connection to the daemon and then get rid of the connected socket, while the client is now happily connected with the daemon. In some way, I want to give the daemon's process my already connected socket. Is there any way to do something like this?
I'm running Debian, if that's important. I would want to code the application in C/C++, and it's okay to have OS-specific solutions (i.e. use syscalls). Forgive me though, I am not much of a Linux coder, so I am not very familiar with Linux system programming. If there is some obvious way to do it, I simply didn't know.
Of course, I am open for any kind of suggestion.
This problem has a pre-existing standard solution, generically known as inetd. It has been around for a long time, first in Unix systems and then Linux.
The more modern implementation is xinetd

Bind failed: Address already in use

I am attempting to bind a socket to a port below:
if( bind(socket_desc,(struct sockaddr *) &server, sizeof(server)) < 0)
{
perror("bind failed. Error");
return 1;
}
puts("bind done");
But it gives:
$ ./serve
Socket created
bind failed. Error: Address already in use
Why does this error occur?
Everyone is correct. However, if you're also busy testing your code your own application might still "own" the socket if it starts and stops relatively quickly. Try SO_REUSEADDR as a socket option:
What exactly does SO_REUSEADDR do?
This socket option tells the kernel that even if this port is busy (in
the TIME_WAIT state), go ahead and reuse it anyway. If it is busy,
but with another state, you will still get an address already in use
error. It is useful if your server has been shut down, and then
restarted right away while sockets are still active on its port. You
should be aware that if any unexpected data comes in, it may confuse
your server, but while this is possible, it is not likely.
It has been pointed out that "A socket is a 5 tuple (proto, local
addr, local port, remote addr, remote port). SO_REUSEADDR just says
that you can reuse local addresses. The 5 tuple still must be
unique!" by Michael Hunter (mphunter#qnx.com). This is true, and this
is why it is very unlikely that unexpected data will ever be seen by
your server. The danger is that such a 5 tuple is still floating
around on the net, and while it is bouncing around, a new connection
from the same client, on the same system, happens to get the same
remote port. This is explained by Richard Stevens in ``2.7 Please
explain the TIME_WAIT state.''.
You have a process that is already using that port. netstat -tulpn will enable one to find the process ID of that is using a particular port.
Address already in use means that the port you are trying to allocate for your current execution is already occupied/allocated to some other process.
If you are a developer and if you are working on an application which require lots of testing, you might have an instance of your same application running in background (may be you forgot to stop it properly)
So if you encounter this error, just see which application/process is using the port.
In linux try using netstat -tulpn. This command will list down a process list with all running processes.
Check if an application is using your port. If that application or process is another important one then you might want to use another port which is not used by any process/application.
Anyway you can stop the process which uses your port and let your application take it.
If you are in linux environment try,
Use netstat -tulpn to display the processes
kill <pid> This will terminate the process
If you are using windows,
Use netstat -a -o -n to check for the port usages
Use taskkill /F /PID <pid> to kill that process
The error usually means that the port you are trying to open is being already used by another application. Try using netstat to see which ports are open and then use an available port.
Also check if you are binding to the right ip address (I am assuming it would be localhost)
if address is already in use can you just want to kill whoso ever process is using the port, you can use
lsof -ti:PortNumberGoesHere | xargs kill -9
source and inspiration this.
PS: Could not use netstat because it not installed already.
As mentioned above the port is in use already.
This could be due to several reasons
some other application is already using it.
The port is in close_wait state when your program is waiting for the other end to close the program.refer (https://unix.stackexchange.com/questions/10106/orphaned-connections-in-close-wait-state).
The program might be in time_wait state. you can wait or use socket option SO_REUSEADDR as mentioned in another post.
Do netstat -a | grep <portno> to check the port state.
It also happens when you have not give enough permissions(read and write) to your sock file!
Just add expected permission to your sock contained folder and your sock file:
chmod ug+rw /path/to/your/
chmod ug+rw /path/to/your/file.sock
Then have fun!
I was also facing that problem, but I resolved it.
Make sure that both the programs for client-side and server-side are on different projects in your IDE, in my case NetBeans. Then assuming you're using localhost, I recommend you to implement both the programs as two different projects.
To terminate all node processes:
killall -9 node
First of check which port are listening,
netstat -tlpn
then select available port to conect,
sudo netstat -tlpn | grep ':port'
Fix it into also to your server and clients interfaces. Go Barrier tab -> change settings, -> port value type -> save/ok
Check both clients and server have similar port values
Then Reload.
Now it should be ok.
Check for running process pid:
pidof <process-name>
Kill processes:
sudo kill -9 process_id_1 process_id_2 process_id_3

Resources