Click here to Skip to main content
13,202,706 members (56,023 online)
Rate this:
Please Sign up or sign in to vote.
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 11-Dec-12 5:59am
Yvar Birx2.4K
Rate this: bad
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.
Rate this: bad
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:[^],[^].

Most likely, for performance reasons, you need binary serialization. Please see:[^].

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.[^].


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2017
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