-1

I have made a simple program to send and receive a packet from a SA:MP server, which contains information about the specified server. This works correctly when I send the packet with a program called "Packet Sender", but my Java program just doesn't want to work. This is the code I am currently using:

        DatagramSocket dataSocket = new DatagramSocket(iPort);
        dataSocket.setSoTimeout(iTimeout);
        byte[] bytepacket = szPacket.getBytes(Charset.forName("UTF-8"));
        byte[] buf = new byte[256];
        DatagramPacket recpacket = new DatagramPacket(buf, buf.length);
        DatagramPacket packet = new DatagramPacket(bytepacket, bytepacket.length, address, iPort);
        packet.setData(bytepacket);
        packet.setAddress(address);
        packet.setPort(iPort);
        dataSocket.connect(address, iPort);
        socketConnected = true;
        System.out.println("Local port: " + dataSocket.getLocalPort());
        System.out.println("Local IP: " + dataSocket.getLocalAddress());
        System.out.println("Remote port: " + dataSocket.getPort());
        System.out.println("Remote IP: " + dataSocket.getInetAddress());
        System.out.println("Recieve Timeout: "+iTimeout+" milliseconds.");
        dataSocket.send(packet);
        System.out.println("Sent packet: "+ packet.getData());
        dataSocket.receive(recpacket);
        String recieved = new String(recpacket.getData(), 0, recpacket.getLength());
        System.out.println("Recieved packet: "+ recieved);
        dataSocket.close();
        dataSocket.disconnect();

This is my debug:

Packet: SAMPÀßµai
Local port: 7777 
Local IP: /192.168.0.103
Remote port: 7777
Remote IP: samp.lawlessrp.com/192.223.24.181
Recieve Timeout: 5000 milliseconds.
Sent packet: [B@76dd3333
java.net.SocketTimeoutException: Receive timed out
at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.receive0(Unknown Source)
at java.net.AbstractPlainDatagramSocketImpl.receive(Unknown Source)
at java.net.DatagramSocket.receive(Unknown Source)
at lillyramcharan.blacksirrah239.tracker.main.<init>(main.java:199)
at lillyramcharan.blacksirrah239.tracker.launch.init(launch.java:17)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

In the debug "Packet" is the packet before it is converted into bytes. It is also missing one character due to it being an invalid ascii character (represented as CANCEL).

Here is a screenshot of the program I mentioned earlier, sending and returning the packet: http://puu.sh/6gRd5.png

EDIT: I have already tried disabling my firewall/allowing javaw (it is an applet) through my firewall, as well as packets with the port 7777 and 55056.

Any help would be appreciated! Thanks

user2672165
  • 2,986
  • 19
  • 27
user1338675
  • 61
  • 1
  • 3
  • 8
  • You talk about two different ports, yet your code uses exactly one. What port are you sending to on the server, and what port is the server sending the reply to, exactly? – Brian Roach Jan 11 '14 at 08:17
  • SAMP uses port 7777, so that is the port I was sending to/from. – user1338675 Jan 11 '14 at 09:35
  • why you use packet.setData, packet.setAddress methods? data and address and other data already defined in new DatagramPacket(bytepacket, bytepacket.length, address, iPort). Isn't it? – Valeriy K. Feb 13 '20 at 10:01

1 Answers1

1

There is nothing obviously wrong with your code, so you need to do some investigation.

For example, find and install a packet logger and use it to see if the packet is actually being send over the wire and if a reply packet is coming back from the server.


Actually, there is a possible clue in the output messages:

    Local IP: /192.168.0.103
    Remote IP: samp.lawlessrp.com/192.223.24.181

You have set up your program to listen on the IP address 192.168.0.103. That is a private IP address. But you are trying to talk to 192.223.24.181, which is a (notionally) public IP address. If 192.223.24.181 is not in your organization's network(s), the chances, it won't know how to route packets to your address ... and they will get silently dropped.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216