2

I have been recently writing a UDP server for a 2D shooter game I am making in C# and XNA for PC, which will update and send world data, entity data, chat data .etc when required.

A question recently sprinted into mind when I was creating a way for players to change their weapons; what would happen if in the occasion the packets send to the server requesting a weapon change were lost? This question then made me think of another question; how can I make a way for the client and server to acknowledge receiving certain packets or blocks of data?

So, I came up with the following simple looking solution to tackle this problem :

  1. Server sends packet(s) which require acknowledgement.
  2. Client receives the packet(s) and sends an acknowledgement packet.
  3. Server checks whether or not the client acknowledged the data. If the client has not, resend data.

The proposed solution looks pretty good, but what would happen if in the occasion that the acknowledgement packet gets lost or if anything unintended happens to it?

Would a better solution to this problem be to create a server and client which use TCP and UDP; where TCP is used for data that needs to get there and arrive in order / in one peace, and UDP for data that needs to get there fast and can cope with loss or error?

If a TCP/UDP server and client is a better choice, what are the risks and how would I go around to implement that?

Thanks.

seandewar5
  • 299
  • 1
  • 3
  • 10

1 Answers1

1

You need to be using TCP if you want to ensure packets are delivered. UDP does not guarantee delivery, but TCP has this built into the protocol.

Jonathan
  • 5,495
  • 4
  • 38
  • 53
  • Do you think the better solution would be to use the TCP/UDP server and client suggestion instead then? Thanks. – seandewar5 Feb 04 '12 at 13:53
  • @seanewar5 I am not sure you need to use both. I would just use TCP. If you are experiencing severe lag, then you could switch to using TCP for the "important" communication and UDP for the communication that is less important. – Jonathan Feb 04 '12 at 13:59
  • 1
    @seanewar5 take a look at this: http://stackoverflow.com/questions/1099672/when-is-it-appropriate-to-use-udp-instead-of-tcp – Jonathan Feb 04 '12 at 14:02
  • Okay, but I would like information such as the packets sent from shooting to appear asap; because of the process which assures reliability on TCP, UDP seems to be a faster choice. I don't care about all of my data arriving, I only care about certain data needing to, I don't mind loosing most data through UDP such as position updates; in the hope that they don't keep getting lost constantly. Thanks again. – seandewar5 Feb 04 '12 at 14:04
  • 1
    @seanewar5 Yes, you are correct that UDP is faster. I am just saying that I am not so sure TCP wouldn't be fast enough. I am leaning towards your original suggestion, though. – Jonathan Feb 04 '12 at 14:09
  • Okay, so do you think I should be using TCP, UDP or TCP/UDP? Is there any things I have to consider if I'm going to use TCP/UDP? Thanks again. – seandewar5 Feb 04 '12 at 14:21
  • 1
    @seanewar5 I think that the TCP/UDP combination would be a good fit for what you are doing. – Jonathan Feb 04 '12 at 14:58
  • @Jonathan When combining TCP and UDP, the use of TCP can actually induce UDP packet loss because fundamentally the packets go through the same pipe. See this paper for reference: http://www.isoc.org/INET97/proceedings/F3/F3_1.HTM – Andrew Wang Feb 05 '12 at 19:49
  • @AndrewWang Thanks for the great link. I would point out that the TCP in sean's application would not likely be occurring concurrently, consistently with the UDP communication. For instance, the user probably won't be shooting and changing weapons at the same time. – Jonathan Feb 05 '12 at 20:50