A few questions about UDP sockets

Started by
2 comments, last by markr 10 years, 8 months ago

I've been working on fairly simple client-server setup for a game, using UPD (It's an arena shooter with lots of moving objects) and doing raw socket programming in C. I know how to create sockets, how to send, receive and process packets and whatnot, and at the other end I know what I want to send, who to and how often. However I've got a fairly substantial gap in my knowledge about the general layout I should be using:

Should I be using separate sockets on the server for each client, what are the advantages/disadvantages of this over a single socket for everyone?

Should I have separate inbound/outbound sockets? I believe this is a requirement for TCP, not sure how UDP deals with it though. I know using one socket for both in and out works, just not sure if it's a good way of doing things.

How do I allow any combination of clients and server using IPv4 and IPv6 to connect to each other?

Perhaps I'm just not searching for the right terms, but I'm having difficultly finding resources and this bit-in-the-middle sort of thing, so any help would be great!

Advertisement

>>> Should I be using separate sockets on the server for each client, what are the advantages/disadvantages of this over a single socket for everyone?

You can use a single socket for everyone. It probably makes things slightly easier.

>>> Should I have separate inbound/outbound sockets? I believe this is a requirement for TCP, not sure how UDP deals with it though. I know using one socket for both in and out works, just not sure if it's a good way of doing things.

Sockets are bidirectional. This includes UDP and TCP. Using the same socket for rx and tx is definitely a good idea (in fact, NAT really depends on this for UDP to work at all)

>>> How do I allow any combination of clients and server using IPv4 and IPv6 to connect to each other?

An IPv6 socket allows an IPv4 client to connect to it on a dual-stack host. It does this by using the ipv4-in-ipv6 addresses, i.e. ::ffff:1.2.3.4 addresses (at least on Linux). Port numbers are the same. Multicasting is a bit dubious, I recommend that you use separate sockets for ipv4 and ipv6 if you're multicasting.

However, it's not possible for an ipv4-only host to talk to an ipv6-only host, but this is unlikely to be a problem (yet) as ipv6-only hosts mostly only exist in test-labs.

---

In general I think you'll find making the game work properly is much more of a problem than low-level socket stuff. You should probably think about that.

Thank you, some very useful information there! Just one question:

An IPv6 socket allows an IPv4 client to connect to it on a dual-stack host. It does this by using the ipv4-in-ipv6 addresses, i.e. ::ffff:1.2.3.4 addresses (at least on Linux). Port numbers are the same. Multicasting is a bit dubious, I recommend that you use separate sockets for ipv4 and ipv6 if you're multicasting.

Dubious in what sense?

Thank you, some very useful information there! Just one question:

An IPv6 socket allows an IPv4 client to connect to it on a dual-stack host. It does this by using the ipv4-in-ipv6 addresses, i.e. ::ffff:1.2.3.4 addresses (at least on Linux). Port numbers are the same. Multicasting is a bit dubious, I recommend that you use separate sockets for ipv4 and ipv6 if you're multicasting.

Dubious in what sense?

If you use IPv6 sockets, and want to use IPv4 multicasting, it might be easier (particularly cross-platform) to use a separate socket for v4 and v6. Under Linux it is possible to do this, but only if you use an IPv4 setsockopt on the IPv6 socket. The (v6) socket can then get v4 multicasts, which is useful. But if you need it to work cross-platform then you probably can't guarantee this behaviour, and I'd strongly recommend using different sockets and different port numbers for v4 and v6 (different port numbers, so that your v6 socket won't receive any of your v4 datagrams)

Mark

This topic is closed to new replies.

Advertisement