【问题标题】:Java - Client and Server communication issuesJava - 客户端和服务器通信问题
【发布时间】:2016-02-12 09:37:51
【问题描述】:

我正在尝试创建一个简单的 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();
    }
}

我有两个问题:
- 客户端发送消息,服务器返回相同的大写消息。如何允许客户端在不立即关闭其套接字连接的情况下发送消息?
- 服务器看起来不错,但是当我有多个客户端时,连接多个客户端会有什么变化吗?如何向特定客户端广播消息和消息?

【问题讨论】:

    标签: java tcp server client


    【解决方案1】:

    客户端发送消息,服务器返回相同的消息 大写。我怎样才能让客户端发送无限的消息和 发送第一条消息后不要自行销毁?

    让客户端使用循环

    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();
    }
    

    【讨论】:

    • 感谢大家,但是当我将 wile true 循环放入客户端时,会出现此错误(eclipse)“this.socket.close();”是无法到达的。那么我必须删除这一行吗?当我启动程序时,客户端可以发送他的第一条消息,但第二条消息永远不会发送(或服务器永远不会读取)......你知道原因吗?
    • 您应该对客户进行某种测试,以表明他们何时想退出。
    • 是的,如果服务器说“lol”,我装饰客户端。但他不能发送多条消息(客户端)
    • 服务器也需要相应的内循环。
    • 工作中,感谢您的帮助,对我的不理解表示抱歉:P
    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多