Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C#4.0
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 11-Dec-12 6:59am
Yvar Birx2.4K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 BillWoodruff 260
3 Shweta N Mishra 244
4 Deepu S Nair 230
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,818
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,755


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100