2

I've a tcp based client-server application. I'm able to send and receive strings, but don't know how to send an array of bytes instead.

I'm using the following function to send a string from the client to the server:

static void Send(string msg)
{
    try
    {
        StreamWriter writer = new StreamWriter(client.GetStream());
        writer.WriteLine(msg);
        writer.Flush();
    }
    catch
    {
    }
}

Communication example

Client sends a string:

Send("CONNECTED| 84.56.32.14")

Server receives a string:

void clientConnection_ReceivedEvent(Connection client, String Message)
{
    string[] cut = Message.Split('|');
    switch (cut[0])
    {
        case "CONNECTED":
            Invoke(new _AddClient(AddClient), client, null);
            break;
        case "STATUS":
            Invoke(new _Status(Status), client, cut[1]);
            break;
    }
}

I need some help to modify the functions above in order to send and receive an array of bytes in addition to strings. I want to make a call like this:

Send("CONNECTED | 15.21.21.32", myByteArray);
1
  • An empty catch block ist always a rather bad idea. Commented May 5, 2012 at 18:43

3 Answers 3

5

Just use Stream - no need for a writer here. Basic sending is simple:

stream.Write(data, 0, data.Length);

However you probably need to think about "framing", i.e. how it knows where each sun-message starts and ends. With strings this is often special characters (maybe new line) - but this is rarely possible in raw binary. A common appoach is to proceed the message with the number f bytes to follow, in a pre-defined way (maybe network-byte-order fixed 4 byte unsigned integer, for example).

Reading: again, use the Stream Read method, but understand that you always need t check the return value; just because you say "read at most 20 bytes" doesn't mean you get that many, even if more is coming - you could read 3,3,3,11 bytes for example (unlikely, but you see what I mean). For example, to read exactly 20 bytes:

var buffer = new byte[...];
int count = 20, read, offset = 0;
while(count > 0 && ((read = source.Read(buffer, offset, count)) > 0) {
    offset += read;
    count -= read;
}
if(count != 0) throw new EndOfStreamException();
Sign up to request clarification or add additional context in comments.

Comments

0

Since you seem new to networking you might want to use WCF or another framework. I've just written an article about my own framework: http://blog.gauffin.org/2012/05/griffin-networking-a-somewhat-performant-networking-library-for-net

You need to use a header for your packets as Mark suggested, since TCP uses streams and not packets. i.e. there is not 1-1 relation between send and receive operations.

Comments

0

This is the same problems I'm having. I only code the client and the server accepts byte arrays as proper data. The messages start with an ASCII STX character preceded by a bunch of bytes of any values except the STX and ETX characters. The message ends with a ETX ASCII CHARACTER. In C I could do this in my sleep, but I'm learning C# on the job. I don't understand why you would send bunches of double byte unicodes when single byte ASCII codes work just as well. Wasting double the bandwidth.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.