客户端通过服务器发送它等待服务器的回复

时间:2013-02-22 08:48:53

标签: java sockets java-ee network-programming client-server

我有一个类似下面的代码

服务器端:

Socket socket = server-client conn socket
try
{
BufferedReader inFromNode = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
PrintWriter outToNode = new PrintWriter(socket.getOutputStream(), true);
String data = inFromNode.readLine().toString();
String data1 = inFromNode.readLine().toString();
String data2 = inFromNode.readLine().toString();
outToNode .println("Hi");
}

客户方:

Socket socket = server-client conn socket
try
{
BufferedReader inFromNode = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
PrintWriter outToNode = new PrintWriter(socket.getOutputStream(), true);
outToNode .println("Hi");
outToNode .println("Hi");
outToNode .println("Hi");
String data = inFromNode.readLine().toString();
}

问题是客户端代码正在等待来自服务器的回复。我确定服务器端发送了它(我尝试在服务器端发送后放置日志并打印出来。)我在这里可以忽略一些东西吗?代码是否有任何错误?

1 个答案:

答案 0 :(得分:0)

尝试从服务器写入客户端时关闭PrintWriter和Socket。这应该理想地解决您的问题。