客戶端A如何向客戶端B和B發送消息,如何向服務器S發送消息?還有如何在A和B之間進行通信?我試過並解決了雙方(即單個客戶端和單個服務器)的問題。我嘗試過以下方法:在Socket編程中,A,B和S如何在java中進行通信?
//Server
public class DateServer {
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9090);
try {
while (true) {
Socket socket = listener.accept();
System.out.println("Waiting for client on port " +
listener.getLocalPort() + "...");
try {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
} finally {
socket.close();
}
}
} finally {
listener.close();
}
}
}
//Client
public class DateClient {
public static void main(String[] args) throws IOException {
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
Socket s = new Socket(serverAddress, 9090);
BufferedReader input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
//System.exit(0);
System.out.println(answer);
}
}
java socket編程中客戶端之間的通信如何進行。但我不明白A,B和S之間的通信是如何發生的。我嘗試了太多但沒有成功。我在等待最佳答案
在這種情況下,「B」應該是你的服務器,「A」和「C」應該是你的客戶端 –
套接字用於將數據從一個地方發送到另一個地方。由於有幾乎無限的可能的實現和設計,因此您需要制定出如何實現目標的最佳方案。當您遇到特定問題時,請隨時返回並詢問相關問題。 –
我的主要問題是客戶端向客戶端B發送消息,然後B從A發送消息並將其消息發送到服務器S.還發送消息給A。這是我必須實現的架構。 – Ganesh