使用DataOutputStream发送多条消息

时间:2016-09-08 16:41:27

标签: c# android tcpclient networkstream dataoutputstream

我尝试将3条消息从Android tcp客户端发送到C#服务器。它在另一个C#客户端中正常工作,但它不在Android客户端中。 我发送了3条消息,但是服务器正确获取了第一条消息,然后第二条和第三条消息加入了,就像它们只是一条消息一样

服务器代码:

NetworkStream stream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
int data = stream.Read(buffer, 0, client.ReceiveBufferSize);
string message = Encoding.UTF8.GetString(buffer, 0, data);
int one = Int32.Parse(message);
buffer = new byte[client.ReceiveBufferSize];
data = stream.Read(buffer, 0, client.ReceiveBufferSize);
message = Encoding.UTF8.GetString(buffer, 0, data);
int two = Int32.Parse(message);
buffer = new byte[client.ReceiveBufferSize];
data = stream.Read(buffer, 0, client.ReceiveBufferSize);
message = Encoding.UTF8.GetString(buffer, 0, data);
int three = Int32.Parse(message);

Android客户端是:

EditText et = (EditText) findViewById(R.id.EditText01);
String one = "56";
String two = "300";
String three = "4";
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeBytes(one);
out.flush();
out.writeBytes(two);
out.flush();
out.writeBytes(three);
out.flush();

服务器获取:one = 56 two = 3004

而不是:one = 56 two = 300 three = 4

0 个答案:

没有答案