0
我正在用c#編寫一個tcp服務器,並在java中對應的客戶端。我正在測試本地主機上的連接,並且客戶端能夠連接到服務器。但是,當我發送消息時,客戶端永遠不會收到它們。使用調試器我已經驗證了stream.Write(...)被執行。任何想法可能是什麼問題?Java TCP客戶端不接收從C#服務器發送的消息
這是C#服務器:
TcpClient client = (TcpClient)cl;
NetworkStream stream = client.GetStream();
byte[] msg = new byte[512];
int bytesRead;
while (running)
{
while (messages.getCount() > 0)
{
String msg = messages.Take();
if (cmd != null)
{
byte[] bytes = Encoding.UTF8.GetBytes(msg.ToCharArray());
try
{
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
}
catch (Exception e)
{
}
}
}
Thread.Sleep(1000);
}
和Java客戶端:
public void run()
{
try
{
socket = new Socket(address, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
running = true;
}
catch (Exception e){
e.printStackTrace();
running = false;
}
String data;
while(running)
{
try
{
data = in.readLine();
if(data != null)
{
processData(data);
}
}
catch (IOException e)
{
e.printStackTrace();
running = false;
break;
}
}
try
{
socket.close();
socket = null;
}
catch (IOException e)
{
e.printStackTrace();
}
running = false;
}