Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1 - <strong>Binding and listening on a socket</strong></p> <p>Looks fine to me. Your code will bind the socket only to one IP address though. If you simply want to listen on any IP address/network interface, use <code>IPAddress.Any</code>:</p> <pre><code>serverSocket.Bind(new IPEndPoint(IPAddress.Any, 4444)); </code></pre> <p>To be future proof, you may want to support IPv6. To listen on any IPv6 address, use <code>IPAddress.IPv6Any</code> in place of <code>IPAddress.Any</code>.</p> <p>Note that you cannot listen on any IPv4 and any IPv6 address at the same time, except if you use a <a href="http://msdn.microsoft.com/en-us/library/bb513665(VS.85).aspx" rel="nofollow noreferrer">Dual-Stack Socket</a>. This will require you to unset the <code>IPV6_V6ONLY</code> socket option:</p> <pre><code>serverSocket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, 0); </code></pre> <p>To enable <a href="http://technet.microsoft.com/en-us/network/cc917486.aspx" rel="nofollow noreferrer">Teredo</a> with your socket, you need to set the <code>PROTECTION_LEVEL_UNRESTRICTED</code> socket option:</p> <pre><code>serverSocket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)23, 10); </code></pre> <p>2 - <strong>Receiving data</strong></p> <p>I'd recommend using a <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx" rel="nofollow noreferrer"><code>NetworkStream</code></a> which wraps the socket in a <code>Stream</code> instead of reading the chunks manually.</p> <p>Reading a fixed number of bytes is a bit awkward though:</p> <pre><code>using (var stream = new NetworkStream(serverSocket)) { var buffer = new byte[MaxMessageLength]; while (true) { int type = stream.ReadByte(); if (type == BYE) break; int length = stream.ReadByte(); int offset = 0; do offset += stream.Read(buffer, offset, length - offset); while (offset &lt; length); ProcessMessage(type, buffer, 0, length); } } </code></pre> <p>Where <code>NetworkStream</code> really shines is that you can use it like any other <code>Stream</code>. If security is important, simply wrap the <code>NetworkStream</code> in a <a href="http://msdn.microsoft.com/en-us/library/system.net.security.sslstream.aspx" rel="nofollow noreferrer"><code>SslStream</code></a> to authenticate the server and (optionally) the clients with X.509 certificates. Compression works the same way.</p> <pre><code>var sslStream = new SslStream(stream, false); sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true); // receive/send data SSL secured </code></pre> <p>3 - <strong>Sending data and specifying the data length</strong></p> <p>Your approach should work, although you probably may not want to go down the road to reinventing the wheel and design a new protocol for this. Have a look at <a href="http://en.wikipedia.org/wiki/BEEP" rel="nofollow noreferrer">BEEP</a> or maybe even something simple like <a href="http://code.google.com/p/protobuf-net/" rel="nofollow noreferrer">protobuf</a>.</p> <p>Depending on your goals, it might be worth thinking about choosing an abstraction above sockets like WCF or some other RPC mechanism.</p> <p>4/5/6 - <strong>Closing &amp; Unknown disconnections</strong></p> <p>What <a href="https://stackoverflow.com/questions/1162950/getting-started-with-socket-programming-in-c-best-practices/1163105#1163105">jerryjvl</a> said :-) The only reliable detection mechanism are pings or sending keep-alives when the connection is idle.</p> <p>While you have to deal with unknown disconnections in any case, I'd personally keep some protocol element in to close a connection in mutual agreement instead of just closing it without warning.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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