0

I have a C# TCP chat program. Currently, I have formatted the messages sent using strings i.e, a "login" message starts with a "3" then followed by a "U:" then the username etc.

I think this method is very crude in a way that it's not really readable and not standardized. In early research, I have read that I can format my messages using XML but I dont know where to start exactly. Do I just make a string builder and append it tags like .append("<Login>"+message)?

3 Answers 3

3

The most common approach for dealing with a problem like this is to use serialization. Serialization is the process of converting an in-memory object into a format that can be easily streamed "over the wire," and de-serialization is the reverse process of converting the serialized format back into an object. .NET has good support for XML and binary serialization out-of-the-box, but there are other ways to implement this. Here's a link to get you started:

http://msdn.microsoft.com/en-us/library/7ay27kt9(VS.71).aspx

Sign up to request clarification or add additional context in comments.

Comments

1

You can send whatever you like over the connection - as long as it's just for your program it doesn't really matter what you choose. Xml might give you some benefits as it lends itself to some kind of more structured messages and there are many classes and tools and knowledge around on the net regarding XML. JSon format might be another option - it will make it potentially easier creating a JavaScript client for it in case you want to go web based.

Comments

0

Unless there is a reqirement that 3rd parties be able to read these messages then I would probably favour binary serialisation, as it has a more compact format.

That said, I'd probably just use WCF rather than uisng TCP directly.

If you want to know more about XML serialisation then the most commonly used methods are:

You can write our XML yourself as a string, but its better to use the serialisation methods made available in the .Net framework as it makes things considerably easier and reduces the chance that you will make a mistake and inadvertantly start working with invalid xml.

3 Comments

So I can just format my messages by making a string builder like "<LOGIN><UNAME>test</UNAME><PASS>1234</PASS></LOGIN>" Then convert or interpret that somehow in my server? Is there a good way of doing that? Sorry if I seem lazy but I'm really just trying to broaden my knowledge in programming and I've stumbled upon TCP. I tried googling for good articles about XML but what I've seen so far are about XML files.
@Raphael Yes, that would be valid XML - you could then use either a XmlSerializer (combined with a class with appropriate serialisation attributes) or a XmlDocument (combined with some XPath) to read it on the server. You should definitely give this a try and see how it works, but its not the approach I would recommend (use binary serialisation instead).
Thanks a lot, @Kragen. I'd try this first then I'll look into binary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.