Java and C# – Ensuring String Sentence Consistency Over Sockets

cjavanetworkingsockets

I am learning socket programming. I have a Java application as my server, and C# for the client. Just sending string lines from one to another. Below I have code snippets showing how I am currently doing it.

Anyway, being new to this, there are two points that are still bugging me:

If you send a very large string, it is possible that one fragment arrives, and then the other fragment. Of course this makes sense, specially in slow connections like mine. My doubts are:

  • Suppose that my very large string is sent, and it will arrive in two halves. I suppose it is plausible for the second half to arrive before the first one – for whatever network reason. If this is the case, how is one supposed to handle this?
  • So my very large string is sent and chunks of varying sizes arrive one after the other – what is the right way to determine that all these chunks belong to the same string line? In other words, if I send "I like cats" and the target receives "I like" and "cats", how can it deduce that those two things are meant to be part of the same sentence, rather than two different sentences? I guess that this is entirely up to my own protocols: perhaps I can send over tags like <start> and <finish> as part of my sentence – this way the receiver knows that whatever lies between thew two tags is one sentence… assuming that the chunks arrive in the intended order (which depends on my question above).

Receiving a string

JAVA

    while (true) {
        if (socket.getInputStream().available() != 0) {
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
            System.out.println("Got message: " + input.readLine());
        }
    }

C#

    byte[] myReadBuffer = new byte[1024];
    System.Text.StringBuilder message = new System.Text.StringBuilder();
    int numberOfBytesRead = 0;
    do{
        numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);
        message.AppendFormat("{0}", System.Text.Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
    }
    while(stream.DataAvailable);

Sending a string

JAVA

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    writer.write("HELLO");

C#

    string msg = "HELLO";
    byte[] messageBytes = System.Text.Encoding.ASCII.GetBytes(msg);
    stream.Write(messageBytes,0,messageBytes.Length);

Best Answer

Given that you use streams then the underlying implementation will be using TCP which guarantees that packets arrive in order aka out of order delivery is not your problem™. If you use UDP (datagrams) you will have to deal with out of order and missing packets yourself though they won't be split up.

And most protocols should have a way of differentiating subsequent (logical) packets; for example by prefixing the length of the packet or by delimiting (post fix a special sequence).

Related Topic