Sign in to follow this  
etsuja

UDP multiple sockets?

Recommended Posts

I'm just starting networking programming and need to know if a server using UDP needs multiple sockets for multiple connections? I know it doesn't really make a connection though but I'm just curious if you had to people trying to connect right at the same time or something.

Share this post


Link to post
Share on other sites
Nope, a single socket is fine. That's one of the benefits of UDP. On the other hand, as you pointed out, if you were to get 2 packets at the EXACT same time, chances are good the second packet would just be dropped.

Cheers!

Share this post


Link to post
Share on other sites
Quote:
if you were to get 2 packets at the EXACT same time, chances are good the second packet would just be dropped


This is just not true. First, you can't, actually, receive two packets at the EXACT same time, unless you have two network cards connected to two physical circuits -- and, in that case, the system can still receive two packets simultaneously just fine.

Even if your switch receives two packets for your computer at once, there's often sufficient queuing in the switch to deal with the full throughput of the uplinks.

Dropping typically happens when buffers and queues overflow. This typically is more likely to happen on a router upstream in the internet, than on your computer. For example, the ISP end of your DSL or cable connection will drop packets when too many are coming in (i e, you're trying to receive more data than your bandwidth can fit).

However, if you receive a flood of data (lots of packets), and fill up the input buffer in the kernel, without reading from the socket for a while, you'll start dropping packets. Thus, when using a single UDP socket where some operations may take a lot of time, either use a thread to poll the socket, or ioctl() to make the read buffer really big.

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603:
This is just not true. First, you can't, actually, receive two packets at the EXACT same time, unless you have two network cards connected to two physical circuits -- and, in that case, the system can still receive two packets simultaneously just fine.


Uhh, ok. I Guess I'm wrong. While I was just trying to illustrate that it was unlikely he'd receive two packets at the EXACT same time, I figured it was because the NIC wouldnt handle it, but it turns out it's just impossible to get two packets at the EXACT same time...My bad. Thanks hplus for correcting me.

And unless hplus says otherwise, no, you dont need to listen() for UDP sockets, that's only for stream based connections.

Cheers!

Share this post


Link to post
Share on other sites
thanks, I guess that answers the question to the new thread I started

EDIT: But if I don't listen() how do I know who to recieve data from with recvfrom()?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by etsuja
I'm just starting networking programming and need to know if a server using UDP needs multiple sockets for multiple connections? I know it doesn't really make a connection though but I'm just curious if you had to people trying to connect right at the same time or something.




You can have a single IN and a single OUT UDP port. All machines send all their traffic to/from the one server port set. What you do is have your own 'connection' ordinal imbedded in every packet and use it to route the data once a packet is received.

You could use a lazy evaluation by using a connection ordinal for a lookup into a connection index (on the server) and then validate that by comparing the address in the packet against the IP of the other computer (saved in your connection array element -- pointed to by the ordinal).

You now have to do your own connection establish/break mechanism (login/logout/etc..)

You now can route packets to each client connections packet queue -- or more than one queue if you have a message priority scheme (and/or reliable/unreliable messages).

Share this post


Link to post
Share on other sites
Quote:
You can have a single IN and a single OUT UDP port.


There is no advantage to having a separate OUT UDP port, and in fact one disadvantage: it will make your application much less compatible with NAT firewalls/routers.

Quote:
if I don't listen() how do I know who to recieve data from with recvfrom()


What does the man page for recvfrom() say? (Hint: there's a reason it's called recvfrom()).


Btw: jwalsh is right: you cannot listen() on a datagram socket.

Share this post


Link to post
Share on other sites
I have another problem gethostbyname() isn't working and I have no idea why.. here's what I'm doing just for testing

struct hostent *h;
h=gethostbyname("www.yahoo.com");

when I look at the values of h in the debugger there's nothing.

EDIT: nevermind, got it working.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this