Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a Stream from Socket

 
Chandar S Vellithirumutha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While reading a stream from a socket, if there is no data comming in and if you are performing a readObject on the socket will it be in the blocked state or will it return null.
Thanks in advance....
 
pollai mandala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandar S Vellithirumutha,
While reading a stream from a socket, if there is no data comming in and if you are performing a readObject on the socket:
then it will be in the blocked state until it timed out.
if u want to wait infinitely then u can use setSoTimeout(int) method in Socket class.
if the sent object is not a serialized one or if the server
is sending some response rather than an Object then this
listening socket will receive that and throws
java.io.StreamCorruptedException

regards,
 
Chandar S Vellithirumutha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pollai
This is the code I am writting in the run method of a chat i am trying to code. Please help me in this.
In this case my logic in the run method is to check whether data has to be written out to this socket(checking for null), if so I write it and after that I try reading the socket whether data is comming in. If there is no data comming in, then after writting the data it will be in a blocked state and i could not write data out to the socket if there are new message to be written after the first write. How can i resolve this.
Thanks in advance....
Chandar

Here is the code
public void run() {
int noOfAttempts = 0;
//Object to be serialized
private SObject readObj;
try {
while(socOpenFlag) {
//Sets the serialized object data
sObject.setData(dataToBeTxed);
try {
out.writeObject(sObject);
noOfAttempts = 0;
}catch(IOException e) {
e.printStackTrace();
noOfAttempts++;
}
//Discard the message after attempting 3 times
if (noOfAttemepts == 3) dataToBeTxed = null;

continue;
}

try {
readObj = (serObject) in.readObject();
}catch(IOException e) {
e.printStackTrace();
}

if (readObj != null) processReadObject(readObj);
}
}finally{
SocketCon.close();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic