EQvan
EQvan

Reputation: 397

Both ends of Java socket hung on flush

We have a set of applications, written in Java, which communicate with each other by exchanging short XML messages via sockets. At present, during development, we are running all the applications on the same workstation. Our problem is that it is somehow possible, after some tens of thousands of messages have been exchanged in each direction, for the socket connections between these programs to hang.

Looking at the problem in the Eclipse debugger, we find that both ends of the connection are running separate instances of identical code. Both ends have just successfully written a valid message to a BufferedWriter based on the Socket, using its write(String) call, and both ends appear to be blocked waiting on completion of a BufferedWriter flush() call. There is nothing unusual about the code, and it runs flawlessly for thousands of messages before hanging. No exceptions are thrown on either end, and the wait appears to be indefinite. We have observed this problem while running on both Linux and Windows platforms.

Can anyone suggest what might be going on?

Upvotes: 0

Views: 393

Answers (4)

EQvan
EQvan

Reputation: 397

thank you all for your thoughts. We've got it figured out, it wasn't really mysterious at all, once we took a large enough view. EJP was on the right track. To give you more information, each of the sockets opened by each of the apps was in fact, attended by a read thread, which read the socket as messages came available, and placed these on a queue for further processing. These read threads were, on both ends of the socket that deadlocked, waiting to read, which is what made things seem so inexplicable yesterday. Today, after a better look, it appears that the whole system is backed up; the particular sockets in question weren't blocked trying to queue freshly-read messages, but others in the system were. Because all of the apps were communicating using the same code, this 'rigidity' had the ability to lock up the whole system.
We recognize now what's going on, and how to fix it. Thanks again.

Upvotes: 0

seand
seand

Reputation: 5286

Check your messaging protocol carefully to ensure when socketA writes, socketB reads and vice versa. If there's a scenario where both ends are trying to write at the same time, it indicates trouble. It's most common for the initiating connection (client) to write a message to the accepting end (server). The server will then write a response and the client will hopefully be listening.

Upvotes: 0

Pulah Nandha
Pulah Nandha

Reputation: 774

To make efficient 2 way exchange socket programming in java make put reader & writer is different thread.

Which is best way to implement socket programming in 2 way communication

Upvotes: -1

user207421
user207421

Reputation: 310860

Both ends have just successfully written a valid message to a BufferedWriter based on the Socket, using its write(String) call, and both ends appear to be blocked waiting on completion of a BufferedWriter flush() call.

In other words both ends are writing and no end is reading. So if the write overflows the sender's socket send buffer, which can happen if the receiver's socket receive buffer is full, which can happen if the receiver isn't reading, the sender will block. This sounds like an application protocol error. At the time one end is sending, the other end should be receiving. Or else there should be separate sending and receiving threads at one or both ends.

Upvotes: 2

Related Questions