简体   繁体   中英

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

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 different 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM