如何使用套接字将多个服务器连接到Java中的客户端?

时间:2019-03-23 23:45:41

标签: java sockets tcp

我正在通过套接字(从单个客户端到多个服务器)在Internet上编写程序,但我尚未找到有关此信息

该系统由将多个服务器连接到单个客户端组成,客户端必须向用户请求一个数字,然后在该服务器中使用该数字执行算法,该算法将查找介于0到0之间的质数。 N之后,计算算法的执行时间,然后发送给客户端。每台服务器每次都发送给客户端,客户端将它们存储起来。

问题是,除了要同步此连接之外,在所需的服务器数量之间此连接必须同时进行。

目前,我已经编写了一个简单的代码,从客户端到服务器。

主服务器

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Server s = new Server(5000, "Hilo servidor");
    s.start();
    }

}

模块化服务器

这是服务器类内部的一种方法,该类执行算法以计算有多少表亲并将该时间发送给客户端。它在主服务器中调用c.start()时运行

private void startServer() {

            try {
                ServerSocket ss = new ServerSocket(port);
                System.out.println("Esperando Conexion");
                Socket socket = ss.accept();


                DataInputStream in = new DataInputStream(socket.getInputStream());
                int n = in.readInt();
                long time = encontrarPrimeros(n);
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                out.writeLong(time);
                System.out.println(time);
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
}

主客户端

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
            Client c = new Client("Hilo cliente", 5000, "192.168.0.19");
            c.start();
    }

}

模块化客户端

    private void startClient() {
            try {
                    Socket socket = new Socket(ip, port); 
                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());


                    System.out.println("Ingrese dato...");
                    Scanner scanner = new Scanner(System.in);
                    int n = scanner.nextInt();
                    out.writeInt(n);
                    DataInputStream in = new DataInputStream(socket.getInputStream());
                    long tiempo = in.readLong();
                    System.out.println(tiempo);
                    socket.close();
            } catch (IOException ex) {
                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

问题是,如何将多个服务器连接到客户端,以便它们开始同步?

谢谢

更新

由于这个原因,我修改了客户端的main方法:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Client c = new Client("Hilo cliente", 5000, "192.168.0.19");
    Client c2 = new Client("Hilo cliente", 5000, "192.168.0.19");
    c.start();
    c2.start();
}

}

但是执行时,出现以下错误:

mar 23, 2019 7:14:10 PM Client startClient
GRAVE: null
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.io.DataInputStream.readFully(DataInputStream.java:195)
    at java.io.DataInputStream.readLong(DataInputStream.java:416)
    at Client.startClient(Client.java:69)
    at Client.run(Client.java:41)
    at java.lang.Thread.run(Thread.java:748)

执行错误的指令如下:long tiempo = in.readLong();

1 个答案:

答案 0 :(得分:0)

实际上,在套接字编程中:

  • 客户端只能连接到一台服务器
  • 服务器可以接收许多客户端连接
  • 客户端通过套接字对象管理与服务器的连接

如果您的客户端需要连接到许多服务器,则客户端必须创建与服务器数量一样多的套接字对象。为了并行管理通信,您可以创建一个单独的线程,在该线程上管理每个套接字的IO。

因此适用于您的方案:

  • 您可以保留Client类,因为它可以管理一个套接字
  • 您在服务器数量中创建了客户端的N个对象(将每个客户端实例仅与一个服务器相关联)
  • 您可以使Client类可运行(通过从Thread类继承)。
  • 您将客户端作为线程运行(可以将startClient()类放在从Thread继承的受保护的void run()方法中。)
  • 在您的主机中,您调用每个客户端对象的start()方法(从线程继承)。这将在后台运行run()方法。
  • 要收集所有结果,请在每个Client对象上调用join()

客户端类示例

    public class Client extends Thread {

    private String ip;
    private int port;


    public Client(String serverIP, int serverPort) {
        this.ip = new String(serverIP);
        this.port = serverPort;
    }

    private void startClient() {
        try {
            Socket socket = new Socket(ip, port); 
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());

            System.out.println("Ingrese dato...");
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            out.writeInt(n);
            DataInputStream in = new DataInputStream(socket.getInputStream());
            long tiempo = in.readLong();
            System.out.println(tiempo);
            socket.close();
        } catch (IOException ex) {
            ex.getStackTrace();
        }
    }

    @Override
    public void run() {

        startClient();

        super.run();
    }
}

主要

Client client1 = new Client("192.168.1.100", 8888);
    Client client2 = new Client("192.168.1.101", 8888);
    Client client3 = new Client("192.168.1.102", 8888);

    client1.start();
    client2.start();
    client3.start();

    try {
        client1.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    try {
        client2.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    try {
        client3.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

服务器类

public class Server extends Thread {


ExecutorService threadPool = Executors.newCachedThreadPool();

//Private class
class ClientSocket implements Runnable {

    private Socket m_socket;

    ClientSocket(Socket sock) {
        m_socket = sock;
    }

    @Override
    public void run() {
        try {
            DataInputStream in = new DataInputStream(m_socket.getInputStream());
            int n = in.readInt();
            long time = n;
            DataOutputStream out = new DataOutputStream(m_socket.getOutputStream());
            out.writeLong(time);
            out.flush();
            System.out.println(time);
            System.out.flush();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}


@Override
public void run() {
    startServer();
    super.run();
}

private void startServer() {

    try {
        ServerSocket ss = new ServerSocket(8888);
        System.out.println("Esperando Conexion");

        do {
            Socket socket = ss.accept();

            threadPool.execute(new ClientSocket(socket));

        } while(true);

    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}