gpt4 book ai didi

Java - 套接字编程 - 如何使客户端从多个服务器接收消息?

转载 作者:行者123 更新时间:2023-12-02 13:35:38 25 4
gpt4 key购买 nike

我需要让多个客户端与多个服务器通信并处理来自它们的响应。

到目前为止,我已经能够编写绑定(bind)到多个客户端(为每个客户端生成一个线程)的服务器代码,并且客户端连接到多个服务器。

我面临问题的地方是在客户端 - 我无法接收来自服务器的响应。

操作顺序如下 -

假设我有 2 个服务器和 1 个客户端。客户端连接到两台服务器,向它们发送消息,两台服务器都接收消息并向客户端发送回复 - 我无法收到此回复。

服务器代码 -

 @Override
public void run() {
try {

// create a serversocket to listen to requests
ServerSocket serverSocket = new ServerSocket(port);

// create n sockets to listen to 5 client
for (int i = 0; i < n; i++) {
Socket socket = serverSocket.accept();
// create a processor thread for each to read and process the incoming Messages
Processor processor = new Processor(socket);
processor.start();
}

serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}

服务器代码处的处理器 -

@Override
public void run() {
try {
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());

while (true) {
String str = in.readObject();

System.out.println(message);

out.write("Got your message " + message.toString());

}
} catch (IOException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (ClassCastException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("Processor completed " );
}

客户端代码 -

public static void main(String[] args) throws IOException, InterruptedException {

// make the connections with other nodes
connections = connect();

// connect() creates connections from the client to all servers and stores the socket and out objects in the object called Connections.Code omitted to avoid clutter

// process all the commands
while(!commands.isEmpty()){

for(int i=0 ; i<2; i++){
send(commands.poll() , i);

}

Thread.sleep(500);
}

}


// Sends Message m to the node i
public static synchronized void send(Message m, int i) {
try {
connections.outs[i].writeInt(m.nodeId);
connections.outs[i].writeInt(m.timestamp);
connections.outs[i].writeObject(m.type);
connections.outs[i].writeObject(m.value);
connections.outs[i].flush();

InputStreamReader isr = new InputStreamReader(connections.sockets[i].getInputStream());
final BufferedReader br = new BufferedReader(isr);

new Thread() {
public void run() {
try {
while (true) {
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}.start();

} catch (IOException e) {
e.printStackTrace();
}
}

我确信我在收听消息时做错了什么。任何有关如何接收和处理来自多个服务器的消息的建议都会非常有帮助。

TIA

最佳答案

我面临两个问题:

1。你没有冲水。

out.write("Got your message " + message.toString());

2。在服务器中您不发送\n

问题出在方法readLine

  new Thread() {
public void run() {
try {
while (true) {
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}.start();

来自Documentation :

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

但是服务器既不发送\n也不发送\r。尝试一下

  out.write("Got your message " + message.toString() + "\n");

关于Java - 套接字编程 - 如何使客户端从多个服务器接收消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43013701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com