Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

help! intermittent problem with socket communications

843790May 13 2010 — edited May 16 2010
I'm running into an intermittent problem where the data from my server does not get sent properly - random garbage is received on the client. It happens maybe once every 1,000,000 requests. I check the data immediately before calling write and it is correct. I'm pretty sure it's the server and not the client because it only happens when the server uses multiple threads. It happens both across the network (client and server on different machines) or when client and server are both on the same machine. This is with 20 clients (threads in a test client).

Originally the server was written non-blocking using a Selector and a cachedThreadPool

When I ran into problems I rewrote it using a thread per connection and blocking. The problem still occurs.

If I synchronize the channel write on a global object this seems to eliminate the problem.

synchronized(MyServer.class) { channel.write(buf); }

But channels are supposed to be thread safe, and I only write to a given channel from a single thread anyway, so I can't see why/how this helps, other than perhaps by altering the timing.

Even with the blocking version, I am using channels because I want to use gathering writes. (The problem still occurs with non-gathering writes though.) I will probably try without channels next, to see if that makes a difference.

I'm pretty sure the code is correct and it does run fine the vast majority of the time. Presumably it's some kind of concurrency problem since it only occurs when the server is multi-threaded, but the threads don't share any data so I can't see how the problem can be in my code.

Any suggestions are welcome. I can post the code if that would help.

This is on an iMac with 3 ghz Intel Core 2 Duo with 4 gb ram, running Mac OS X 10.6.3

java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-10M3025)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01-101, mixed mode)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 13 2010
Added on May 13 2010
12 comments
441 views