java中的tcp会话

发布于 2024-10-08 14:15:03 字数 132 浏览 1 评论 0原文

我想一次将多个客户端连接到服务器,并将服务器与所有客户端进行通信。 服务器如何识别每个客户端。以及如何向特定客户端发送数据?

考虑一下,有 3 个客户 A、B、C。所有客户端都连接到服务器。服务器想要向 B 发送消息。它是如何完成的?

i want to connect morethan one client at a time to the server and also communicate server to all the clients.
how server recognize each client. and how to send data to a particular client?

consider , there are 3 clients A,B,C. all the clients are connected to the server. the server wants to send message to B. how its done ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余厌 2024-10-15 14:15:08

如果我理解正确的话 - 您所需要的不是为一个连接绑定套接字。
您的客户端代码将如下所示:

客户端类:

public class TCPClient {

 public TCPClient(String host, int port) {

        try {
            clientSocket = new Socket(host, port);
        } catch (IOException e) {
            System.out.println(" Could not connect on port: " + port + " to " + host);
        }
}

服务器(主机)类:

   public class TCPListener {

    public TCPListener(int portNumber) {
            try {
                serverSocket = new ServerSocket(portNumber);
            } catch (IOException e) {
                System.out.println("Could not listen on port: " + portNumber);
            }
            System.out.println("TCPListener created!");
                    System.out.println("Connection accepted");
            try {
                while (true) {
                    Socket clientConnection = serverSocket.accept();

    //every time client's class constructor called - line above will be executed and new connection saved into Socket class.
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

这是最简单的示例。更多内容可以在这里找到:
http://www.oracle.com/technetwork/java/socket-140484.html

If i understand you right - all you need is not bind socket for one connection.
Your client code will looks like that:

Client class:

public class TCPClient {

 public TCPClient(String host, int port) {

        try {
            clientSocket = new Socket(host, port);
        } catch (IOException e) {
            System.out.println(" Could not connect on port: " + port + " to " + host);
        }
}

Server(host) class:

   public class TCPListener {

    public TCPListener(int portNumber) {
            try {
                serverSocket = new ServerSocket(portNumber);
            } catch (IOException e) {
                System.out.println("Could not listen on port: " + portNumber);
            }
            System.out.println("TCPListener created!");
                    System.out.println("Connection accepted");
            try {
                while (true) {
                    Socket clientConnection = serverSocket.accept();

    //every time client's class constructor called - line above will be executed and new connection saved into Socket class.
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

That is simplest example. More can be found here:
http://www.oracle.com/technetwork/java/socket-140484.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文