Java IO / NIO:服务器可以监听多个端口吗?

时间:2014-03-06 10:35:07

标签: java tcp io

我正在研究Java IO / NIO。我已经为TCP和UDP(Java IO)实现了许多服务器/客户端应用程序,并且最近开始使用NIO。

我想写一个服务器(TCP或UDP),在收听多个端口时执行多个任务。我一直实现服务器一次监听一个端口,我的问题可能是最佳做法。但有没有办法让服务器可以接受多个端口上的客户端?像单个服务器中的多个服务器一样?

如果有可能,我不是在寻找代码,只是程序的概念/设计。

服务器:

public class ServerAll implements Runnable {

private Socket connection;

ServerAll(Socket s) {
    this.connection = s;
}

public static void main(String[] args) {
    int port1 = 7777;
    int port2 = 8888;
    int port3 = 9999;
    int count = 0;
    try{
        ServerSocket server1 = new ServerSocket(port1);
        ServerSocket server2 = new ServerSocket(port2);
        ServerSocket server3 = new ServerSocket(port3);

        System.out.println("Server Initialized");
        while (true) {
        Socket con1 = server1.accept();
        Runnable runnable1 = new ServerAll(con1);
        Thread thread1 = new Thread(runnable1);
        thread1.start();

        Socket con2 = server2.accept();
        Runnable runnable2 = new ServerAll(con2);
        Thread thread2 = new Thread(runnable2);
        thread2.start();

        Socket con3 = server3.accept();
        Runnable runnable3 = new ServerAll(con3);
        Thread thread3 = new Thread(runnable3);
        thread3.start();
        }
    }
    catch (Exception e) {}
}

public void run() {
    try {
        DataInputStream in = new DataInputStream(connection.getInputStream());
        String clientMsg = in.readUTF();
        String replyMsg = null;

        if(clientMsg.equalsIgnoreCase("1")){
            String msg = "Server Reply on 1";
            replyMsg=msg;
        }

        if(clientMsg.equalsIgnoreCase("2")){
            String msg = "Server Reply on 2";
            replyMsg=msg;
        }

        if(clientMsg.equalsIgnoreCase("3")){
            String msg = "Server Reply on 3";
            replyMsg=msg;
        }

        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.writeUTF(replyMsg);
    }
    catch (Exception e) {
        System.out.println(e);
    }
    finally {
        try {
            connection.close();
        }
        catch (IOException e){}
    }
}

客户端:

public class ClientTcpE {

public static void main(String[] args) {
    String host = "localhost";
    int port = 7777;
    System.out.println("SocketClient initialized");
    try {
        InetAddress address = InetAddress.getByName(host);
        Socket client = new Socket(address, port);
        System.out.println("Just connected to "+ client.getRemoteSocketAddress());
        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        out.writeUTF("1");
        System.out.println("Just sent msg to server");

        InputStream inFromServer = client.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        String serverMsg = in.readUTF();

        System.out.println("server says : "+serverMsg);
        client.close();
    }catch(IOException e){
        e.printStackTrace();
    }   
}

2 个答案:

答案 0 :(得分:1)

是的,确定。简单的方法是使用多个线程创建ServerSocket,绑定到端口,然后在其上调用accept

答案 1 :(得分:0)

您一次只接受一个端口,即accept()都在同一个线程中。您只为每个客户端处理一条消息。您编写代码的方式,客户端必须按顺序联系三个端口,重复。

如果要同时接受不同端口上的连接,则必须使用每个端口进行此阻塞操作(或使用非阻塞NIO)