最好是开放不同的端口,或者使用标识符来区分(或其他方式)。

huangapple go评论73阅读模式
英文:

Is it better to open different ports or one port with identifiers (or else)?

问题

我正在编写一个系统,其中有三个不同的Java应用程序,它们通过TCP/IP进行交互。其中两个应用程序通过ServerSocket与另一个名为Directory的应用程序连接。

其中一个应用程序仅连接以登录并被添加到列表中,而另一个应用程序仅连接以请求列表或发送消息。

所有这些连接都通过Directory的ServerSocket上的相同端口进行。与Directory连接的应用程序通过套接字发送一个带有任务标识的字符串,Directory随后处理该标识以确定需要执行的操作。

阅读标识字符串的这种方法是否可行?它是否高效、可维护,或者应该使用其他方法?例如,为不同类型的客户端使用不同的端口,或者为不同功能使用不同的端口。目前只提到了这些功能,但可能会添加更多功能,因此我想知道这是否是可行的实现。

public class Directory {
    private ServerSocket server;
    public Directory() {
        super();
    }

    public void openServer(int port) throws IOException {
        new Thread() {
            public void run() {
                try {
                    server = new ServerSocket(port);
                    while (true) {
                        Socket socket = server.accept();
                        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String identifier = in.readLine();
                        if (identifier.equalsIgnoreCase("Connect")) {
                            connect(); // 存根
                        } else if (identifier.equalsIgnoreCase("NeedList")) {
                            giveList(list); // 存根
                        } else if (identifier.equalsIgnoreCase("SendMessage")) {
                            sendMessage(); // 存根
                        }
                    }
                } catch (IOException e) {
                    // 中断处理
                }
            }
        }.start();
    }
}
英文:

I'm coding a system in which there are three different java applications that interact with eachother via TCP-IP. Two of these apps connect with the other one, called Directory, through a ServerSocket.
One of the apps connects with it only to log in and be added to a list, while the other app connects with it only to ask for the list or to send a message.
These connections are all being done via the same port in the Directory's ServerSocket, the apps that connect with the Directory send a String through the socket with a sort of task-identifier slapped on the front, which the Directory then processes to know what it has to do.
Is this approach of reading identifier Strings ok? Is it efficient, maintainable, or should it be done in another way? e.g. having ServerSockets with different ports for different types of clients, or different ports for different funcionalities. The funcionalities mentioned are the only ones for the time being, but more may be added so I would like to know if this is a viable implementation.

public class Directory {
    private ServerSocket server;
    public Directory() {
        super();
    }
    
    public void openServer(int port) throws IOException {
        new Thread() {
            public void run() {
                try {
                    server = new ServerSocket(port);
                    while (true) {
                        Socket socket = server.accept();
                        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String identifier = in.readLine();
                        if (identifier.equalsIgnoreCase("Connect")) {
                            connect(); // stub
                        } else if (identifier.equalsIgnoreCase("NeedList")) {
                            giveList(list); // stub
                        } else if (identifier.equalsIgnoreCase("SendMessage")) {
                            sendMessage(); // stub
                        }
                    }
                } catch (IOException e) {
                    // interrupted
                }
            }
        }.start();
    }
}

答案1

得分: 0

如果所有应用程序使用相同的协议,基本上意味着只要所有应用程序使用相同的数据包结构,您可以使用一个端口,这样就没问题了。
使用一个端口的另一个好处是配置较少,比如说您有一个防火墙,您只需要打开这一个端口,如果有人试图在受限网络内连接,情况也是相同的。

如果应用程序使用不同的协议,最好使用三个端口。

英文:

If all apps use the same protocol, which basically just means as long as all apps use the same packet structure, you´re fine with going with one port.
Another pro using one port is that it´s less coinfiguration since, let´s say you have a firewall, you only have to open this one port and if someone tries to connect within a restricted network, the same applies.

If the apps use different protocols you´d be better off using three ports.

huangapple
  • 本文由 发表于 2020年5月4日 07:25:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61582956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定