0

I'm trying to realize a simple StreamSocket-communication between server and client. The server is written in Java and the client in C#.

To send a message (string) from the client to the server works fine. But the other way around I have some trouble.

When I send a string from the server to the client, I want, that every message is printed out from the client.

I initialize my StreamSocket on client-side as follows:

public async void startConn()
{
   StreamSocket streamSocket = new StreamSocket();
   try
     {
       await streamSocket.ConnectAsync(new Windows.Networking.HostName(server), port.ToString());

       DataReader reader = new DataReader(streamSocket.InputStream);
       reader.InputStreamOptions = InputStreamOptions.Partial;
       reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
       var count = await reader.LoadAsync(256);

       while (true)
       {
         string text = reader.ReadString(count);           
         Debug.WriteLine("Message: " + text);
         count = await reader.LoadAsync(256);
       }  
     }catch (Exception e)
     {
       //TODO
     }
}

I want, that the client prints out every string from the server, even how long the message is. But if I send a message from the server to the client, it does not print out every string immediately although I set the option of the StreamSocket to Partial.

For example:

Server send:   Hello         Client: print out nothing
Server send:   Today         Client: print out "Hello"
Server send:   Hi            Client: print out nothing
Server send:   Bye           Client: print out "Hi"

I tried different versions of LoadAsync() with another size (not 256) and UnconsumedBufferLength. But everytime I have the same result. Can anybody tell me, what's wrong?

Best regards DJTrust

DJTrust
  • 23
  • 6
  • Can you please be more specific about the server's behavior? What is the interval of time between each string that is sent? Are the strings sent without any delimiter (e.g. line-break characters)? Are you _sure_ the server is working correctly? Have you tried testing the server with a regular desktop .NET program instead of the WinRT API? – Peter Duniho Mar 28 '15 at 20:12
  • For what it's worth, I tried to reproduce your issue by implementing a simple server to send the strings you describe, and running your code above in a basic WinRT app, and I was unable to reproduce the problem. So there's nothing fundamentally wrong with the code you've shown so far. You will need to provide [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that reliably reproduces the problem if you would like specific help with your question. – Peter Duniho Mar 28 '15 at 22:10

1 Answers1

0

yes the problem was server-side. It unbelievable, i was so sure, that the java-code is correct because this side is really easy. I was too concentrated on the client-side.

My mistake was as follows:

...
while(!(in.readLine().equals("quit")))
{
   String str = in.readLine();
   out.println(str);
   out.flush();
}
...

There was one in.readLine() to much. Now the correct way is:

String str = "";
while(!(str.equals("quit")))
{
   str = in.readLine();
   out.println(str);
   out.flush();
}

I could bite myself ;). Thanks for your hints.

DJTrust

DJTrust
  • 23
  • 6