LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   socket programming (https://www.linuxquestions.org/questions/programming-9/socket-programming-236455/)

pantera 09-28-2004 10:24 PM

socket programming
 
i am using UDP sockets to communicate between client and server i am using c programming in linux.........my problem is that when i send the first message to the server it reaches the serverand the server sends a reply but when i send the same message again to the server it gives me "cannot bind the port" error........
is there any way to free the socket so that it can bind the thing again.....or is there any other way to do it

JoshFed 09-29-2004 12:04 AM

Is the socket being destroyed after the reply, somehow?

Does the program end and then you start it again right away? In school, I had to wait a minute or so before re-running the client so the server could free the port I was using.

pantera 09-29-2004 12:27 AM

the thing is i am trying to make the server and the client communicate both ways.....so after the client send the message to the server and after the server send reply to the client....if the client tries to send message to the server again it shows error

??: sending data to '192.168.0.8' (IP : 192.168.0.8)
??: sending data to '192.168.0.8' (IP : 192.168.0.8)
cannot bind port number 1501

CroMagnon 09-29-2004 05:03 AM

Why are you binding more than once? Your program should bind the socket once after the socket is created, and that's it - just keep checking it for new packets.

CroMagnon 09-29-2004 05:04 AM

Also, if your server and client are running on the same machine, they will not be able to use the same port number to receive data...

Nerox 09-29-2004 04:01 PM

The kernel takes a while in order to free the bound port, so if you do something like this:

Code:

LOOP{
...
bind(sd,(struct sockaddr *) &server, size);
...
}

It can fails when bind is called again due to the kernel delay to free the bound port, so try to bind your socket only once.

Nerox 09-29-2004 04:11 PM

You can only bind the same socket once, you must close it and create another one before bind it again, you can use the same variable. But remember the kernel delay, try to use the same bound socket in order to avoid two near bind calls.

AquamaN 09-30-2004 03:39 PM

Like Cromagnon and Nerox stated, you can and only need to bind a port to an address once. Once the port is bound, you can send and recieve until you want to close the connection and free up the port. That should fix your problem.


All times are GMT -5. The time now is 08:58 PM.