繁体   English   中英

Java-客户端和服务器通信问题

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM