failed to bind socket

Nov 4, 2010 at 4:32am
Hi,

I got a problem with my UDP server on linux. On windows everything's fine, and it's suposed to be the same on linux.

The problem is on this line bind(sckHandle, (struct sockaddr*)(SvrAddress), sizeof(sockaddr_in))

except that this line is ok. I found on a forum a problem similar and the dude answered that it was solved, it was a problem with his firewall settings on linux.

I searched to modify them with FireStarter but could not find where was the problem.

I hope you guys can help me, thanks in advance.
Nov 4, 2010 at 8:57am
What is the declaration for SvrAddress?
Nov 4, 2010 at 12:32pm
sockaddr_in* SvrAddress;

and i initialized it with

1
2
3
4
5
SvrAddress = new sockaddr_in;

SvrAddress->sin_family = AF_INET;
SvrAddress->sin_addr.s_addr = htonl(ip);
SvrAddress->sin_port htons(port);
Last edited on Nov 4, 2010 at 12:32pm
Nov 4, 2010 at 1:37pm
What error are you getting? (errno)
Nov 4, 2010 at 1:40pm
the result from bind is -1, if it's what u'r asking for.
Nov 4, 2010 at 4:43pm
No, I'm asking for the value of errno.

Nov 5, 2010 at 2:16am
Well i don't know 'cause i didn't manage to find how it works, but i found the error str "Address Already In Use" But who would use the port 7153 and the IP address is equivalent to INADDR_ANY
Nov 5, 2010 at 1:33pm
netstat -ap | grep 7153

will tell you which process.

Are you, by any chance opening two sockets and attempting to bind both to the same port?
Nov 5, 2010 at 1:43pm
It may be that the port is being held from a previous run of your program. Sometimes it takes time for it to be released. Don't know why.
Nov 5, 2010 at 2:17pm
Machine A does that because it wants to give the other side (machine B) time to realize that machine A is
no longer responding and close its side of the connection. Otherwise, if a different process starts on Machine A
and opens the same port, it will receive packets that it can't comprehend.

To turn off this behavior, you have to set the SO_REUSEADDR socket option.
Nov 5, 2010 at 2:42pm
Thanks for your precious help.

1. It was from a previous process wrong terminated. I guess CodeLite don't terminate the process it started when it crashes.

2. it is possible to check at Application startup if another process of the same name is open ?
Nov 5, 2010 at 2:43pm
Learn something new every day :o)
Nov 5, 2010 at 4:30pm
You have a couple of options.

One is to create a temporary file with a well-known name somewhere (say /tmp) at the beginning of your program.
If it fails to create the file because it already exists, then you know another instance is running.

Another is to create a well-known semaphore at the beginning of your program. If it fails to create the semaphore
because it already exists, then you know another instance is running.
Topic archived. No new replies allowed.