繁体   English   中英

套接字未收到消息

[英]Socket does not receive messages

我编写了一个简单的客户端和简单的udp服务器,需要从特定端口读取字符串消息。 这是UDP套接字:

public class UDPServer {

    // boolean variable defines if the infinite loop
    // in startServer() runs or not
    private boolean isSwitched = false;
    private DatagramSocket socket = null;

    public UDPServer(int port) throws SocketException {     
        socket = new DatagramSocket(port);      
        Logger.getLogger(Main.class.getName()).log(Level.INFO, "Server started! Bound to port: " + port);
    }
    //this method start the server and switches on the infinite loop to
    // listen to the incoming UDP-packets
    public void startServer() throws IOException {      
        this.isSwitched = true;
        Logger.getLogger(Main.class.getName()).log(Level.INFO, "Server starts listening!");     
        while (isSwitched) {            
            byte[] size = new byte[30];
            DatagramPacket dp = new DatagramPacket(size, size.length);
            try {       
                System.out.println("Debug: receive loop started!");
                socket.receive(dp);
                System.out.println("Debug: Packet received after socket.receive!");
                Thread requestDispatch = new Thread(new Request(dp.getData()));
                requestDispatch.start();
            } catch (SocketException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.INFO, "Stops listening on specified port!");
            }           
        }           
    }

    // this method stops the server from running
    public void stopServer() {
        this.isSwitched = false;
        socket.close();
        Logger.getLogger(Main.class.getName()).log(Level.INFO, "Server is shut down after last threads complete!");
    }

}

我将其部署在远程服务器上并打开程序。 服务器打印出它开始监听,因此到达socket.receive()阶段。 然后,我从远程客户端发送UDP消息。 但是什么也没发生。 udp服务器不会继续前进-它只是保持状态,似乎没有收到任何消息。 我尝试使用tcpdump调试端口,它显示消息到达所需的端口。 但Java程序似乎未收到它们。 当我在远程服务器上发出此命令时:

tcpdump udp port 50000 

并发送一些数据包,这就是它写的:

12:53:40.823418 IP x.mobile.metro.com.42292 > y.mobile.metro.com.50000: UDP, length 28
12:53:43.362515 IP x.mobile.metro.com.48162 > y.mobile.metro.com.50000: UDP, length 28

我使用netcat在本地测试了您的服务器代码,并且工作正常,因此问题必须出在其他地方。 确定要发送UDP数据包吗? 您是否在远程服务器上运行tcpdump? 否则,您的数据包可能会被过滤。

好的,问题解决了。 问题是:

Red Hat Linux上的防火墙,我已成功关闭了所需端口的电源。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM