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?
gobernador
5,5913 gold badges31 silver badges51 bronze badges
asked Mar 4, 2013 at 9:59
6
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.».
answered Mar 4, 2013 at 10:09
JoeJoe
7,1684 gold badges37 silver badges52 bronze badges
3
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.
answered Mar 4, 2013 at 10:03
Ed HealEd Heal
58.6k17 gold badges87 silver badges125 bronze badges
2
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
answered Jun 1, 2018 at 5:22
primeprime
13.9k13 gold badges92 silver badges127 bronze badges
1
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)
Will Vousden
32.1k9 gold badges84 silver badges94 bronze badges
answered Mar 4, 2013 at 10:03
TechmonkTechmonk
1,45912 silver badges20 bronze badges
3
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.
answered Jun 21, 2021 at 9:01
best wishesbest wishes
5,3161 gold badge32 silver badges57 bronze badges
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 optionSO_REUSEADDR
as mentioned in another post.
Do netstat -a | grep <portno>
to check the port state.
answered Mar 4, 2013 at 10:15
PradheepPradheep
3,42325 silver badges35 bronze badges
4
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!
answered Oct 26, 2019 at 19:56
AbdolHoseinAbdolHosein
5084 silver badges15 bronze badges
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.
answered Apr 1, 2018 at 8:30
To terminate all node processes:
killall -9 node
answered Jan 24, 2022 at 0:45
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.
answered Jun 9, 2022 at 10:43
Check for running process pid:
pidof <process-name>
Kill processes:
sudo kill -9 process_id_1 process_id_2 process_id_3
answered May 16, 2022 at 20:41
0
1
Добрый вечер.
Не пойму, почему моя программа не может получить доступ к UDP порту. В лог-файлах пишет следующее:
Jun 18 18:01:17 myserver mydaemon0[3904]: bind error for udp error string: ‘Address already in use’
Jun 18 18:01:17 myserver mydaemon0[3904]: not binding to 4005 port
смотрю, что выдаёт netstat:
root@myserver:~# netstat -atulpn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1754/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2495/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 3130/exim4
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 2749/mysqld
tcp 0 0 0.0.0.0:37322 0.0.0.0:* LISTEN 1807/rpc.statd
tcp 0 0 192.168.0.99:22 192.168.0.130:54276 ESTABLISHED 2780/0
tcp6 0 0 :::111 :::* LISTEN 1754/rpcbind
tcp6 0 0 :::8080 :::* LISTEN 2249/apache2
tcp6 0 0 :::80 :::* LISTEN 2249/apache2
tcp6 0 0 :::22 :::* LISTEN 2495/sshd
tcp6 0 0 ::1:25 :::* LISTEN 3130/exim4
tcp6 0 0 :::44730 :::* LISTEN 1807/rpc.statd
tcp6 0 0 192.168.0.99:80 192.168.0.130:44882 TIME_WAIT —
tcp6 0 0 192.168.0.99:80 192.168.0.130:44881 TIME_WAIT —
tcp6 0 0 192.168.0.99:80 192.168.0.130:44883 ESTABLISHED 2899/apache2
udp 0 0 0.0.0.0:68 0.0.0.0:* 1850/dhclient
udp 0 0 0.0.0.0:68 0.0.0.0:* 1848/dhclient
udp 0 0 0.0.0.0:111 0.0.0.0:* 1754/rpcbind
udp 0 0 0.0.0.0:657 0.0.0.0:* 1754/rpcbind
udp 0 0 0.0.0.0:65209 0.0.0.0:* 1848/dhclient
udp 0 0 127.0.0.1:711 0.0.0.0:* 1807/rpc.statd
udp 0 0 0.0.0.0:43207 0.0.0.0:* 1850/dhclient
udp 0 0 0.0.0.0:49897 0.0.0.0:* 1807/rpc.statd
udp6 0 0 :::32813 :::* 1807/rpc.statd
udp6 0 0 :::111 :::* 1754/rpcbind
udp6 0 0 :::657 :::* 1754/rpcbind
udp6 0 0 :::65209 :::* 1850/dhclient
udp6 0 0 :::11527 :::* 1848/dhclient
и вижу, что нигде мой порт не встречается.
правда, в выводе netstata я не вижу записей с нужного мне интерфейса — на сервере их 3:
root@myserver:~# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default readynas.netgea 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.10.0 * 255.255.255.0 U 0 0 0 eth5
192.168.11.0 * 255.255.255.0 U 0 0 0 eth6
Я пытаюсь открыть UDP порты 4005 на 192.168.10.0 и на 192.168.11.0 и получаю ‘Address already in use’
Подскажите, пожалуйста, в чём может быть проблема?
- Remove From My Forums
-
Question
-
using System; using System.Net; using System.Net.Sockets; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int port = 11000; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint ipendpoint = new IPEndPoint(IPAddress.Any, port); try { socket.Bind(ipendpoint); Console.WriteLine("Successfully bound to port " + port); } catch (SocketException e) { Console.WriteLine("Winsock error: " + e.ErrorCode.ToString() + ", " + e.Message.ToString()); } Console.Read(); } } }
I get the error code [b]10022[/b] when trying to bind the UDP socket, which is «Invalid argument specified». This is the same for all the UDP server examples on the internet at the binding stage, as well as the MSDN one. It will work if the port
is 0, but not if the port is anything but 0. TCP binding works fine, and I can make a UDP server on any port with Java (the binding works fine) but in C# it gives me this error.The code works for everyone I’ve asked to try it, and I’ve got the latest NET framework (version 4) and Windows XP with SP3 so I know the commands are supported by the OS (also, Visual C# 2010 Express and 2008 Express both give this error).
Any ideas to fix it? All help welcome.
-
Moved by
Tuesday, August 31, 2010 12:56 AM
(From:Visual C# General)
-
Moved by
Answers
-
[SOLVED]
Installed Windows 7 Ultimate, problem was solved.
-
Marked as answer by
Anon17
Saturday, September 4, 2010 3:39 PM
-
Marked as answer by