tags:

views:

66

answers:

4

I have an application which opens a datagram sockets and sends to various other processes .... sometimes this application lauches another process (using ProcessBuilder) which also does some network communication...

Now, the joke is, the launched process will "sometimes" only recieve messages after the main application is terminated ... OR sometimes it will send to X but they will only be delivered when the main application is stopped...

I've got no clue what is going on ... anyone ever hear of something like this? Packets only being transmitted when a process is stopped?

+2  A: 

It's been a while that I've been programming with sockets in Java, but I do remember that you have to explicitly flush a socket to "force" all data to be send. This will be done upon closing the socket for you, which would explain your observed behaviour.

jawi
But I am not done sending ... should I .Close after every send the =new DataSocket before every send?
Shaitan00
@Shaitan: He said "flush" to force sending, not "close" - mention of close was only to say closing does a flush, and ending the JVM closes any open process handles.
Software Monkey
A: 

Have you looked at socket.setTcpNoDelay(true);?

This is an optimization that waits for a certain amount of data to collect before it sends it over the network. For applications sending sporadic and small amounts of data, this should be set to off.

EDIT: Sorry, I think this is only for non-datagram sockets. Probably not your problem.

z5h
The OP says he's using datagrams (I guess UDP). So I don't think this would have any effect.
Stephen C
A: 

One possibility is that this is a result of using datagrams. Datagrams are inherently unreliable with the possibility that packets may be dropped or delivered out of order.

It is also possible that your application is just terminating too quickly. Do the application and the launched process do a handshake to ensure that they both know when to shut down?

EDIT:

But actually, I think that the most likely cause is that the child process is blocking because the parent process does not read stuff written to the child's output ... until it it is too late to matter. As an experiment, add the following to the child process startup:

OutputStream os = new FileOutputStream(...); // pick a suitable temp filename
System.setOut(os);
System.setErr(os);

If this hack fixes things, it is a sure sign that this is the root problem.

Stephen C
How am I supposed to FLUSH it? There is no .flush() for DatagramSocket and I am not done with it ... but I would like for message to make it over as sent and not all in one huge bunch the the main application is killed ...
Shaitan00
A: 

Is the child process writing to System.out or System.err? Have you set up the main process to drain those streams once the child process is started? It could be that the child process is just deadlocked trying to print a log message on a blocking stream that isn't being read. When the parent app is killed, the stream gets unblocked and everything starts moving again. It may be a longshot, but I've been bitten by it enough times when starting child processes that I always check.

Dave Ray
Ya ... I found that one out the hard way ... it was causing my entire application to block ... but now ... I pass the .SEND and it just doesn't get delivered (they pile up somewhere) until the app is killed then they come in as a rush
Shaitan00