Artem Moskalev
Artem Moskalev

Reputation: 5818

Socket does not receive messages

I have written a simple client and simple udp server that needs to read string messages from particular port. here is the UDP-socket:

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!");
    }

}

I deploy it on the remote server and switch on the program. The server prints out that it started listening so it reaches the socket.receive() stage. Then I send a UDP-message from a remote client. But nothing happens. The udp-server does not move any further - it justs holds and seems to receive no messages. I tried to debug the ports with the tcpdump and it shows that messages come to the required port. but java program does not seem to receive them. When I issue this command on the remote server:

tcpdump udp port 50000 

and send a few packets thats what it writes:

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

Upvotes: 0

Views: 1249

Answers (2)

Artem Moskalev
Artem Moskalev

Reputation: 5818

Ok, question resolved. The problem was:

FIREWALL on Red Hat linux, which I successfully switched off for the required port.

Upvotes: 1

R2-D2
R2-D2

Reputation: 1624

I tested your server code locally with netcat and it works just fine, so the problem has to be somewhere else. Are you sure you're actually sending UDP packets? Did you run tcpdump on the remote server? When not, maybe your packets get filtered.

Upvotes: 1

Related Questions