socket programming and thread
Hi all,
I am getting problem while reading or writing to a port.
we have two different thread for reading and writing, are they synchronize by itself or we have to make it explictly.
the error i get are either of these two:
1) java.io.EOFException
2) java.net.SocketException: Broken pipe
# 1
Hey, Can you give source code, So i can test it?
# 2
Code is too big to tell. i tell u the root cause.
in class A we read data:
ch = R1.readUnsignedByte();
i read byte by byte in a loop and a thread control this class.
java.io.EOFException
in class B we write data
S1.write(uByte[i / 2]);
where uByte is a integer.
i write this data byte by byte in a loop control by thread of class B.
java.net.SocketException: Broken pipe
when it read and write simultaneously it get error in any of those classes
# 3
If you're getting 'broken pipe' or 'EOFException it has nothing to do with synchronization (which BTW you don't have to worry about unless you have > 1 writing thread). These are errors in your application protocol: broken pipe means that the reader has closed while the writer is still writing, and EOFException means the reverse.
Also uByte[i/2] may be an integer, although why it would be called uByte is a mystery, but writing it with OutputStream.write() will only write the low-order byte of it. Are you sure you're reading and writing the same things?
ejpa at 2007-7-9 6:27:54 >

# 4
thanks for reply,
uByte is just an user define integer array.
S1.write(uByte[i / 2]);
S1->object for dataoutputstream.
messageHolder = Integer.toHexString(R1.readUnsignedByte());
R1->object for datainputstream.
Our server end, in which these code is defined, continuesly read data from client application in form of messages of few byte.
a thread read it from server socket now when our server simultaneoisly tries to write message to that port it gives errror.
it is not giving error when we only read data for any number of time
# 5
Not very clearly expressed, but anyway what is the client doing?
BTW I'm wondering whether you know that closing a socket input stream or output stream closes the other stream and the socket? I suspect the client may have done one of these things.
Anyway you shouldn't be surprised at getting an EOFException, that just means the other end has closed the socket. This is a normal situation. The connection reset is a concern but I think the explanation is above.
ejpa at 2007-7-9 6:27:54 >

# 6
Server is actually open its port to read data and also write data on it.
client is sending messagein a packate formate having length of packate.
we need a program that read message formate from client and reply accordingly.
its a continues process, so it needs to read and write at the same time.
the same code works with simulatorsin unix but fails in real time in HP server.
it is reading 4-5 message from client and when respond back first message and get failed giving above prob.
logger shows it exactly on read or write line;
can u suggest what could be the exact loophole in program.
should i reinitialize all connection at exceptions
Message was edited by:
sourabh_27
# 7
If you get any exception on a socket the only sensible reaction is to log it and close the socket.Can you show us some code please, and also the packet format.
ejpa at 2007-7-9 6:27:54 >

# 8
i am writing server and client which is thread based, i have implemented the biderectional ,first client writes to server. server reads from client first and then writes to client ,after server write client is not able to read data written by server. i am using the ObjectRead and Object Write methods of socket .
At client when i read it gives the following error java.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.canarys.web.pushlet.newClient.run(Client.java:93)
please help me out how to avoid this, the object i am using is serialised, if any of u need more clarification i will paste the code also
any help regarding this will be appreciated
# 9
You are probably creating one ObjectInputStream for reading and multiple ObjectOutputStreams for writing (at the other end), or the other way around.Or you are sending data as well as objects and you have an error in your application protocol implementation.
ejpa at 2007-7-9 6:27:54 >
