• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading a Stream from Socket

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic