2009-04-30

TCP/IP .NET Sockets FAQ

This is an attempt to address some TCP/IP frequently asked questions and present best practices. While the WinSock Programmer's FAQ will remain the ultimate FAQ for native code, there is a growing need for a simplified version that addresses the managed interface to TCP/IP sockets.

Section 1 - Application Protocol Design
1.1 - Message framing, also known as:
  "One side sent X bytes, but the other side only got Y bytes."
  "One side sent several packets, but the other side only got one packet, which was all the sent packets appended together."
  "I need the function that will send exactly one packet of data."
1.2 - Detection of half-open (dropped) connections, also known as:
  "My socket doesn't detect a lost connection; it just sits there forever waiting for more data to arrive."
1.3 - Application Protocol Specifications
1.4 - XML over TCP/IP

Section 2 - Socket Class
2.1 - Socket operations
2.2 - Error handling
2.3 - Using Socket as a client socket
2.4 - Using Socket as a server (listening) socket
2.5 - Using Socket as a connected socket

Section 3 - Miscellaneous
3.1 - Resources
3.2 - Getting the local IP address

Section C - Code
C.1 - Length-prefix message framing for streams
C.2 - Getting the local IP addresses

9 comments:

  1. Stephen, this is some very helpful documentation you have put together hear. Thanks very much for your efforts!

    ReplyDelete
  2. Fantastic article, really appreciate your work here. It's just a shame I've already encountered most of the pitfalls.

    ReplyDelete
  3. Hello,

    I've been able to program using sockets for a long time but I have never completely understood them. This really demystified everything I had questions about (such as the backlog on a socket's listening function). I'm very excited for my current project as I feel I'll be able to make it better just by reading what you've wrote.

    ReplyDelete
  4. If only I read this article a 2 years ago, would have saved me a lot of headaches. Great work!

    ReplyDelete
  5. This is brilliant! Thank you so much!

    ReplyDelete
  6. Thank you for the invaluable info. Though Im having one minor problem. I'm sending 10 small serialized objects from a server app to a client app (less than 100 bytes each). The Code works great when I step through it in debugger, but when I execute my program at 'full speed' it's not processing all of the objects. Any suggestions on how I can cure this?

    ReplyDelete
    Replies
    1. Sounds like your message framing is not working correctly. More than that I can't say without looking at the code.

      Try to get a minimal repro, post in Stack Overflow, and ping me to take a look at it.

      Delete