Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind/connect multiple UDP socket

Tags:

c

sockets

udp

My initial UDP socket is binded to 127.0.0.1:9898.

The first time that I get notified of incoming data by epoll/kqueue, I do recvfrom() and I fill a struct sockaddr called peer_name that contain the peer informations (ip:port).

Then I create a new UPD socket using socket(),

then I bind() this newly created socket to the same ip:port (127.0.0.1:9898) than my original socket.

then I connect my newly created socket using connect() to the peer who just sent me something. I have the information in the struct sockaddr called peer_name.

I then add my newly created socket in my epoll/kqueue vector and wait for notification.

I would expect to ONLY receive UDP frame from the peer i'm ""connected to"".

1/ does netstat -a -p udp is suppose to show me the IP:PORT of the peer my newly created socket is ""connected to"" ?

2/ I'm probably doing something wrong since after creating my new socket, this socket receive all incoming UDP packets destinated to the IP:PORT I'm binded to, regardless of the source peer IP:PORT.

I would like to see a working example of what I'm trying to do :) or any hint on what I'm doing wrong.

thanks!

like image 804
nicboul Avatar asked Jun 03 '10 16:06

nicboul


People also ask

Can multiple UDP sockets bind to same port?

Yes, it is also possible to have multiple sockets using a single UDP port. With the caveat that only broadcast & multicast packets are going to be multiplexed, unicast packets will only be delivered to the first socket.

What does connect () do on a UDP socket?

The CONNECT command enables an application to associate a socket with the socket name of a peer. The socket then is considered to be a connected UDP socket. You can call the CONNECT command multiple times with different peer names to change the socket association.

Can multiple clients send data to the same UDP socket used by a server?

You could create a new UDP socket, bind() it to a different port, and then instruct the client to start sending traffic to the new socket's port instead of the usual one; but note that there isn't really any advantage to be gained by doing that.

Do you need to bind socket for UDP?

UDP server Traditionally on the server side UDP requires unconnected sockets. Using them requires a bit of finesse. To illustrate this, let's write an UDP echo server.


1 Answers

http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-5.html

"Does doing a connect() call affect the receive behaviourof the socket? Yes, in two ways. First, only datagrams from your "connected peer" are returned. All others arriving at your port are not delivered to you.

But most importantly, a UDP socket must be connected to receive ICMP errors. Pp. 748-749 of "TCP/IP Illustrated, Volume 2" give all the gory details on why this is so."

like image 133
javed Avatar answered Sep 19 '22 02:09

javed