我有一个客户端类和一个服务器类。 如果客户端向服务器发送消息,服务器会将响应发送回客户端,然后客户端将打印收到的所有消息。
例如,
如果客户端向服务器发送“A”,则服务器将向客户端发送响应 “1111”。所以我在客户端类中使用readLine()从服务器读取消息,然后客户端在控制台中打印“1111”。
如果客户端向服务器发送“B”,则服务器将向客户端发送响应 “2222 \ n 3333”。所以客户的预期打印输出是:
“2222”
“3333”
因此,从服务器到客户端的响应消息可能有1行或2行,具体取决于它从客户端发送到服务器的消息。
我的问题是我如何使用readLine()来读取从服务器发送到客户端的消息。更具体地说,如果我使用以下代码,
String messageFromServer;
while(( messageFromServer = inputStreamFromServer.readLine()) != null) {
println(messageFromServer);
}
它只会打印第一行,即使我不断从客户端向服务器发送消息也不会打印任何其他内容,因为readLine()会在读取第一行后停止。
更新 更具体地说,我正在寻找客户端类中的一些方法来读取一次包含来自服务器的1行或多行的消息。我想知道如果我不想更改从服务器发送到客户端的消息格式,是否有任何方法可以在客户端执行此操作。
更新2 为了使我的问题更清楚,我将在下面添加一些示例代码:
这是服务器:
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(1234);
} catch (IOException e) {
System.err.println("Could not listen on port: 1234.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
}
System.out.println("Connected");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String textFromClient =null;
String textToClient =null;
textFromClient = in.readLine(); // read the text from client
if( textFromClient.equals("A")){
textToClient = "1111";
}else if ( textFromClient.equals("B")){
textToClient = "2222\r\n3333";
}
out.print(textToClient + "\r\n"); // send the response to client
out.flush();
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
客户:
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
try {
socket = new Socket("localhost", 1234);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection");
}
System.out.println("Connected");
String textToServer;
while((textToServer = read.readLine())!=null){
out.print(textToServer + "\r\n" ); // send to server
out.flush();
String messageFromServer =null;
while(( messageFromServer = textToServer=in.readLine()) != null){
System.out.println(messageFromServer);
}
}
out.close();
in.close();
read.close();
socket.close();
}
private static void debug(String msg)
{
System.out.println("Client: " + msg);
}
}
答案 0 :(得分:2)
您不需要更改服务器发送的数据的格式,readLine()应该可以工作,但我怀疑服务器在写入可能解释事情的响应后没有刷新或关闭OutputStream。
readLine()的调用是挂起的吗?你在控制服务器代码吗?如果是这样,你可以加入吗?
修改后的课程,我相信您的期望:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ClientServerTest2
{
public static void main(String[] args) throws Exception
{
Thread serverThread = new Thread(new Server());
serverThread.start();
Thread clientThread = new Thread(new Client());
clientThread.start();
serverThread.join();
clientThread.join();
}
private static class Server implements Runnable
{
@Override
public void run()
{
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(1234);
Socket clientSocket = null;
clientSocket = serverSocket.accept();
debug("Connected");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String textFromClient = null;
String textToClient = null;
textFromClient = in.readLine(); // read the text from client
debug("Read '" + textFromClient + "'");
if ("A".equals(textFromClient))
{
textToClient = "1111";
}
else if ("B".equals(textFromClient))
{
textToClient = "2222\r\n3333";
}
debug("Writing '" + textToClient + "'");
out.print(textToClient + "\r\n"); // send the response to client
out.flush();
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void debug(String msg)
{
System.out.println("Server: " + msg);
}
}
private static class Client implements Runnable
{
@Override
public void run()
{
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
try
{
socket = new Socket("localhost", 1234);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
debug("Connected");
String textToServer;
textToServer = read.readLine();
debug("Sending '" + textToServer + "'");
out.print(textToServer + "\r\n"); // send to server
out.flush();
String serverResponse = null;
while ((serverResponse = in.readLine()) != null)
debug(serverResponse); // read from server and print it.
out.close();
in.close();
read.close();
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private static void debug(String msg)
{
System.out.println("Client: " + msg);
}
}
答案 1 :(得分:0)
将while(( messageFromServer = inputStreamFromServer.readLine() != null)
更改为while(( messageFromServer = inputStreamFromServer.readLine()) != null)
实际上这甚至不应该编译......
答案 2 :(得分:0)
这是一个解决方法。
如果你想发送多个字符串,如你的情况:“2222 \ n 3333”。
您可以通过在两个字符串之间添加分隔符(如:)来发送它们:“2222 :
3333”。
然后你可以从服务器端调用
clientOut.write("2222: 3333\n");
在客户端解析收到的字符串:
messageFromServer = inputStreamFromServer.readLine();
String strArray[] = messageFromServer.split(":");
strArray[0] : 2222
strArray[0] : 3333