Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Readers,

Before you think this is about how to actually send a byte array via the Internet, I already know how to do that. What I need to know is how to actually send a packet like thing. I might actually use the wrong term, it might actually not be called a Packet. But anyways, I'll try to explain my problem;

I am about to create a Chat Application, in this Chat Application you can chat, I already know how to do that. But I want to take about a million steps further, I am going to add an abillity to use the Microphone, also I am going to add Usernames, Statusses and more. The only problem I've faced as I was creating this is making a method that allows to send different types of information.

This is how a TCP Server works:
Wait for byte[], Receive byte[], do following action, repeat.

I usually made the first byte in the byte[] to a certain number, for instance, byte 50 would mean a regular chat message, byte 60 would be a voice message. So that way, a client and the server know exactly what those byte[] are for. But this is really annoying and only works half of the time. Does anyone know a better way of doing this ?
Posted

Use record types. Prefix the actual data in each message with a header that identifies the record type and its length, and perhaps any other information you may want. Then each end will know exactly what the payload is on any message they process.
 
Share this answer
 
It's good that you are not satisfied with this ad-hoc approach. You need to use more or less advanced data structures, and your transport layer should be totally agnostic to them. For this reason, you need to learn serialization:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[^],
http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx[^].

Most likely, for performance reasons, you need binary serialization. Please see:
http://msdn.microsoft.com/en-us/library/72hyey7b.aspx[^].

You don't have to work with with network and TCP using sockets or TcpWriter/TcpReader. Alternatively, you can use either "classic" .NET remoting of WCF. Please see my short overview of different levels of networking in my past answers:
how i can send byte[] to other pc[^],
Communication b/w two Windows applications on LAN.[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900