 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Knute Johnson Guest
|
Posted: Wed Jul 09, 2003 11:33 pm Post subject: Re: best practice for reading objects from sockets? |
|
|
Jeff wrote:
| Quote: | I'd appreciate advice on the best approach to reading objects from sockets.
Is it possible to make a blocking read or do I have to poll?
My server application sends data to my client application via TCP socket in
time intervals ranging from every few milliseconds to 100s of milliseconds.
I'd like to use a blocking read in the client to avoid unnecessary sleeps,
reads, etc. But readObject does not block in my tests, it returns null if
there is no data. Right now, my client sleeps for 10ms, then attempts to
read.
Can someone tell me how to do a blocking read when retrieving objects? Is
there a better approach?
Thanks
here's my input stream creation:
is = new ObjectInputStream(new BufferedInputStream(sock.getInputStream()));
|
I think you may be having some other problems. Even though the docs
don't say that the ObjectInputStream.readObject() blocks, it does say it
returns an object. It doesn't say that it returns a null but it can if
it is sent a null. I have never seen the behaviour that you describe so
I think your problem lies somewhere else.
I did write a couple of little test programs and tried them on my
computer but not across the network. I wouldn't think it would make any
difference but try them and see how they work for you.
Just start the two programs in a DOS window or Terminal and type some
characters on the keyboard on the tc (test client) end and they should
show up on the ts (test server) end. An empty line terminates both
programs.
import java.io.*;
import java.net.*;
public class ts {
public static void main (String[] args) {
try {
ServerSocket ss = new ServerSocket(33333);
Socket s = ss.accept();
ss.close();
ObjectInputStream ois = new
ObjectInputStream(s.getInputStream());
String str = "";
do {
str = (String)ois.readObject();
System.out.println(str);
} while (!str.equals("")) ;
} catch (Exception e) {
System.out.println(e);
}
}
}
import java.io.*;
import java.net.*;
public class tc {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost",33333);
ObjectOutputStream oos =
new ObjectOutputStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String str = "";
do {
str = br.readLine();
oos.writeObject(str);
oos.flush();
} while (!str.equals("")) ;
oos.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
--
Knute Johnson
email s/nospam/knute/
Molon labe...
|
|
| Back to top |
|
 |
Sudsy Guest
|
Posted: Thu Jul 10, 2003 5:22 am Post subject: Re: best practice for reading objects from sockets? |
|
|
Jeff wrote:
| Quote: | here's my input stream creation:
is = new ObjectInputStream(new BufferedInputStream(sock.getInputStream()));
|
I don't see the need for the intermediate BufferedInputStream. When
I'm reading objects from a socket stream then I use this code:
ObjectInputStream ois = null;
Object obj = null;
ois = new ObjectInputStream( sock.getInputStream() );
obj = ois.readObject();
|
|
| Back to top |
|
 |
Jeff Guest
|
Posted: Thu Jul 10, 2003 2:26 pm Post subject: Re: best practice for reading objects from sockets? |
|
|
Good point - now that I fixed my code, readObject blocks until new data has
arrived. So I don't need to buffer.
Thanks
"Sudsy" <bitbucket44 (AT) hotmail (DOT) com> wrote
| Quote: | Jeff wrote:
here's my input stream creation:
is = new ObjectInputStream(new
BufferedInputStream(sock.getInputStream()));
I don't see the need for the intermediate BufferedInputStream. When
I'm reading objects from a socket stream then I use this code:
ObjectInputStream ois = null;
Object obj = null;
ois = new ObjectInputStream( sock.getInputStream() );
obj = ois.readObject();
|
|
|
| Back to top |
|
 |
Roedy Green Guest
|
|
| Back to top |
|
 |
Thomas Gagné Guest
|
Posted: Mon Jul 14, 2003 3:51 pm Post subject: Re: best practice for reading objects from sockets? |
|
|
The best practice for reading objects from sockets is to /not/ read objects
from sockets. Middleware is the best for communicating between processes
across networks and adds all kinds of valuable features.
The only problem you now is /what/ to communicate between processes.
Serialized objects? XML? RPCs? Other?
Sudsy wrote:
| Quote: | Jeff wrote:
here's my input stream creation:
is = new ObjectInputStream(new
BufferedInputStream(sock.getInputStream()));
I don't see the need for the intermediate BufferedInputStream. When
I'm reading objects from a socket stream then I use this code:
ObjectInputStream ois = null;
Object obj = null;
ois = new ObjectInputStream( sock.getInputStream() );
obj = ois.readObject();
|
--
..tom
remove dashes in email for replies
http://isectd.sourceforge.net
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|