开发者

Send commands from client to server in C# using tcp/ip

开发者 https://www.devze.com 2023-02-27 00:38 出处:网络
All the examples i found are sending only text/string data over the network. I figure out how to send different types of objects (ArrayLists etc).

All the examples i found are sending only text/string data over the network. I figure out how to send different types of objects (ArrayLists etc). I'm now trying to find out how to process commands sent from the client on the server. From the client i have to : add ä "Student" in the database delete a student in the database get all students in the database etc.

so, if i create a protocol on the client side with a method "processCommand" sometimes i have a different number of parameters, depending on the request from the client ( when adding a student, i have to send the student object) , ( when getting data from the database i don't have to send any parameters) ; also i have to return di开发者_JS百科fferent type of objects. How can i do this ? Thank you very much.


As suggested by @marc_s. There is no reason to reinvent the wheel. Use WCF with tcpBinding.

If you need to do it by yourself you need to use some kind of serialization. You also need to attach a header since TCP is stream based and do not guarantee that everything arrives in the same Receive.

I would do it like this:

  1. Serialize your object into a byte buffer using BinaryFormatter.
  2. Send a header containing version (an int) and number of bytes in the byte buffer (int)
  3. Send the byte buffer.
0

精彩评论

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