开发者

.Net using Chr() to parse text

开发者 https://www.devze.com 2022-12-22 16:47 出处:网络
I\'m building a simple client-server chat system. The clients send data to the server and the server resends the data to all the other clients. I\'m using the TcpListener and Network stream classes t

I'm building a simple client-server chat system.

The clients send data to the server and the server resends the data to all the other clients. I'm using the TcpListener and Network stream classes to send the data between the client and the server.

The field开发者_C百科s I need to send are, for example: name, text, timestamp, etc. I separate them using the ASCII character 29.

I'm also using ASCII character 30 to mark the end of the streamed data.

The data is encoded with UTF8..

Is this a good approach? Will I run into problems? Are there better methods?

UPDATE:

Probably my question was misunderstood, so I explain it better.. Suppose to have a list of data to send from client to server, and suppose to send all the data in only one stream, how do you send these data?

  • Using a markup
  • Using a character as a delimiter
  • Using a fixed length for every fields


Rather than delimiting text, I find it much simpler to length-prefix individual messages - so rather than writing

message [TERMINATOR]

You'd write

[LENGTH] message

(Send the length in binary, rather than text - either a fixed-length number, e.g. 4 bytes, or a variable-length number if you're willing to sacrifice simplicity for the sake of reducing the transmitted data size very slightly.)

Then it's easy to read exactly the right amount of data from the stream without worrying about overshooting into the next message. The only downside is that you need to know the length of the message before you start sending it, but I don't think it should be a problem for you in this case.

By the way, UTF-8 is an encoding, not an encryption algorithm.


I thinkg that another option could be to serialize/deserialize your objects and send/receive them using .NET Remoting. You could also encrypt your objects when sending and decrypt when receiving.

In The Code Project I´ve found a little example with source code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号