0

I have multiple transmitters configured to send back a response when they receive a broadcast packet sent from a server through local port 5255, remote port 5252 containing the string "AST show me\0" (as stated in transmitters' manual). This should help me to scan for all the transmitters within the local network. I have implemented a server side code to broadcast that string message, but when I run the code , it have a bug at the following line code :

  socket.receive(packet1);

I don't really know what I am doing wrong or missing in this code. Any help will be greatly appreciated.

Note: If it might help : The transmitters' IP address are from 192.168.40.* ; and the server IP address is 192.168.40.254.

Thanks in advance !!

Here is the code :

package socket_test;

import java.io.*;
import java.net.*;


public class Server_UDP_Broadcast {

//@SuppressWarnings("null")
public static void main(String[] args) {
    // TODO Auto-generated method stub

    DatagramSocket socket = null;

    try{
        socket = new DatagramSocket(5255);
        socket.setSoTimeout(10000);

        String str = "AST show me\0";
        byte[] buf = new byte[256];
        buf = str.getBytes();
        InetAddress group = InetAddress.getByName("255.255.255.255");
        DatagramPacket packet;
        packet = new DatagramPacket(buf, buf.length, group, 5252);
        socket.send(packet);


        // receive answer and display
        byte[] buf1 = new byte[1024];
        DatagramPacket packet1 = new DatagramPacket(buf1, buf1.length); 
        socket.receive(packet1);   
        String received = new String(packet1.getData(), 0, packet1.getLength());
        System.out.println("Answer from I-Lite :: " + received);          

    }
    catch (IOException e) {
        e.printStackTrace();
    }

}

}

Here are the bugs :

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 socket_test.Server_UDP_Broadcast.main(Server_UDP_Broadcast.java:31)

Here is the screenshots from from wireshark:

Captured packet's details

WireShark's Screenhot

Gerko Ford
  • 41
  • 2
  • 12
  • 1
    It looks like you are simply not receiving a packet within 10 seconds. You might want to sniff your network using WireShark or tcpdump. – Kenney Dec 02 '15 at 16:51
  • Sorry but I am newbie in networking, I have just installed wireshark. While sniffing the network, what should I look for which might give me a clue about the possible problem ? btw the transmitters and the server are all connected via a switch. – Gerko Ford Dec 03 '15 at 09:02
  • When you're capturing, enter this into the *filter* field: `udp and (udp.port==5255 or udp.port==5252)`; then you'll see the packets the server and clients send. You might see your server sending a packet, but no packets from the client. I'm not sure what the clients are - did you program them yourself? If possible you can also run WireShark on the client to see if the server packet arrives (but that should work, even with a switch). – Kenney Dec 03 '15 at 15:10
  • I have just updated the question with the screenshots from WireShark. Actually the clients are already fully programmed transmitter with IP addresses and other parameters already set. I am just using the manual released by the manufacturer to program the server side in order to communicate/interact with the transmitter. – Gerko Ford Dec 03 '15 at 17:48
  • The UDP packet in your 2nd screenshot is missing the trailing `\0`; when I run your code and capture the packet the payload is 12 bytes, including the trailing `\0`. Are you sure you're running the posted code? Before you send the packet, do `for (byte b : packet.getData()) System.out.println("BYTE " + b);` - does it print `BYTE 0` as the last line? – Kenney Dec 03 '15 at 18:29
  • I am sorry, when testing the code, I was playing around with all string composition alternatives. So I happened inadvertently to upload the screenshots of the captures without the trailing \0 instead. But I have updated the files and I have now got 12 bytes payload. you can now have a look not. Thanks for your help ! – Gerko Ford Dec 04 '15 at 07:45
  • That's what I thought, just checking! If you ping the transmitters, do they respond? Your Java code seems fine - it looks as if the transmitters are simply not responding, perhaps they don't recognize the packet. You could also try not sending a broadcast, but target one specific IP, to see if that works. If there really is a switch, it should forward broadcasts; if it is a router, by default it doesn't; in that case you might need to use multicast. Btw, do you have a link to the manual? – Kenney Dec 04 '15 at 16:27
  • When I ping the transmitters , they respond well. I also tried targeting one specific IP, but still nothing . Btw I am using a switch all the time. I would appreciate if you could send me a link to the manual about multicast :) I wanna try that and see what will happen – Gerko Ford Dec 04 '15 at 17:12
  • Here's some [general info](https://en.wikipedia.org/wiki/IP_multicast), here's [how to do it in Java](https://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html), here is [the RFC](https://www.ietf.org/rfc/rfc1112.txt). But, you will need to verify *if* your devices join a multicast group. You can [monitor in WireShark](http://stackoverflow.com/questions/11400046/wireshark-filter-by-multicast-in-gui) or perhaps there's something in the elusive I-Lite transmitter manual. But first, you could maybe make a normal Java UDP client that listens for your packets and responds? – Kenney Dec 04 '15 at 17:55

0 Answers0