Datagram socket problem

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

Got a bug somewhere...
Is this bit of code going to cause problems ie the bit running in the
loop? I find that the code never exits ds.receive(incoming)


try
{
DatagramSocket ds = new DatagramSocket(Constants.LOCAL_PORT);
DatagramPacket incoming = new DatagramPacket(receiveBuffer,
receiveBuffer.length);
incoming.setLength(60000);


while(true) //Run this as an endless loop
{
ds.receive(incoming);
packetStr = new String(receiveBuffer, 0, incoming.getLength(),
"UTF-8");
if (packetStr != null) BlinkAPI.update(packetStr);
}
} catch (IOException e1) {}
 
M

Martin Gregorie

[...]
I don't see why you're seeing the DatagramPacket length [...]

Er, "setting", not "seeing". Sorry if that was confusing.

Maybe I'm stupid, but I don't see any way out of the inner 'while(true)'
loop. If you're relying in an Exception to escape, at least document that
in a comment.
 
D

Dirk Bruere at NeoPax

On 3/27/11 2:12 PM, Peter Duniho wrote:
[...]
I don't see why you're seeing the DatagramPacket length [...]

Er, "setting", not "seeing". Sorry if that was confusing.

Maybe I'm stupid, but I don't see any way out of the inner 'while(true)'
loop. If you're relying in an Exception to escape, at least document that
in a comment.

I don't think you're stupid. Actually, I'm pretty certain you're not. :)

Short of shutting down the network adapter, I don't see any way in that
code to force an exception (e.g. closing the socket). Presumably there's
code elsewhere to do the ugly "abort thread" thing. Or maybe it's a
daemon thread and the OP is planning to just let it run indefinitely.

Not my preferred means of managing i/o threads (well, okay…actually a
daemon thread isn't all _that_ terrible), but I figured better to focus
on correctness first. :)

Pete

Yes, it's running in a thread of its own that lasts as long as the app
is active.

The problem might be with the Android emulator.
I can send packets but I'm not getting anything back.
The Windows firewall is open on the correct port.
 
E

Esmond Pitt

Another problem with this code is that you need to set the packet length
every time around the loop, before you read: not just once. Otherwise
the datagram shrinks to the length of the smallest received packet.
 
D

Dirk Bruere at NeoPax

Another problem with this code is that you need to set the packet length
every time around the loop, before you read: not just once. Otherwise
the datagram shrinks to the length of the smallest received packet.

Thanks
 
M

Mike Schilling

Peter Duniho said:
[...]
The problem might be with the Android emulator.
I can send packets but I'm not getting anything back.
The Windows firewall is open on the correct port.

Well, you can always implement each other end in a different way (e.g.
C++/Winsock receiver, and a plain, non-Android/non-emulated Java sender)
to double-check the work of each.

At least, that's what I'd do in terms of diagnosis.

I'll also suggest using Wireshark, but frankly very time I use it I find
myself muttering to myself about how awkward it is to use. It's powerful,
but hardly simple (and I never have gotten it to work properly on my Mac).
For most network issues I've had to deal with (i.e. never really anything
all that complicated), simple test apps have always sufficed to identify
the issue, whether they are in my own code or in some other component I'm
using.


I've only had to use Wireshark's full power once, but it was invaluable.
The issue was HTTP requests being processed twice. The problem turned out
to be that different sides of an HTTP connection had very different
keepalive and timeout settings. The result was (from memory, so probably
not exact):

Sender sends a request on a reused TCP connection
Receiver takes a long time to acknowledge the request
Sender decides the TCP connection is dead, creates a new one and uses it to
resend the request
Receiver processes both requests

I might have figured it out eventually, but looking at the packets going
back and forth made it pretty obvious.
 
D

Dirk Bruere at NeoPax

Got a bug somewhere...
Is this bit of code going to cause problems ie the bit running in the
loop? I find that the code never exits ds.receive(incoming)


try
{
DatagramSocket ds = new DatagramSocket(Constants.LOCAL_PORT);
DatagramPacket incoming = new DatagramPacket(receiveBuffer,
receiveBuffer.length);
incoming.setLength(60000);


while(true) //Run this as an endless loop
{
ds.receive(incoming);
packetStr = new String(receiveBuffer, 0, incoming.getLength(), "UTF-8");
if (packetStr != null) BlinkAPI.update(packetStr);
}
} catch (IOException e1) {}

Nothing wrong in general (apart from the bug picked up by Esmond).
As soon as I tried it on real h/w instead of the emulator it worked fine
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,126
Latest member
FastBurnketoIngredients
Top