[英]Java - Socket Programming - How can I make a client receive msg from multiple servers?
我需要讓多個客戶端與多個服務器對話並處理它們的響應。
到目前為止,我已經能夠編寫綁定到多個客戶端的服務器代碼(為每個客戶端生成一個線程),並且客戶端連接到多個服務器。
我遇到問題的地方是客戶端-我無法從服務器接收響應。
操作順序如下-
假設我有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
我面臨兩個問題:
out.write(“收到您的消息” + message.toString());
問題是方法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();
來自文檔 :
讀取一行文本。 一行被認為由換行符('\\ n'),回車符('\\ r')或回車符后緊跟換行符之一終止。
但是服務器既不發送\\ n也不發送\\ r。 嘗試
out.write("Got your message " + message.toString() + "\n");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.