我创建了一个java类TCPClient,它将Message对象发送到TCPServer。我使用DataOutputStream和DataInputstream(这是我们被告知要执行的任务,我们必须使用它们以便从客户端向服务器发送对象)。 这是我的TCPClient类
public class TCPClient {
public static final int PORT = 4000;
public static void main(String[] args){
String hostname = "localhost";
try(Socket client = new Socket(hostname,PORT);
//BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
Message message;
String input;
String the_message;
double random;
int id;
while(!(input = stdIn.readLine()).equalsIgnoreCase("end")){
random = Math.random()*100;
message = new Message();
the_message = input;
id = (int)random;
message.setId(id);
message.setMessage(the_message);
encode(message,client);
//System.out.println(in.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void encode(Message message, Socket client) {
try(DataOutputStream out = new DataOutputStream(client.getOutputStream())){
out.writeUTF(message.getMessage());
out.writeInt(message.getId());
} catch (IOException e) {
e.printStackTrace();
}
}}
TCPServer类
public class TCPServer {
public static final int PORT = 4000;
public static void main(String[] args) {
try (ServerSocket ss = new ServerSocket(PORT);
Socket cs = ss.accept();
//PrintWriter out = new PrintWriter(cs.getOutputStream(), true);
) {
Message message = decode(cs);
System.out.print("Message received: " + message.getMessage() + " id: " + message.getId());
} catch (IOException e) {
e.printStackTrace();
}
}
private static Message decode(Socket client){
Message message = new Message();
try(DataInputStream in = new DataInputStream(client.getInputStream())){
message.setMessage(in.readUTF());
message.setId(in.readInt());
return message;
} catch (IOException e) {
System.err.print("Error happened while decoding object");
return null;
}
}}
我不想做的是当我从客户端发送新消息时,服务器关闭。我希望服务器继续监听消息。我该如何做到这一点?
尝试过这样的事情
while(cs.getInputStream().available()==0){
Message message = decode(cs);
System.out.println("Message received = " + message.getMessage() + " id " + message.getId());
}
但没有工作
编辑:我不想构建一个多线程服务器来托管多个客户端。我希望单个服务器从唯一客户端接收多条消息,并在客户端发送"结束"时关闭套接字,我不知道是否可能。答案 0 :(得分:0)
您的客户端最多只发送一条消息,因为encode()
通过关闭套接字输出流来关闭套接字。如果您希望客户端发送多个消息,请尝试以下操作:
public class TCPClient {
// ...
public static void main(String[] args) {
try (Socket client = new Socket(hostname, PORT);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream out = new DataOutputStream(client.getOutputStream())) {
// ...
while (!(input = stdIn.readLine()).equalsIgnoreCase("end")) {
// ...
encode(message, out);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void encode(Message message, DataOutputStream out) throws IOException {
out.writeUTF(message.getMessage());
out.writeInt(message.getId());
}
}
服务器最多只接收一条消息,因为decode()
通过关闭其输入流来关闭套接字。这应该这样做:
public class TCPServer {
// ...
public static void main(String[] args) {
try (ServerSocket ss = new ServerSocket(PORT)) {
// Accepts infinite clients.
while (true) {
try (Socket cs = ss.accept();
DataInputStream in = new DataInputStream(client.getInputStream())) {
boolean isClientOpen = true;
while (isClientOpen) {
try {
Message message = decode(in);
System.out.print("Message received: " + message.getMessage() + " id: " + message.getId());
} catch (IOException e) {
// The client may have closed the socket.
isClientOpen = false;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static Message decode(DataInputStream in) throws IOException {
Message message = new Message();
message.setMessage(in.readUTF());
message.setId(in.readInt());
return message;
}
}