AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: best practice for reading objects from sockets?

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Knute Johnson
Guest





PostPosted: Wed Jul 09, 2003 11:33 pm    Post subject: Re: best practice for reading objects from sockets? Reply with quote



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





PostPosted: Thu Jul 10, 2003 5:22 am    Post subject: Re: best practice for reading objects from sockets? Reply with quote



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





PostPosted: Thu Jul 10, 2003 2:26 pm    Post subject: Re: best practice for reading objects from sockets? Reply with quote



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





PostPosted: Thu Jul 10, 2003 8:42 pm    Post subject: Re: best practice for reading objects from sockets? Reply with quote

On Wed, 09 Jul 2003 22:45:49 GMT, "Jeff" <jwoofy (AT) hotmail (DOT) com> wrote or
quoted :

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?

see http://mindprod.com/jgloss/tcpip.html
and http://mindprod.com/jgloss/socket.html
and http://mindprod.com/jgloss/readfully.html
and http://mindprod.com/jgloss/readblocking.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Back to top
Thomas Gagné
Guest





PostPosted: Mon Jul 14, 2003 3:51 pm    Post subject: Re: best practice for reading objects from sockets? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.