While I am new to socket programming, since I wrote a simple tcp client-client program, I found that stream api is not so convenient to use.
My program is simple:
- Client connects to server
- The server continuously sends messages to the client for an undefined interval
- The client receives the message and performs the work in accordance with the data contained in the message
However, using thread based api, I have to:
- Define a message separator
- Use a
while to read data from a stream - Match all bytes, try to find the delimiter
- If a separator exists, separate the data, extract the message
It works a lot, even more than the whole logic of my program, so I wonder if there is a library that could do the things above for me, with a library that I can program as follows:
// Server TcpListenerEx server = new TcpListenerEx(); TcpClientEx client = server.Accept(); client.SendMessage(new StringMessage("hello world")); // Client TcpClientEx client = new TcpClientEx(); client.Connect("localhost", 8989); while (true) { IMessage message = client.ReadMesage(); // Do work acording to message }
Any suggestion would be welcome, thanks.
source share