Does .NET have a message-based socket library?

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.

+4
source share
5 answers

Embedding a delimiter in a byte stream is not as simple as it might seem. You will have to deal with a message accidentally containing a delimiter. This leads to a lot of unpleasant code detection and escaping.

A very simple way to report frames is to add a length prefix. On the sender, you serialize the message to the buffer, then write the length, and then write the buffer. The receiver first reads 4 bytes to get the length. Then he reads the body.

This is not built into .NET, perhaps because each binary protocol is slightly different.

All this is so simple that you are unlikely to find a library. Such a protocol can be written in several tens of lines. My recommendation is just to do it yourself.

If you want to structure your message, there is a large library: protobuf-net (Google protocol buffers for .NET). This makes feedback for you as well as serialization in a very reasonable way. It perfectly supports version logging.

+2
source

Another option for this would be MSMQ, which is a message queue (not a TCP / IP stream) and processes messages, not byte streams. MSMQ itself can use various vehicles, including TCP / IP or WCF, etc.

If you are dealing only with Windows (and since you mentioned .NET, this is not an unreasonable limitation), it is available for all versions of Windows 2000.

There is a good example of code that shows the basic ways to use it, as well as some more advanced things. It is simple enough that the sender basically creates the message and queues it. Listeners receive full messages.

It could not have been much easier, and if you want to do messaging on Windows, this is one of the easiest ways to quickly do this.

+3
source

Apache Thrift is another option similar to WCF, but much simpler and not tied to a specific .NET Framework.

http://thrift.apache.org/

It works great for .NET 2 and even for Mono,

http://thrift.apache.org/docs/install/

0
source

I think the SignalR Open Source Framework should help.

Check out quickstarts samples to see if it meets your requirements.

0
source

You may currently want to use WebSockets.

Despite the name, this is actually a more general protocol. It is essentially message oriented. Although messages can be divided into frames, there are many libraries that will collect these frames in such a way that you get the full message.

Because of these libraries, you can use WebSockets rather than platform independent. Node, .Net, a clean javascript browser - no problem.

0
source

Source: https://habr.com/ru/post/1433957/


All Articles