views:

499

answers:

2

I'm building a simple udp lan chat application in vb.net and I'm wondering how I should split my packets. Each sent packet should have like an id, a username and ip address from where it's coming and maybe also a command part for like join or leave to update my userlist and a text message. I'd like to know what is the easiest way to put all this in a simple packet then easily split and access different parts from it when it's received. thanks.

I'm using UDP since this is only in lan so i'm broadcasting to *.*.*.255

EDIT: Thank you for your answer Jon but I already know all that. What I want to know is what would be the most easy and powerful way to format my packets so they include a username , an id, a command and a text message, then the user receiving it decrypt it to show only the message written by which user or if it's a command like join or leave to show the appropriate message of joining and add the user to the list for exemple.

+1  A: 

Create an appropriate class with an instance ToByteArray method and a static FromByteArray() method (for serializing to a byte array and parsing from a byte array respectively). Then use UdpClient.Send() to send it, and UdpClient.Receive() to receive it.

You may want to use BinaryReader/BinaryWriter and/or BitConverter to help with the ToByteArray and FromByteArray methods. You can use a MemoryStream as a quick in-memory stream to pass to BinaryReader/BinaryWriter.

Jon Skeet
A: 

I would probably just format the packet in xml and then on the receive side use linq to xml to pull it apart. You could also use JSON for the format but that might be slightly harder to parse.

Rex Logan