[英]Java(client) and C#(Server) TCP Socket. and Server read infinite last data from client
Java(client) and C#(Server) TCP Socket. Java(客户端)和 C#(服务器)TCP 套接字。 and Server read infinite last data from client(Java) I has been search for entire day already, its weird.
和服务器从客户端(Java)读取无限的最后数据我已经搜索了一整天,这很奇怪。
I created 2 client: Java(real), C#(for testing) like this:我创建了 2 个客户端:Java(真实),C#(用于测试),如下所示:
Java(Real): Java(真实):
Socket socket = new Socket(SyncActivity.ip,SyncActivity.port);
DataOutputStream out;
out = new DataOutputStream(socket.getOutputStream());
String s = "Hello!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$";
out.write(s.getBytes(),0,s.getBytes().length);
out.flush();
s = "Yes this another data$";
out.write(s.getBytes(),0,s.getBytes().length);
out.flush();
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
Thread.currentThread().interrupt();
and C#(for testing)和 C#(用于测试)
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
clientSocket.Connect("192.168.0.138", 11838);
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
On C#(testing) side, sent one data to server, and server readed the data once and blocked to wait another data.(second data same as reading it once and blocked)I just want like this.在 C#(测试)端,向服务器发送一个数据,服务器读取一次数据并阻塞以等待另一个数据。(第二个数据与读取一次并阻塞相同)我只是想要这样。
On Java(Real)side, sent two data to server and server reading 1st data once and read same 2nd data forever.在Java(Real)端,向服务器发送两个数据,服务器读取第一个数据一次并永远读取相同的第二个数据。 Java side is verified sent only once.
Java 端验证仅发送一次。
It is full data I was received.这是我收到的完整数据。 Why?
为什么? its weird.
有点奇怪。
result as:
结果为:
on C#(testing)在 C# 上(测试)
Client request connection.
客户端请求连接。
Server accept and created connection.
服务器接受并创建连接。
Server try Read (and Blocked)
服务器尝试读取(并阻止)
Client send >> TextBox
客户端发送>>文本框
Server received >> TextBox
服务器收到 >> 文本框
Server try Read (and Blocked)
服务器尝试读取(并阻止)
but on Java(Real):但在 Java(Real) 上:
Client request connection.
客户端请求连接。
Server accept and created connection.
服务器接受并创建连接。
Server try Read (and Blocked)
服务器尝试读取(并阻止)
Client send >> Hello!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
客户端发送 >> 你好!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!
Server received >> Hello!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
服务器收到 >> 你好!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!
Client send >> Yes this another data
客户端发送 >> 是的,这是另一个数据
Server try Read
服务器试读
Server received >> Yes this another data
服务器收到 >> 是的,这是另一个数据
Server try Read
服务器试读
Server received >> Yes this another data
服务器收到 >> 是的,这是另一个数据
Server try Read
服务器试读
Server received >> Yes this another data
服务器收到 >> 是的,这是另一个数据
Server try Read
服务器试读
Server received >> Yes this another data
服务器收到 >> 是的,这是另一个数据
Server try Read
服务器试读
Server received >> Yes this another data
服务器收到 >> 是的,这是另一个数据
Server try Read
服务器试读
Server received >> Yes this another data
服务器收到 >> 是的,这是另一个数据
(and forever and forever)
(并且永远永远)
Server code:服务器代码:
TcpListener serverSocket = new TcpListener(IPAddress.Any, Convert.ToInt16(Ini.IniReadValue("About", "ServerPort", "MVS_VAN.info")));
TcpClient clientSocket = default(TcpClient);
serverSocket.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
serverSocket.Start();
clientSocket = serverSocket.AcceptTcpClient();
serverSocket.Stop();
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[128000];
do
{
Socket soc = clientSocket.Client;
soc.Receive(bytesFrom);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
MessageBox.Show("", dataFromClient);
networkStream.Flush();
//networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
//string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
//dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
//tried ^ this 3 line also.
} while ((true));
clientSocket.Close();
serverSocket.Stop();
The server is ignoring the count returned by Receive()
.服务器忽略
Receive()
返回的计数。 It is therefore (a) ignoring endi of stream and (b) processing invalid data in the buffer.因此,它 (a) 忽略流的 endi 和 (b) 处理缓冲区中的无效数据。
Im found what is the problem after a day investigate.经过一天的调查,我发现了问题所在。
but its still weird:但它仍然很奇怪:
on Java Client :在 Java 客户端上:
socket.close();//<--here causing a start of server read same last data infinitly
it was not closing it properly.它没有正确关闭它。 maybe where protocol is still close_wait state to ensure all packets is arrived to server?
也许协议仍然是close_wait状态以确保所有数据包都到达服务器?
. .
fake resolve :假解决:
Im write my own command packets send to server like "cmd_request_close_connection", and close the connection on server side when server receive packet like this.我写了我自己的命令数据包发送到服务器,如“cmd_request_close_connection”,并在服务器收到这样的数据包时关闭服务器端的连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.