I\'m developing a small TCP server, which will process some TCP packets and behave differently based on the request.
How can I write unit test for this? If it\'s really
If you write a TCP server, you should also be writing a client library. Unless you are doing something fancy, you can simply fire packets over the loopback adapter between your unit test and the stub server. You only need to verify that you are doing TCP properly with these tests, as the rest of your tests should, in proper unit test fashion, stub out/skip over the TCP/socket logic and jump right in to the individual unit being tested.
Doing all this is completely worth it. If you are new enough to TCP that you aren't sure how and why you should unit test it, chances are you'll make a few mistakes/faulty assumptions in how you write your TCP code, and unit tests will be invaluable for calling that out. You will also find it encourages you to focus on the client's needs more, which tends to result in a cleaner protocol and a cleaner server codebase.