简体   繁体   English

Java:如何从一个端口上的客户端套接字连接到不同的服务器?

[英]Java: how to connect to different servers from a client socket on one port?

A TCP connection is identified by four elements(maybe protocol included): client port, client address, server port, server address. TCP连接由四个元素(可能包括协议)标识:客户端端口,客户端地址,服务器端口,服务器地址。 So it's possible for a client with one port to connect to multiple different servers.Because they're different TCP connections. 因此,具有一个端口的客户端可能会连接到多个不同的服务器,因为它们是不同的TCP连接。

Here's my demo: a local client on port 9999 conncecting to two local servers on port 12345 and port 12346. But the code isn't correct. 这是我的演示:端口9999上的本地客户端连接到端口12345和端口12346上的两个本地服务器。但是代码不正确。

Can anybody help me? 有谁能够帮助我? Tell me how to correct it, please.(not use SO_REUSEPORT or fork) 请告诉我如何更正。(请勿使用SO_REUSEPORT或fork)

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketTest {

    public static void main(String[] args) throws IOException {


        startServer(12345);
        startServer(12346);

        Socket socket = new Socket();
        socket.bind(new InetSocketAddress(9999));
        System.out.println("client: " + socket.getLocalSocketAddress().toString());

        startClient(socket, 12345);
        startClient(socket, 12346);
    }

    public static void startClient(Socket socket, int port) {
        (new Thread() {
            @Override
            public void run() {
                try {
                    // Problem: connect() can be called only once
                    socket.connect(new InetSocketAddress(port));
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void startServer(int port) {
        new Thread() {
            @Override
            public void run() {
                ServerSocket ss;
                try {
                    ss = new ServerSocket(port);
                    System.out.println("listen on: " + ss.getLocalSocketAddress());
                    while (true) {
                        Socket s = ss.accept();
                        System.out.println("accept from: " + s.getRemoteSocketAddress());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

You can bind 2 different sockets to the same local port (that is what SO_REUSEADDR is made for), but one single socket can only be connected to one single destination. 您可以将2个不同的套接字绑定到同一个本地端口(这就是SO_REUSEADDR的用途),但是一个单独的套接字只能连接到一个单独的目的地。

If you want to send one single message, and you want that message to be received by two servers, then you need multicast. 如果要发送一条消息,并且希望该消息被两台服务器接收,则需要多播。 multicast can only use UDP (not TCP) and one of the special multicast addresses (224.0.0.0 to 239.255.255.255) that isn't listed as reserved by IANA. 多播只能使用UDP(非TCP)和IANA未列出为保留的特殊多播地址之一(224.0.0.0至239.255.255.255)。

暂无
暂无

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

相关问题 我如何将Java套接字客户端连接到多个服务器 - How do i connect java socket client to multiple servers 如何在特定端口上搜索响应式套接字服务器(如何在给定端口但没有其ip的情况下连接到服务器)? - How to search for responsive socket servers on a particular port (How to connect to a server given the port but not having its ip)? Java SSL套接字无法从客户端连接 - Java SSL socket cannot connect from client Java-套接字编程-如何使客户端从多个服务器接收消息? - Java - Socket Programming - How can I make a client receive msg from multiple servers? Java 套接字客户端端口转发 - Java socket client port forwarding 如何使用 IP 地址和端口号连接到服务器套接字? (客户端运行在与服务器不同的机器上) - How do I connect to the server socket using the ip address and port number? (client is running on a different machine than server) 如何从Android Tcp客户端套接字连接到具有公共IP的Java TCP服务器套接字? - How to connect to a Java TCP server socket having Public IP From Android Tcp Client Socket? 如何使用套接字将多个服务器连接到Java客户端? - How to connect multiple servers to a client in Java using Sockets? 需要在两个不同端口上运行的Java ServerSocket和Socket(Client)之间进行连接 - Need to connect between a Java ServerSocket and Socket(Client) running on 2 different ports 如何在Java中为Socket客户端获取动态端口号 - How to get the dynamic port number in Java for Socket client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM