failed to bind socket

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.
What is the declaration for SvrAddress?
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
What error are you getting? (errno)
the result from bind is -1, if it's what u'r asking for.
No, I'm asking for the value of errno.

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
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?
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.
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.
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 ?
Learn something new every day :o)
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.