我在Java中有一个TCP服务器,并且在C#中有一个客户端。该连接已经存在,可以发送一些消息,但是当我在流媒体关闭类似
之类的东西时,我收到的服务器收到的服务器已发送了许多消息。 LOCATION|015|0LOCATION|014,99163|0LOCATION|014,89123|0LOCATION|014,81594|0LOCATION|014,78247|0LOCATION|014,74063|0LOCATION|014,69043|0LOCATION|014,63187|0LOCATION|014,56493|0LOCATION|014,56493|0LOCATION|014,3976|0LOCATION|014,3976|0LOCATION|014,2972|0LOCATION|014,18843|0LOCATION|014,0713|0LOCATION|0,000219846914,07641|-0,000412796LOCATION|0,000259
在一行中,而不是一行中的LOCATION|UniversumGames|double;double;double
。请有人帮忙吗?P.S.客户不会错,因为我在Twitchbot中使用了类似于....
服务器代码(Java(:
public void run(){
try{
BufferedReader in = new BufferedReader((new InputStreamReader(socket.getInputStream())));
PrintWriter out = new PrintWriter(socket.getOutputStream());
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
out.println("Connected");
out.flush();
out.write("Connected 1");
out.flush();
while(socket.isConnected()){
String line = in.readLine();
//String line = dataInputStream.readUTF();
if(line != null){
CommandController.control(new Data(line), socket);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
客户端代码(C#(:
private TcpClient tcpClient;
private NetworkStream stream;
private StreamReader reader;
private StreamWriter writer;
private bool recieve;
public ClientConnection()
{
this.recieve = true;
this.tcpClient = new TcpClient("127.0.0.1", 2700);
Console.WriteLine("Connecting.....");
// use the ipaddress as in the server program
Console.WriteLine("Connected");
this.stream = tcpClient.GetStream();
this.writer = new StreamWriter(stream);
this.reader = new StreamReader(stream);
Console.WriteLine("Transmitting.....");
this.writer.AutoFlush = true;
writer.WriteLine("Connected clientside sucessful");
writer.WriteLine("Connected clientside sucessful 1");
writer.WriteLine("Connected clientside sucessful 2");
writer.WriteLine("Connected clientside sucessful 3");
}
public async void Recieve()
{
await Task.Run(()=>RecieveString());
}
public string RecieveString()
{
this.recieve = true;
while (recieve)
{
//if (stream.DataAvailable)
//{
string data = reader.ReadLine();
if (data != null)
{
writer.WriteLine(data + " recieved");
writer.Flush();
return data;
}
else return "";
//}
//else return "";
}
return "";
}
嘿,我帮助自己...
String line = "1";
while(line != null) {
byte size = dataInputStream.readByte();
byte[] buf = new byte[size];
dataInputStream.read(buf, 0, size);
line = new String(buf);
}
这是服务器中while-loop的更好方法...我发现我正在从客户端发送不同线程中发送消息,这可以解释为什么字符串混合在一起...