开发者

Server - client project (messanging protocols)

I want to do a project, that will include server and clients sides, using TcpSocket network communication (I use TcpListener for server and TcpClient for client side) and threading. But threading is not giving me any problems so far any longer.

But what it does, is something else... because the project will not include only chat, (but also creating new game, joining game, making moves, leaving game), I need to define somehow the message format.

I have read about messaging protocols and about using first few bytes of each message to tell the server what they are trying to do. The problem is that I do NOT know how to do it. So can someone show me an example of creating formated message?

Maybe its good to mention I use StreamReader and StreamWriter classes to pass data between server and clients. Is this a good way?


To add:

My problem now, is how to seperate this data, so that the server will know what to do with it. I have read about using 1nd few bytes to "be reserved" for the type of the message. But the problem is I don't know how to solve this issue. So far I was only using StreamReader and StreamWriter classes to pass only strings. If I use these kind of coding, it will all become too开发者_开发知识库 messy (not recognizable), if you know what I mean.

So I need to do something like it:

To send bytes:

  • 1st few bytes the type of the data (but I don't know which class to use, maybe a BinaryWriter, and BinaryReader on the other side??)

  • the rest of the message

  • on server I have to have some code that will recognize these "1st few bytes" so the code will know what to do with the test of the message.

  • and based on these "1st few bytes" the code has to send data back to clients

Do you have any ideas on what this might look like, I mean as a skeleton (something basic, so I can work on with it).

Every bit of help would be very much appreciated.


I have found one example here on stackOverflow.com. It seems to be a code into a right direction. What do you think guys?


One option that might be much easier than defining your own protocol would be to use some existing library, like WCF or JSON-RPC.

Libraries like this have their disadvantages (they often produce output that is relatively big). If if you build your applications in a modular way, you can easily switch the communication backend later, when you find your first solution wasn't good enough.


You have a few options: Make the message format textual - including its header. So, the header contains the message size, written out as text string, and ends with some terminator (\r\n for example.) HTTP takes this approach - the headers are text. When receiving the message, you process it with ReadLine, and parse header lines with stuff like int.TryParse(str).

Send message:

output.WriteLine(message.Length);
output.Write(message);

Receive message:

int len = int.Parse(input.ReadLine());
//message is the next 'len' characters

A second option: entire message is in a binary format - the message header is binary, and specifies amongst other things the message length. Message content is written as a stream of bytes, encoded using for example Encoding.UTF8.GetBytes(str).

Or, perhaps use the HTTP protocol? I believe there is basic support for HTTP server in the form of HttpListener. Here is a simple intro to HttpListener

Whatever you do, avoid using WCF :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜