DispatchSource.makeReadSource is holding onto socket after app is killed by Xcode [duplicate] - ios

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

Related

Checking for port exhaustion with Netstat HttpClientFactory WPF

I want to check that I have properly implemented HttpClientFactory. I have a desktop application that pings my server every 20 seconds. When I open command prompt and run "netstat -ano | findstr {My server IP}" I can see there are always 2 or 3 connections. As time goes on and I continue to check, the ports will slowly change (go up in their port #'s, older ports disappear) but there are never more than 2 or 3 connections. Does this mean that the old ports are being released and I am not at risk for port exhaustion? Thanks.
As mentioned above. I am going to begin selling my application very soon and need to be sure that I am not going to exhaust my client's ports and hinder their network.

Is there a way to find pid of a process of socket peer?

I have the following case:
Two applications (mine and 3rd party) on iOS need to communicate over TCP/IP
I can change the code of my app, but not the 3rd party app
The protocol can't be changed (because I can't change the 3rd party app).
In my app, I want to make sure that I talk to the correct app.
I know how I can get peer port with geetpeername
What I am looking for is a way to figure out the pid of the process which uses this port.
I found similar question: How do you determine the PID of a peer TCP connection on the same iOS device?
However, in his case, both ports are in the same app.
Disclaimer: I am fine with private API. It won't be sent to AppStore. However, I am looking for a solution for non a jailbroken phone.
Update 1
I said that I need PID, because there are known ways of getting association between PID and application bundle id (using sysctl).
Generally speaking, I don't care about PID per se. My concern is figure out what is the application on other side of the socket.
I don't have a solution for this, but if I was going to try to do it, I would proceed in the following way:
On a UNIX system, you can use the lsof command to determine which processes have which files open. This includes sockets, and lsof allows you to determine the pid of a process using a given port. For example, use
lsof -n -P -i :443
if you're trying to determine which process is using port 443 (HTTPS), which might yield (on OS X):
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
firefox 81615 myname 112u IPv4 0xffffff8017379a40 0t0 TCP 192.168.2.7:52521->74.125.28.103:443 (CLOSE_WAIT)
The open source version of lsof for Darwin, I believe, can be found here. So you might try starting with main.c, and using those command line arguments, navigate through the code until you find the system calls that are used.
It's certainly possible that the calls needed will not work when run in a process with user mobile privileges. But, maybe not? If there's a security check inside the lsof source itself, then you can certainly remove that, if you copy and paste the source yourself.
Anyway, it might be worth a try, if no one offers another answer.
Note: lsof is available for jailbroken phones, and I just tried running the current versions available from Cydia. They did not work for me, on 5.1.1 or 6.1.2. Not sure why. I assume that if they were on the repository, though, at some point, someone was able to port lsof to iOS.

how to restart a grails web application

how can I restart a grails application other than shutting it down by doing ctrl z and running it again.
when I do it that way it says
Error Server failed to start for port 8080: Address already in use
CTRL-Z doesn't shut down an application, sends a SIGTSTP signal to the process to suspend it... Suspended processes still have connections to their resources (ports, files, etc), so you cannot run another instance of grails on the same port whilst one is suspended.
To bring it back to the foreground, you can enter fg in the same shell (or if you want it to run in the background, you can use bg)
To shut it down, you need CTRL-C (or you can kill it as nickdos says in the comments)
To cleanly shut down a run-app or run-war, create an empty file named .kill-run-app in the top-level directory of your grails application (i.e. alongside grails-app, src, etc.). Grails will automatically delete this file once the application has been stopped, so don't be surprised when it disappears shortly after you created it.
Shutdown should not require process hacking and doesn't require a 'could ruin everything' disclaimer :)
Just run:
grails stop-app
If that still doesn't work:
lsof -i :8080 or ps aux | grep grails
Will show the process id, then first try killing it nicely:
kill 12345 (where 12345 is the processId)
Of if it really doesn't want to play nicely, then HAMMER it with:
kill -9 12345 (where 12345 is the processId)
Works every time (DISCLAIMER: Could ruin everything)

Cassandra Cluster Setup getting JMX error

I m trying setup a cassandra cluster as a test bed but gave the JMX remote connection error. I seem to found the answer for my error from cassandra FAQ page
Nodetool says "Connection refused to host: 127.0.1.1" for any remote host. What gives?
Nodetool relies on JMX, which in turn relies on RMI, which in turn sets up it's own listeners and connectors as needed on each end of the exchange. Normally all of this happens behind the scenes transparently, but incorrect name resolution for either the host connecting, or the one being connected to, can result in crossed wires and confusing exceptions.
If you are not using DNS, then make sure that your /etc/hosts files are accurate on both ends. If that fails try passing the -Djava.rmi.server.hostname=$IP option to the JVM at startup (where $IP is the address of the interface you can reach from the remote machine).
But can somebody help me on how to do -Djava.rmi.server.hostname=$IP
Or what to add is hosts file, i know that in hosts normally we add "IP Alias", but whose ip and alias.
I dont know much java or either linux
I m currently working on ubuntu v10.04 and cassandra v0.74
Sudesh
For JMX you need to enable JMX-remoting:
java -Dcom.sun.management.jmxremote
Depending on from where you want to access the jmx-server, you also need to specify a port:
-Dcom.sun.management.jmxremote.port=12345
and set or disable passwords.
Have a look at http://download.oracle.com/javase/1.5.0/docs/guide/management/agent.html for more details.

Investigating which Windows service is listening to which IP and port

I am investigating a production system where there are several Windows services communicating with each other through TCP/IP sockets. I'm trying to figure out which executable is listening to which IP address and which port on a given machine.
Other than rummaging through each windows service's obscure configuration files, is there a system tool that can more easily give me the details I want?
As already mentioned TCPView by SysInternals (i.e. Microsoft) is a great tool.
But on production systems you may not be allowed to install additional software, so I think you may want to try out netstat.exe, which is typically located at C:\WINNT\system32\netstat.exe .
A help page is available with
netstat -?
Examples are:
netstat -a
Lists all local TCP connections and listening ports together with remote TCP endpoint.
netstat -o
Adds the process ID to the output.
netstat -b
Gives you the name of the executable wich was involved in establishing this connection/port.
http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx
SysInternals TCPView is great
Give this a whirl.
netstat -abn
Command line netstat tool might help you. To learn available parameters run it with /?: netstat /?
Or there is a better GUI alternative: SysInternals TcpView (freely downloadable from ms site)
Thanks everyone. Very helpful indeed. A friend also introduced me to a freeware utility called "Active Ports" from DeviceLock: http://www.devicelock.com/freeware.html/

Resources