1

I'm working on a TCP Client/Server system for practice purposes and I want to send specific data between the two.

I'm already able to send bytes and let them display as a string. Also I am able to send a specific string ("mb") and let a MessageBox pop-up on the server-side.

The content of the Box is the text sent (in this case "mb", though).

This is the Server-Side:

byte[] msg = new byte[4096];

var count = stream.Read(msg, 0, msg.Length);
string returnData = Encoding.ASCII.GetString(msg, 0, count);

switch(returnData)
{
    case "mb":                            
        MessageBox((IntPtr)0, returnData, "HACKERZ", 0);
             break;
    case "":                            
            client.Client.Disconnect(true);                        
        Console.WriteLine("User disconnected");
             break;
    default:
        Console.WriteLine(returnData);
             break;
}      

And this is Client-Side:

private void btnSend_Click(object sender, EventArgs e)
{
    NetworkStream stream = client.GetStream();

    byte[] msg = new byte[4096];
    msg = Encoding.ASCII.GetBytes(txtMsg.Text);

    stream.Write(msg, 0, msg.Length);
}

So if I write "mb" in the Textfield, it shows a MessageBox saying "mb".

I would like to know, how can I separate the message that was sent by the NetworkStream, so I can set Capture and Content of the MessageBox separately.

Rodia
  • 1,407
  • 8
  • 22
  • 29
Schmarc
  • 11
  • 4
  • You're going to run into problems eventually with your current code. There's nothing about the TCP protocol that guarantees you'll receive the entire message in one `Read()`. Whatever you send can freely be split up into separate packets. Separating the message on top of that is an entirely different concern. You can devise that however you want. Maybe use a newline character. Maybe prefix each component with a length. It's up to you. – itsme86 Oct 05 '17 at 23:08
  • Well, that is what i basically meant by separating. Putting a specific symbol in between the "messages" so my server knows where it starts and ends. How may I do that? – Schmarc Oct 06 '17 at 11:43
  • Possible duplicate of [C# Deserializing a struct after receiving it through TCP](https://stackoverflow.com/questions/37341382/c-sharp-deserializing-a-struct-after-receiving-it-through-tcp) – Visual Vincent Oct 15 '17 at 08:05

1 Answers1

0

As you noticed, you are sending binary data over socket. In client you get bytes from message msg = Encoding.ASCII.GetBytes(txtMsg.Text); and in server you're casting it back to string with string returnData = Encoding.ASCII.GetString(msg, 0, count);

In this approach, you can send for instance comma-separated string where first field would be Capture and second Content.

However, as it is byte stream, you can send anything what is serializable into bytes. You can use BinaryWriter\BinaryReader over Network stream to send any structure.

WueF
  • 450
  • 8
  • 15
  • Yes, I would like to separate them by a comma, that is what I initially meant. How would I do that? – Schmarc Oct 06 '17 at 10:30