开发者

Sometimes the packets are only transmitted after the process is ended? [Java]

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 bein开发者_StackOverflow社区g transmitted when a process is stopped?


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜