Java 客户端混合 TCP 和 UDP 功能

标签 java networking client-server tcpclient udpclient

我正在用 Java 编写一个客户端-服务器应用程序,客户端和服务器可以通过 UDP 套接字相互交互,通过 TCP 套接字进行乒乓球交互,并且用户(客户端)可以相互聊天通过 TCP 套接字。

我有两个独立的 TCP 和 UDP 线程。我将服务器的 TCP 和 UDP 功能融合在一起(只需启动 TCP 和 UDP 线程)。但我该如何为客户做到这一点呢?

我在这里浏览了以下页面:

1) 这家伙为两个单独的客户端隔离了 TCP 和 UDP:Java TCP and UDP echo in one server

2) 线程 Can two applications listen to the same port?表明我不能对同一个客户端同时使用 TCP 和 UDP 来处理同一个端口号。这让我们...

3) ...本页:Listening for TCP and UDP requests on the same port 。但是,它没有建议任何有关如何实现客户端的信息。另外,他有一个线程,而不是客户端(奇怪)从服务器接收数据包并将其发送到服务器。

我一直在网络上浏览(特别是 Google),但没有想到任何东西。我相信很少有人(如果有的话)解决这个问题。

所以,我的问题又是:如何在客户端中融合 TCP 和 UDP 功能?我希望服务器能够通过 TCP 和 UDP 套接字与客户端连接,并在客户端之间使用 TCP。我不知道下一步该做什么。

感谢任何帮助。提前致谢。

==============================================

这是我当前形式的客户端代码:

        String hostName = args[0];
        int portNumber = Integer.parseInt(args[1]);

        MulticastSocket udpSocket = new MulticastSocket(4446);
        InetAddress address = InetAddress.getByName("230.0.0.1");
        udpSocket.joinGroup(address);

        DatagramPacket packet;

        // UDP: get a few quotes
        for (int i = 0; i < 5; i++) {

            byte[] buf = new byte[256];
            packet = new DatagramPacket(buf, buf.length);
            udpSocket.receive(packet);

            String received = new String(packet.getData(), 0, packet.getLength());
            System.out.println("Quote of the Moment: " + received);
        }

        udpSocket.leaveGroup(address);
        udpSocket.close();

        // TCP

        try (
            Socket tcpSocket = new Socket(hostName, portNumber);
            PrintWriter out = new PrintWriter(tcpSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                new InputStreamReader(tcpSocket.getInputStream()));
        ) {
            BufferedReader stdIn =
                new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String fromUser;

            // AS LONG AS server and client are interacting:
            while ((fromServer = in.readLine()) != null) {
                System.out.println(fromServer);
                if (fromServer.equals("Bye."))
                    break;

                fromUser = stdIn.readLine();
                if (fromUser != null) {
                    //System.out.println(fromUser);
                    out.println(fromUser);
                }
            }
        } catch (UnknownHostException e) {
            System.err.println("Unknown host: " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Unable to find I/O for connection to " +
                hostName);
            System.exit(1);
        }

在这种情况下,客户端在 UDP 连接中正确接收数据报,然后按预期切换到 TCP 功能。但有两个问题:

1)一旦输入正确的输入,服务器就无法退出监听循环(我硬编码为“退出”命令)。

2)服务器和客户端只会维持一次UDP连接,然后切换到TCP。

最佳答案

我在这里无法确定实际问题,但是:

2) The thread Can two applications listen to the same port? suggests that I can't use both TCP and UDP for the same port number at the same time for the same client.

它没有说任何这样的话。它表示不能将两个 TCP 套接字绑定(bind)到同一端口。无论如何,事实是您可以。

Which brings us to ...

3) ... this page: Listening for TCP and UDP requests on the same port.

有一个反例可以证明这一点。

However, it does not suggest anything on how to implement the client.

比如?您只需创建一个 TCP 或 UDP 套接字,然后将 TCP 套接字连接到远程 IP:端口,或者仅使用 UDP 套接字发送到该 IP:端口。我不明白这里有什么问题。

Plus, he has a thread, not a client (weird) receiving packets from and sending them to the server.

所以线程是客户端。没什么奇怪的。只需提取代码并将其放入程序中即可。 瞧:一个客户端程序。

how can I have the server listen to both TCP and UDP requests from the same client?

只需创建一个新的 ServerSocket(port)对于 TCP 和 new DatagramSocket(port)对于 UDP 并有一个线程监听每个。 TCP 线程应循环调用 accept()并为每个接受的 Socket; 生成一个新线程UDP 线程可以在 DatagramSocket.receive(). 上循环

关于Java 客户端混合 TCP 和 UDP 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19914692/

相关文章:

java - 同步Android表面 View

Java 命令行界面 : having multiple progress bars on different lines using '\r'

java - Applet 无法运行并崩溃 7u55 插件在 chrome、Firefox 和 IE 中工作正常

c# - 网络延迟测试仪

iphone - iOS 客户端/服务器 - 我应该使用 GameCenter 吗?

xml - 六个不同的更新流是如何工作的?

Java ObjectOutputStream 的方法flush()

java - 测试 RMI 服务

linux - 在同一台机器上配置 netem

java - 将客户端注册到服务器 - java 套接字编程