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