Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Morning.

I'm pretty new in Java and socket connections but I'm trying to send out a UDP packet/broadcast on 255.255.255.255 on port 8001 to a device. I can get the data to send just fine, however when it comes time to receive the data the connection times out. I have a packet sniffer and I can see the packet send and then the device respond.

I'm pretty sure it is a rookie mistake that I'm missing in my code but I've been stuck on it for awhile and any help would be appreciated.

 m_Socket = new DatagramSocket(m_SERVERPORT);
 InetAddress address = InetAddress.getByName(m_SERVERIP);


 m_DataPack = new DatagramPacket(m_SERVERCMD.getBytes(), m_SERVERCMD.getBytes().length,
 address, m_SERVERPORT);
 m_Socket.setBroadcast(true);
 m_Socket.connect(address, m_SERVERPORT);

 m_Socket.send(m_DataPack);
 m_DataPack = new DatagramPacket(data, data.length,
 address, m_SERVERPORT);


 m_Socket.receive(m_DataPack); // This is where it times out


 data = m_DataPack.getData();
 String received = data.toString();
 System.out.println("Received: " + received);
 m_Socket.close();

Thanks and Gig'Em.

EDIT:

I'm not sure if this helps but when I watch the m_Socket object I can see the following right before it sends:

bound = true;
close = false;
connectedAddress = Inet4Address (id = 32) (-1,-1,-1,-1);
connectedPort = 8001;
connectState = 1;
created = true;
impl = PlainDatagramSocketImpl;
oldImpl = false;

and the m_DataPack object is the following:

address = Inet4Address (id = 32) (-1,-1,-1,-1);
bufLength = 6 (size of packet I'm sending is 6 char long);
offset = 0;
port = 8001;
share|improve this question
    
You do realize that your Gig'Em alienates some graduates of one of the larger CS programs in America, right? –  jasonmp85 Jun 2 '10 at 4:29

3 Answers 3

This doesn't make sense. You are broadcasting, which is 1-to-many, and you are also connecting, which is 1-to-1. Which is it?

Lose the connect. And lose the 255.255.255.255. This has been heavily deprecated for about 20 years. Use a subnet-local broadcast address, e.g. 192.168.1.255.

share|improve this answer

You can also take a look at MulticastSocket described at Broadcasting to Multiple Recipients. Hope this helps.

share|improve this answer

If you want to receive a datagram you need to bind() to the local endpoint (address + port).

share|improve this answer
    
In the bind function I need to set the local endpoint to an address and port through a SocketAddress. What is the best way to do this? I know in the DatagramPacket Class there is a getSocketAddress(); function so that doesn't help me. –  user355528 Jun 1 '10 at 15:05
    
I did try m_DataPack = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), m_SERVERPORT); and then the m_Socket.bind(m_DataPack.getSocketAddress()); but it throws an exception saying the socket has already been bound. –  user355528 Jun 1 '10 at 15:06
    
Thanks for the input by the way. –  user355528 Jun 1 '10 at 15:07
    
He is binding, that's what new DatagramSocket(int port) does. –  EJP Jun 2 '10 at 3:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.