Problem while using SocketChannel in multithreading fashion.

Hi,

I want to build a program which establishes the connection between peers and transfer data in multithreaded environment.

Earlier i used to obtain the socket in the following fashion.

Socket obtained from ip and port:

Socket sock =new Socket(ip1, port1);

Now i use SocketChannel to obtain the socket in the following fashion.

SocketChannel sock

socket = sock.socket();

Problem:

First peer is performing handshake with another peer which establishes the connection which is done in single threaded fashion which works fine. But after that reading and writting splits into 2 different threads. Now i am able to write data on the output stream but the reading thread gets stuck on a call to readInt() .

It gives IOException after some time but when i try to print the exception message via ex.getMessage(), it returns null.

All these works fine in single threaded environment for socket extracted from SocketChannel.

And also works fine for multiple thread with the socket extracted in the former fashion.

Can anyone please tell me is there any problem in using socket returned by SocketChannel.socket() in multithreaded fashion?

Thanx

Adeesh Jain

[1283 byte] By [Adeesha] at [2007-11-26 16:51:04]
# 1

(1) Why are you using a SocketChannel? There is no benefit in blocking mode if you're just going to keep using the streams of its Socket.

(2) Unlike the streams you get from a directly created Socket, the streams you get from a SocketChannel.socket() are mutually synchronized against simultaneous reading and writing. Does that explain anything?

ejpa at 2007-7-8 23:18:42 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi, I have to use to some .jar library to perform some functionality in my code. And that .jar returns me the SocketChannel instead of Socket. can you please tell me how can i overcome this situation? ThanxAdeesh Jain.
Adeesha at 2007-7-8 23:18:42 > top of Java-index,Java Essentials,Java Programming...