title: Confused about Sockets with UDP Protocol in C#
body: <p>I've just started learning Sockets through various Google searches but I'm having some problems figuring it out how to properly use Sockets in C# and I'm in the need of some help.</p>
  
  <p>I have a test application (Windows Forms) and on a different class (which is actually in it's own .dll, but that's irrelevant) I have all the server/client code for my sockets code.</p>
  
  <p><strong>Question 1)</strong></p>
  
  <p>On my test application, on the server part, the user can click the "start listening" button and the server part of my sockets application should start listening for connections on the specified address and port, so far so good.</p>
  
  <p>However, the application will be blocked and I can't do anything until someone connects to the server. What if no one connects? How should I handle that? I could specify a receive timeout but what then? It throws an exception, what can I do with that? What I would like is to have some sort of activity on the main application so the user knows the application didn't froze and is waiting for connections. But if a connection doesn't come, it should timeout and close everything.</p>
  
  <p>Maybe I should use asynchronous calls to send/receive methods but they seem confusing and I was not able to make it work, only synchronous work (I'll post my current code below).</p>
  
  <p><strong>Question 2)</strong></p>
  
  <p>Do I need to close anything when some send/receive call times out. As you'll see on my current code, I have a bunch of closes on the socket, but this doesn't feel right somehow. But it also doesn't feel right when an operation times out and I don't close the socket.</p>
  
  <p>In conclusion of my two questions.... I would like an application that doesn't block so the user knows the server is waiting for a connection (with a little marquee animation for instance). If a connection is never established after a period of time, I want to close everything that should be closed. When a connection is established or if it doesn't happen after a period of time, I would like to inform the main application of the result.</p>
  
  <p>Here's some of my code, the rest is similar. The <code>Packet</code> class is a custom class that represents my custom data unit, it's just a bunch of properties based on <code>enums</code> for now, with methods to convert them to bytes and back into properties.</p>
  
  <p>The function that starts to listen for connections is something like this:</p>
  
  <pre><code>public void StartListening(string address, int port) {
      try {
          byte[] bufferBytes = new byte[32];
  
          if(address.Equals("0.0.0.0")) {
              udpSocket.Bind(new IPEndPoint(IPAddress.Any, port));
          } else {
              udpSocket.Bind(new IPEndPoint(IPAddress.Parse(address), port));
          }
  
          remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
  
          int numBytesReceived = udpSocket.ReceiveFrom(bufferBytes, ref remoteEndPoint);
  
          if(numBytesReceived == 0) {
              udpSocket.Close();
              return;
          }
  
          Packet syncPacket = new Packet(bufferBytes);
  
          if(syncPacket.PacketType != PacketType.Control) {
              udpSocket.Close();
              return;
          }
      } catch {
          if(udpSocket != null) {
              udpSocket.Close();
          }
      }
  }
  </code></pre>
  
  <p>I'm sure that I have a bunch of unnecessary code but I'm new at this and I'm not sure what do, any help fixing up my code and how to solve the issues above is really appreciated.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I should probably have stated that my requirements are to use UDP and implement these things myself in the application layer. You can consider this as homework but I haven't tagged as such because the code is irrelevant and will not be part of my grade and my problem (my question) is in "how to code" as my Sockets experience is minimal and it's not taught.</p>
  
  <p>However I must say that I solved my problem for now I think... I was using threading on the demo application which was giving me some problems, now I'm using it in the protocol connections, makes more sense and I can easily change my custom protocol class properties and read those from the demo application.</p>
  
  <p>I have specified a timeout and throw a SocketException if it reaches the timeout. Whenever an exception like this is caught, the socket connection is closed. I'm just talking about the connection handshake, nothing more. If no exceptions are caught, the code probably went smooth and the connection is established.</p>
  
  <p>Please adapt your answers accordingly. Right now it doesn't make sense for me to marky any of them as the accepted answer, hope you understand.</p>