[英]Java - Client and Server communication issues
我正在嘗試創建一個簡單的Java聊天應用程序,但是遇到了一些問題。
這是下面兩個類的代碼:
Serveur.java:
import java.io.*;
import java.net.*;
class Serveur{
private String msgClient, msgServeur;
private ServerSocket serverSocket;
private DataOutputStream out;
private Socket socket;
private int port;
private BufferedReader in;
public Serveur() throws IOException{
System.out.println("Serveur OK...");
this.port = 21;
this.serverSocket = new ServerSocket(this.port);
while(true){
this.socket = serverSocket.accept();
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
this.out = new DataOutputStream(this.socket.getOutputStream());
this.msgClient = this.in.readLine();
System.out.println("Received: " + this.msgClient);
this.msgServeur = this.msgClient.toUpperCase() + '\n';
send(this.msgServeur);
}
}
public void send(String msg) throws IOException{
this.out.writeBytes(msg);
}
public static void main(String argv[]) throws Exception {
Serveur serveur = new Serveur();
}
}
Client.java:
import java.io.*;
import java.net.*;
class Client{
private String sentence;
private String modifiedSentence;
private int port;
private BufferedReader inUser;
private BufferedReader inServeur;
private Socket socket;
private DataOutputStream out;
public Client() throws UnknownHostException, IOException{
this.sentence = "";
this.modifiedSentence = "";
this.port = 21;
this.inUser = new BufferedReader(new InputStreamReader(System.in));
this.socket = new Socket("localhost", this.port);
this.out = new DataOutputStream(this.socket.getOutputStream());
this.inServeur = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
sentence = this.inUser.readLine();
this.out.writeBytes(sentence + '\n');
modifiedSentence = this.inServeur.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
this.socket.close();
}
public static void main(String argv[]) throws Exception{
Client client = new Client();
}
}
我有2個問題:
-客戶端發送一條消息,服務器以大寫形式返回相同的消息。 如何允許客戶端發送消息而無需立即關閉其套接字連接?
-服務器看起來不錯,但是當我有多個客戶端時,有多個連接的客戶端會發生什么變化嗎? 我如何向特定的客戶廣播消息和消息?
客戶端發送一條消息,服務器以大寫形式返回相同的消息。 我如何允許客戶端發送無限消息,並且在發送第一條消息后不破壞自身?
只是讓客戶使用循環
boolean quit = false;
while (!quit) {
sentence = this.inUser.readLine();
quit = sentence.trim().equals("quit");
this.out.writeBytes(sentence + '\n');
modifiedSentence = this.inServeur.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
}
this.socket.close();
服務器看起來還可以,但是當我有多個客戶端時,多個客戶端之間會有區別嗎? 我該如何播放? 向重點客戶發送消息?
是的,該服務器阻止讀取第一個客戶端,因此任何其他客戶端都必須等待。 您可能希望將用於阻塞的代碼放在單獨的線程中,或將其提交給ExecutiveService。 而且,特定於客戶端的所有變量都不能位於單個服務器類中,因為您需要為每個客戶端設置一組變量。
while (true) {
final Socket socket = serverSocket.accept();
new Thread(() -> {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
boolean quit = false;
while (!quit) {
String msgClient = in.readLine();
quit = msgClient.trim().equals("quit");
System.out.println("Received: " + msgClient);
String msgServeur = msgClient.toUpperCase() + '\n';
out.writeBytes(msgServeur);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.