繁体   English   中英

使用套接字连接的简单聊天

[英]Simple Chat using socket connection

我正在尝试进行简单的聊天。 我有一台可以处理更多客户端的服务器。 这是我到目前为止所做的事情:建立连接后,我可以在服务器上确定哪个客户端正在发送消息,但是问题是在客户端上我无法像在服务器上那样进行完整的对话。例如

Client_1  (writing)
msg : hi

Client_2 (writing)
msg : hi from cl2

results 

Server :
Client_1 : hi
Client_2 : hi from cl2

Client_1 
Client_1 : hi

Client_2 
Client_2 :hi from cl2

我想实现两个客户端具有与服务器相同的信息。

My Server Script :
public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(1900);
        System.out.println("waiting");
        while (true) {
            Socket sock = server.accept();
            System.out.println("Client nou conectat !");
            ClientHandler cH = new ClientHandler(sock);
            cH.start();
        }
    }
}

class ClientHandler extends Thread {
    BufferedReader in;
    PrintWriter out;

    ClientHandler(Socket sock) throws IOException {
        out = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()),
                true);
        in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    }

    public void run () {
            try {
                out.println("Hello from server"); //hello message to client
        while (true) {
            String linie = in.readLine();
            if (linie==null)
                return;
            System.out.println(linie); //message from client 
            out.println(linie);
        }
    }catch(Exception e) {e.printStackTrace();}
        }
}

客户端脚本

public class Client {
    public static void main(String[] args) throws Exception {
    //  String ip ="192.168.1.12";
        String ip ="localhost";
        Socket sock = new Socket(ip, 1900); // localhost daca ii pe
                                                        // acelasi pc serverul
                                                        // si clientul,altfel
                                                        // IPul
        PrintWriter out = new PrintWriter(new OutputStreamWriter(
                sock.getOutputStream()), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                sock.getInputStream()));
        String linie = in.readLine();
        System.out.println("Server :" + linie);//meesage from server
        BufferedReader console = new BufferedReader(new InputStreamReader(
                System.in));

        while (true) {
            linie = console.readLine();
            out.println("Client_1 :"+linie); // sending to server
            String linie2 =in.readLine();
            System.out.println(linie2); //resend message to client
        }
    }
}

我希望我能很好地解释我的问题:D。 感谢大家的建议

在您的服务器端。

您为与服务器的每个客户端连接创建一个ClientHandler,这很好,然后读取来自每个ClientHandler的内容。

但是每个ClientHandler都不知道服务器上还有多少其他客户端,您必须“连接它们”。

我会创建一个

public class ClientDispatcher{

    private List<ClientHandler> clients;

}

并针对ClientHandler收到的每条消息通知ClientDispatcher,以便他可以将消息“分发”给其他人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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