File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Reading a Stream from Socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Reading a Stream from Socket" Watch "Reading a Stream from Socket" New topic
Author

Reading a Stream from Socket

Chandar S Vellithirumutha
Greenhorn

Joined: May 29, 2000
Posts: 15
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

Joined: Jun 16, 2000
Posts: 17
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,


Regards,<BR>Bhavani
Chandar S Vellithirumutha
Greenhorn

Joined: May 29, 2000
Posts: 15
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();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a Stream from Socket