<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>How to write safe UDP socket programs?</title>
		<link>http://www.allegro.cc/forums/view/525936</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 06 Sep 2005 13:55:32 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have heard that TCP network sockets were slower than UDP and that you had to use UDP for network broadcasts, so I thought about switching to UDP in my network library.</p><p>Problem is, that UDP seems to have no guarantee that a message is really sent. So I wanted to ask you if you knew a system to guarantee that complete messages are sent and received.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Faerber)</author>
		<pubDate>Mon, 05 Sep 2005 13:11:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>you write your own wrapper protocol around UDP.<br />Perhaps <a href="http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingd.shtml">this</a> helps a little.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Mon, 05 Sep 2005 13:20:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for your link, but desperately, the headline says it all: Low level Nonsense and Network <b>Theory</b>! I would be happy about a code snippet implementing an existant protocol wrapper.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Faerber)</author>
		<pubDate>Mon, 05 Sep 2005 13:32:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>can&#39;t you use both tcp/udp at the same time - so tcp for the important packages and udp for the rest<br />btw: you know how tcp works then take a look at beejs guide and adjust your code - i think udp isn&#39;t that different
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Mon, 05 Sep 2005 13:51:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Guess what I found after googling for &quot;reliable udp code&quot;&#39;<br /><a href="http://www.codeproject.com/internet/udt.asp">A UDP-based Reliable Data Transfer Library</a></p><p>Well, technically I didn&#39;t find it via google, the third link it gave was broken but searching reliable udp from codeproject gave that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Mon, 05 Sep 2005 13:57:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Frank: My network library&#39;s design is much more like UDP (although it uses TCP), so I think it&#39;s just logic that I will use UDP from now. But I could make abstract classes, which would allow me to use both systems.</p><p>HoHo: Thanks for this link, I&#39;ve downloaded the source and will have a further look at it!</p><p>EDIT: I looked at the source code and it seems to take some time to understand it. Does nobody have a small source file?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Faerber)</author>
		<pubDate>Mon, 05 Sep 2005 14:08:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>HawkNL, has reliable (TCP), unreleiable(UDP) ,and something called reliable packets (UDP with reliablity code).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (A J)</author>
		<pubDate>Mon, 05 Sep 2005 15:15:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>RakNet uses UDP with reliablity code.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Mon, 05 Sep 2005 15:40:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I know of these libraries, but the basic question (the question of life <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /> ) is: How can the peers (clients and servers) know, whether packets have been sent correctly or not? Does nobody know this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Faerber)</author>
		<pubDate>Mon, 05 Sep 2005 16:18:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
How can the peers (clients and servers) know, whether packets have been sent correctly or not?
</p></div></div><p>
With UDP they can&#39;t.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Mon, 05 Sep 2005 16:25:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What? Before, you said I should write a wrapper protocol around UDP to guarantee that packages are sent completely. To send complete packages, I must know whether the package has arrived. So if I don&#39;t know if a package has been received, guaranteed package sending is impossible!! <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Faerber)</author>
		<pubDate>Mon, 05 Sep 2005 16:37:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>example:</p><p>You put and ID(serial number) into each packet. <br />Reciever reads all the packets and if in one second a packet with ID X is missing it requests it from server again. <br />If a response is not recieved in one second another request is sent.</p><p>[edit]<br />If such system is done you can call it your reliable wrapper around UDP
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Mon, 05 Sep 2005 16:48:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>and that&#39;s why i think it&#39;s a good idea to use tcp (at least) too.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Mon, 05 Sep 2005 17:29:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>But what advantages has TCP over UDP (when you have successfully implemented an UDP wrapper)?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Faerber)</author>
		<pubDate>Mon, 05 Sep 2005 17:34:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>it is reliable...<br />if a player moves and this packet is lost - no problem (UDP)<br />if a player shoots a bullet - this packet must arrive (TCP)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Mon, 05 Sep 2005 18:08:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>But UDP has much shorter latency <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>[url <a href="http://www.flipcode.com/articles/network_part07.shtml">http://www.flipcode.com/articles/network_part07.shtml</a>]<br />Network Game Programming - Issue 07 - I bent my Wookie...[/url]
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
TCP vs. UDP (continued)</p><p>TCP is a simple and effective way of transmitting data. For making sure that your client and server can talk to each other it is very good. However, it carries with it a lot of overhead and extra network lag so once you feel you are ready you should make the jump to UDP. The big problems you will face with UDP include:</p><p>  # You won&#39;t have an individual socket for each client. In fact FD_ACCEPT will never be called. There won&#39;t be any clear notice that the client now sending you data wants to join the game unless you make the client say as much.<br />  # Given that clients don&#39;t need to open a unique socket in order to transmit data there is the very real possibility that a client who is not logged into the game will start sending all kinds of garbage to your server in some kind of attack. It becomes much more difficult to stop them at this point.<br />  # Likewise, you won&#39;t have a clear disconnect/leave game message unless you write one yourself.<br />  # Some data may not reach the other machine, so you may have to send important stuff many times.<br />  # Some data may arrive in the wrong order. Imagine that you got snapshot 5 before snapshot 4. Snapshot 4 would have to be suppressed (rejected). </p><p>While <b>switching to UDP will get you a signifigant speed boost</b>, there are a few more tricks you can try.
</p></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Mon, 05 Sep 2005 19:50:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use enet (I tried and recommend it) or raknet (heard it was good) ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Mon, 05 Sep 2005 20:40:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It seems like this is coming up a lot lately.  </p><p>UDP and TCP have the same latency, if you adjust the TCP settings for low latency.  If you use default settings, TCP can have up to twice the latency of UDP.  Note that this comparison assumes the network has no packet loss... if there&#39;s packet loss the comparison is more complicated, and TCP may fare worse.  </p><p>TCP has NO advantage over UDP + a good wrapper, except that TCP is a standard.  TCP is more or less THE standard implementation of UDP + wrapper.  The advantage of reimplementing the wrapper yourself is that you get to optimize the wrapper for your particular purposes, or concievably even do a better job than the TCP designers did.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (orz)</author>
		<pubDate>Mon, 05 Sep 2005 20:42:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="http://www.gamasutra.com/features/20030710/hargreaves_pfv.htm">Shawn Hargreaves had to deal with this problem too</a> <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (amarillion)</author>
		<pubDate>Mon, 05 Sep 2005 22:24:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>How to adjust settings for TCP to have lower latency?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Faerber)</author>
		<pubDate>Mon, 05 Sep 2005 22:33:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Some notes:<br />AJ: NL_RELIABLE_PACKETS works over TCP.  There is no reliable-UDP implementation in HawkNL.  What NL_RELIABLE_PACKETS does in HawkNL is to simulate discrete packets as you see in UDP but in TCP.  Since TCP is a stream, you can&#39;t be guaranteed that one send matches one receive.  NL_RELIABLE_PACKETS puts packet boundaries into the stream to break up the stream as it arrives to make sure you get the &quot;packets&quot; as you sent them.</p><p>orz is right in his implication that any general wrapper for reliable UDP is not beneficial.  TCP guarantees delivery and ordering, and it does this with a lossy datagram-oriented protocol (IP).  UDP is not much more than adding the concept of ports to IP and is pretty much raw IP, which TCP is built upon.</p><p>The benefit to using UDP reliablity layers is if you want some but not all of the features given by TCP:
</p><ul><li><p>Reliable data transmission</p></li><li><p>Stream-based transmission</p></li><li><p>ordered data transmission</p></li><li><p>socket multiplexing through the same port (good for firewalls)</p></li></ul><p>&lt;/li&gt;<br />So, for example if you want reliable, packeted, and unordered data transmission, it should not be hard to &quot;beat&quot; TCP by using UDP with a reliability layer.</p><p>If you are unconditionally implementing all of the five features above with a reliable UDP layer, then there won&#39;t be an advantage over TCP except that the TCP layer in your OS is probably more tested than your code <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Tue, 06 Sep 2005 02:03:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Adjusting TCP for low-latency stuff:  </p><p>The most important thing is to disable packet-combining (aka Nagle algorithm).  If you don&#39;t do this, it wont send any data until either all previous packets have been acknoledged or the send buffer contains a lot of data.  
</p><div class="source-code snippet"><div class="inner"><pre>  <span class="k1">int</span> tmp <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span>
  setsockopt <span class="k2">(</span> sock, IPPROTO_TCP, TCP_NODELAY, <span class="k2">(</span><span class="k1">const</span> <span class="k1">char</span> <span class="k3">*</span><span class="k2">)</span><span class="k3">&amp;</span>tmp, <span class="k1">sizeof</span><span class="k2">(</span>tmp<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

I think it&#39;s also a good idea to reduce the size of the send buffer if your programs bandwidth usage varies based upon how much is available.  If you&#39;re writting a game that uses a fixed amount of bandwidth then this is not useful.  It&#39;s done like this:
</p><div class="source-code snippet"><div class="inner"><pre>  tmp <span class="k3">=</span> <span class="n">2048</span><span class="k2">;</span>
  setsockopt <span class="k2">(</span> sock, SOL_SOCKET, SO_SNDBUF, <span class="k2">(</span><span class="k1">const</span> <span class="k1">char</span> <span class="k3">*</span><span class="k2">)</span><span class="k3">&amp;</span>tmp, <span class="k1">sizeof</span><span class="k2">(</span>tmp<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

On some operating systems it may also be a good idea to adjust &quot;low water thresholds&quot;, but I&#39;m not sure about that, they can&#39;t be adjusted on windows, and I think they default to good values on linux, so it&#39;s pretty much irrelevant.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (orz)</author>
		<pubDate>Tue, 06 Sep 2005 05:56:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>orz, many thanks for this function! This will probably save me a lot of time!</p><p>I just have one question: If I disable the Nagle algorithm, I probably won&#39;t have to reduce the send buffer, because I send everything immediately anyway, don&#39;t I?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Faerber)</author>
		<pubDate>Tue, 06 Sep 2005 13:39:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If the Nagle algorithm is disabled, it won&#39;t delay anything for the purpose of improving bandwidth efficiency.  But it will still delay things for the purpose of throttling the connection down to the available bandwidth.  </p><p>i.e. If you send some data, and the send buffers are small, then the socket will then be not-ready-to-send, as in, if you select with only that socket in the write set, and no read or except set, then select will block if there&#39;s a timeout or return 0 if there&#39;s not, and remove that socket from the set.  This is the desired behavior in low-latency variable-bandwidth games.  <br />If you send some data, and the send buffers are large, the socket will still be ready-to-send, as in, if you select with that socket in the write set then select will not block and will return a positive number and leave that socket in the set.  </p><p>If you are confident that available bandwidth is large compared to used bandwidth, you don&#39;t need to worry about that.  If you have no ability to vary the bandwidth usage for your game, then there&#39;s not much point in worrying about it, any clients without enough bandwidth are doomed anyway.  But if your games uses variable bandwidth, you need to know when to stop sending, otherwise the buffers will fill up with older data that may be optional anyway, and newer data that&#39;s mandatory will get delayed waiting for the older data to finish.  </p><p>Or at least, that&#39;s my understanding.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (orz)</author>
		<pubDate>Tue, 06 Sep 2005 13:55:32 +0000</pubDate>
	</item>
</rss>
