我正在尝试在服务器和客户端之间建立 TCP 连接。服务器是用 C# 编写的,客户端是用 Java 编写的...服务器工作正常...我的问题出在这段代码中:
try {
InetAddress address = InetAddress.getByName("127.0.0.1");
connection = new Socket(address, port);
BufferedReader inFromServer = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
loginInfo = inFromServer.readLine();
System.out.println("username/pass are received");
System.out.println(loginInfo);
connection.close();
} catch (IOException f) {
System.out.println("IOException: " + f);
} catch (Exception g) {
System.out.println("Exception: " + g);
}
应用程序被阻止,我无法再关闭它......直到我从 java 完成调试。 我猜问题出在登录信息中,因为我没有在输出中收到用户名/密码。所以有什么帮助吗?
这是从 C# 发送消息的线程:
Thread listener_service = new Thread((ThreadStart)delegate
{
listener.Start();
while (true)
{
s = listener.AcceptSocket();
Console.WriteLine("Connected to !" + s.RemoteEndPoint);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server. \n"));
Console.WriteLine("\nSent Acknowledgement");
continue;
}
});
最佳答案
调用 readLine()
是阻塞调用,意味着您的代码执行将被阻塞,直到您收到来自服务器通信的任何行。
使用 System.Environment.NewLine
而不是 \n
来终止 C#
代码中的行。
关于c# - 使用 Readline 后我的应用程序(服务器/客户端)被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16671077/