LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2004, 10:24 PM   #1
pantera
Member
 
Registered: May 2004
Posts: 80

Rep: Reputation: 15
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
 
Old 09-29-2004, 12:04 AM   #2
JoshFed
LQ Newbie
 
Registered: Apr 2004
Location: Atlanta, GA, USA
Distribution: Slackware 9.1, 10 Gentoo
Posts: 24

Rep: Reputation: 15
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.
 
Old 09-29-2004, 12:27 AM   #3
pantera
Member
 
Registered: May 2004
Posts: 80

Original Poster
Rep: Reputation: 15
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
 
Old 09-29-2004, 05:03 AM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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.
 
Old 09-29-2004, 05:04 AM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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...
 
Old 09-29-2004, 04:01 PM   #6
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
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.
 
Old 09-29-2004, 04:11 PM   #7
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
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.
 
Old 09-30-2004, 03:39 PM   #8
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me ... about Socket programming.. rajsun Programming 2 04-24-2005 04:50 PM
socket programming???? harbir Linux - Networking 2 07-05-2004 02:52 AM
want help in socket programming valib4u *BSD 2 09-11-2003 11:17 PM
Socket Programming cxel91a Programming 4 03-19-2003 10:05 AM
socket programming herambshembekar Programming 3 04-13-2002 03:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration