Problem in using SocketChannel..

Hi,

I am using the following code to make connection with the server. The speciality of this server is that it has the code of peer.

java.net.socket sock = new Socket(ip1,port1) ;

Then i use this socket to make streams and do the following.

1) Created a class which will handle all the operations related to the Output stream and another class to handle all the operatioin related to the InputStream.

2) Now i have created the seperate thread for output stream class which handles all the operation related to the output stream. And main thread will continue to handle all operations related to the input stream.

Doing this in this manner will work okay for java.net.socket.

handshake();

Class A = new A(this , din);

Class B = new B(this, dout);

//B Class creates a new thread and calls its run method.

A.run();

After this class A handles all the operation related to the reading and B handles operations related to the writting.

But when i try to do the same thing with the socket obtained from the socketChannel then i stuck on reading after splitting the reading and writting in different threads. Actually i have to integrate some .jar in my code which returns me the SocketChannel. I am able to handshake with the server.

After that i got the problem. Is it possible to use the socket returned by the SocketChannel in the above multithreaded fashion.

Waiting for the reply..

Thanx

Adeesh Jain

[1509 byte] By [Adeesha] at [2007-11-26 17:12:02]
# 1
As I already told you in http://forum.java.sun.com/thread.jspa?threadID=5131064&messageID=9472255#9472255, '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
ejpa at 2007-7-8 23:39:55 > top of Java-index,Archived Forums,Socket Programming...
# 2

Hi ejp,

Thanks for your reply.

I agree that reading and writing operations via socket channel are mutually synchronize. Synchronize will make us sure that only one operation (either reading or writing) can be performed but it does not explain that why the system hangs when two are running in parallel.

I am understanding it like when writing operations is getting performed , at that time no reading can be done but after writing on stream, reading thread should obtained the lock and should start reading from streams and at that time writing thread should wait. So there should not be any chance of deadlock.

Is there anything that you can suggest me to try out because the problem getting deadly. I am stuck on the same from last 2 weeks.

Is it possible to chat with you on the same problem. It will be great help.

Adeesh.

Adeesha at 2007-7-8 23:39:55 > top of Java-index,Archived Forums,Socket Programming...
# 3

> Synchronize

> will make us sure that only one operation (either

> reading or writing) can be performed but it does not

> explain that why the system hangs when two are

> running in parallel.

It would explain it if your threads are synchronizing on something else as well and causing a deadlock via the multiple sync blocks.

Show us your reading and writing code, and also the part where you acquire the input and the output streams.

ejpa at 2007-7-8 23:39:55 > top of Java-index,Archived Forums,Socket Programming...