Advertisement

UDP listening socket?

Started by July 16, 2005 08:59 AM
14 comments, last by hplus0603 19 years, 7 months ago
Quote:
Original post by hplus0603
You can't bind() a UDP socket.


Eh? How do you tell the socket what port to listen on?

-=[ Megahertz ]=-

-=[Megahertz]=-
Quote:
Original post by hplus0603
You can't bind() a UDP socket.

O_________________________________________________O

...or?
Advertisement
Really.... if you only want to send data over UDP you don't need to bind the socket.

refering to the question every udp socket you bind is listening, you don't need to listen() over it. If you need to accept only N connections you must use tcp or set up a counter every time a client connects.
Quote:
Original post by hplus0603
You can't bind() a UDP socket.


Not sure how it works in *nix world, but in Windows world you CAN and SHOULD bind() a UDP socket before using it. Implicit binding is done when you call sendto() or WSASendTo(), but calling recvfrom() or WSARecvFrom() will fail if you haven't bound your socket first. As for me, I typically don't leave it up to chance and always explicitly bind() a udp socket before doing any kind of operation on it.

Robert
Quote:
Original post by rmsimpson
Quote:
Original post by hplus0603
You can't bind() a UDP socket.


Not sure how it works in *nix world, but in Windows world you CAN and SHOULD bind() a UDP socket before using it.


In the linux world, you CAN and MAY bind() a UDP socket before using it. sendto()/sendmsg() work independantly of binding. As for recieving:
Quote:
From panda@industry:~$ man 7 udp :
                              ... In order to receive packets the       socket can be bound to an local  address  first  by  using       bind(2).   Otherwise  the  socket layer will automatically       assign a free local port  out  of  the  range  defined  by       net.ipv4.ip_local_port_range   and   bind  the  socket  to       INADDR_ANY.

Of course, manually binding will make it easier to port *nix socket code to WS2, if indeed it works as you say it does (which I have no reason to doubt ^_^).
Sorry, that was a brain fart. You can't listen() on a UDP port. Of course you can, and should, bind() it :-)
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement