Note that there are some explanatory texts on larger screens.

plurals
  1. POLost UDP 'connection' between client app and server app
    primarykey
    data
    text
    <p>I wrote two small applications (a client and a server) to test UDP communication and I found the 'connection' (yeah, I know, there's no real connection) between them gets lost frecuently for no reason.</p> <p>I know UDP is an unreliable protocol, but the problem here does not seem the losing of packets, but the losing of the communication channel between the apps.</p> <p>Here's client app code:</p> <pre class="lang-c# prettyprint-override"><code> class ClientProgram { static void Main(string[] args) { var localEP = new IPEndPoint(GetIPAddress(), 0); Socket sck = new UdpClient(localEP).Client; sck.Connect(new IPEndPoint(IPAddress.Parse("[SERVER_IP_ADDRESS]"), 10005)); Console.WriteLine("Press any key to request a connection to the server."); Console.ReadLine(); // This signals the server this clients wishes to receive data SendData(sck); while (true) { ReceiveData(sck); } } private static void ReceiveData(Socket sck) { byte[] buff = new byte[8]; int cnt = sck.Receive(buff); long ticks = BitConverter.ToInt64(buff, 0); Console.WriteLine(cnt + " bytes received: " + new DateTime(ticks).TimeOfDay.ToString()); } private static void SendData(Socket sck) { // Just some random data sck.Send(new byte[] { 99, 99, 99, 99 }); } private static IPAddress GetIPAddress() { IPHostEntry he = Dns.GetHostEntry(Dns.GetHostName()); if (he.AddressList.Length == 0) return null; return he.AddressList .Where(ip =&gt; ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork &amp;&amp; !IPAddress.IsLoopback(ip)) .FirstOrDefault(); } } </code></pre> <p>Here's the server app code:</p> <pre class="lang-c# prettyprint-override"><code> class ServerProgram { private static int SLEEP = 5; static void Main(string[] args) { // This is a static IP address var localEP = new IPEndPoint(GetIPAddress(), 10005); Socket sck = new UdpClient(localEP).Client; // When this methods returs, a client is ready to receive data var remoteEP = ReceiveData(sck); sck.Connect(remoteEP); while (true) { SendData(sck); System.Threading.Thread.Sleep( ServerProgram.SLEEP * 1000); } } private static EndPoint ReceiveData(Socket sck) { byte[] buff = new byte[8]; EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0); int cnt = sck.ReceiveFrom(buff, ref clientEP); Console.WriteLine(cnt + " bytes received from " + clientEP.ToString()); return (IPEndPoint)clientEP; } private static void SendData(Socket sck) { DateTime n = DateTime.Now; byte[] b = BitConverter.GetBytes(n.Ticks); Console.WriteLine("Sending " + b.Length + " bytes : " + n.TimeOfDay.ToString()); sck.Send(b); } private static IPAddress GetIPAddress() { // Same as client app... } } </code></pre> <p>(this is just test code, don't pay attention to the infinite loops or lack of data validation)</p> <p>The problem is after a few messages sent, the client stops receiving them. The server keeps sending, but the client gets stuck at <code>sck.Receive(buff)</code>. If I change SLEEP constant to a value higher than 5, the 'connection' gets lost after 3 or 4 messages most of the time.</p> <p>I can confirm the client machine doesn´t receive any packet when the connection is lost since I use Wireshark to monitor the communication.</p> <p>The server app runs on a server with direct connection to Internet, but the client is a machine in a local network, behind a router. None of them has a firewall running.</p> <p>Does anyone have a clue what could be happening here?</p> <p>Thank you!</p> <p>EDIT - Additional data:</p> <p>I tested the client app in several machines in the same network and the connection get lost always. I also tested the client app in other network behind other router and there is no problem there. My router is a Linksys RV042 and never had any problem with it, in fact, this app is the only one with problems.</p> <p><br/> <h3>PROBLEM SOLVED - SHORT ANSWER:</h3> It was a hardware problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload