|
13 Jun 2003, 16:30
|
#1
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Network programming: UDP Clients
Do clients that use UDP also have to bind their socket for listening? Or can you receive UDP data without binding the socket (like with TCP)?
|
|
|
13 Jun 2003, 16:36
|
#2
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
Code:
my $sock = new IO::Socket::INET->new(LocalPort=>5555,Proto=>'udp',LocalAddr=>'localhost');
while (1) {
my $data;
$sock->recv($data,128);
}
(in other words, yes a udp listener binds to a TSAP the same as a tcp listener does)
__________________
#linux : Home of Genius
<idimmu> ok i was chained to a desk with this oriental dude
|
|
|
13 Jun 2003, 16:46
|
#3
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
That's not really what I meant...
If you have a client that connects to a server does the client have to be a server to receive data?
When using TCP you use "connect" and a session is started, and you have a two way channel. Both client and server can receive and send data on the same socket.
But with UDP there isn't a "connect", so I was wondering if both parties had to explicitly listen on a certain port by binding the sockets.
|
|
|
13 Jun 2003, 17:06
|
#4
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
I was under the impression that to receive UDP packets you have to have some interface to the socket layer in your code; i.e. listen on a given port, so yeah it looks probable.
__________________
You're now playing ketchup
|
|
|
13 Jun 2003, 17:12
|
#5
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Another thing.... errr... can I somehow FORCE a socket to use a certain outgoing port when opening a socket?
Well... my problem is the following.
I'm making this client/server app, but I want to test it on one computer using the localhost address.
So, for both the clients and servers I bound a UDP socket to listen on a port NEAR 4444 (it can be 4444 to 4449).
However, my problem is the following: The server doesn't know on beforehand to which port the client is listening.
So, I thought, when I use the recv function, I can see what the incoming port is, and send it back to that port.... yeh.... well....
NO. The incoming ports are somewhere in the 23000 while I expected them to be 4444-4449.
So, what am I doing wrong? Why doesn't my client socket send it's stuff with the same port as it's listening on?
Code:
int mySocket = socket(AF_INET, protocol, 0);
// make sure nothing bad happened
if (mySocket == SOCKET_ERROR) {
printf("Error Opening Socket!\n");
return -1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (protocol == SOCK_STREAM)
{
if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
printf("Error connecting to server!\n");
}
}
else if (protocol == SOCK_DGRAM)
{
for (int i=0; i<5; i++)
{
if (bind(mySocket, (sockaddr*)&addr, sizeof(addr)) != SOCKET_ERROR)
break;
port++;
addr.sin_port = htons(port);
printf("Bind Failed!\n");
}
}
|
|
|
13 Jun 2003, 17:30
|
#6
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
If you bind() before connect()ing (or not connecting, if you're just gonna sendto(), but you might connect() anyway) you can choose your local port with sin_port (this works for outgoing and incoming, TCP and UDP).
|
|
|
13 Jun 2003, 17:36
|
#7
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Quote:
Originally posted by Structural Integrity
NO. The incoming ports are somewhere in the 23000 while I expected them to be 4444-4449.
....
|
If it helps,
4444=0x115C
0x5C11=23569
You use htons, but that code snippet is incomplete and out of context as far as I can see.
|
|
|
13 Jun 2003, 18:00
|
#8
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Quote:
Originally posted by queball
If it helps,
4444=0x115C
0x5C11=23569
You use htons, but that code snippet is incomplete and out of context as far as I can see.
|
You're the winner for solving the "odd port prob"...
So my problem is in either the clients receive part or the servers send part....
|
|
|
13 Jun 2003, 20:33
|
#9
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
OK... I severely messed up with casting some variables.
I have a callback function in my main app that took an unsigned long as parameter to identify the client. I use the same callback method for both UDP and TCP, so the client identification was a tad different.
However, I gave a 14 byte SOCKADDR struct to this function in the case of UDP so the last 10 bytes were dropped. I thought that a long was 8 byte, which would have been enough.
Oh well... solved now... I can send data in both directions with UDP!
\o/
[edit]
Editted the "bits" to "bytes", as pab pointed out
[/edit]
Last edited by Structural Integrity; 13 Jun 2003 at 21:06.
|
|
|
13 Jun 2003, 21:04
|
#10
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Quote:
Originally posted by pablissimo
Long == 32bits innit?
|
Sorry, I meant bytes.... =/
|
|
|
13 Jun 2003, 21:20
|
#11
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
I should have noticed that =(
__________________
You're now playing ketchup
|
|
|
13 Jun 2003, 23:00
|
#12
|
Gubbish
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
|
You can of course code directly for IP and answer UDP packets on any port, or a combination of ports, of your likeing.
If it's any consolation, I had a hard time grasping the source and destination ports of IP too, before I sat down and studied the ip rfc.
__________________
Gubble gubble gubble gubble
|
|
|
13 Jun 2003, 23:34
|
#13
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
PEAR network libraries rock my world \o/
I know it's PHP but so what :P
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
14 Jun 2003, 00:22
|
#14
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
I was wondering when W would show up :) IIRC he's the daddy of UDP IPC.
__________________
#linux : Home of Genius
<idimmu> ok i was chained to a desk with this oriental dude
|
|
|
|
All times are GMT +1. The time now is 07:34.
| |