Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sending objects on sockets... Possible?

0 views
Skip to first unread message

Martin Olausson

unread,
Apr 24, 2000, 3:00:00 AM4/24/00
to
HI!

I have a question, that hopefully is simple for a lots of
You who had been working with sockets.

I would like to send an object on a Socket. Could I do that?!
If so, how do I specify that?

Like...

client = new Socket(address, port);

InputStreamReader isr = new
InputStreamReader(client.getInputStream());
in = new BufferedReader(isr);
out = new PrintWriter(client.getOutputStream(), true);

MyClass myObject = new MyClass("This is the string", otherdata);

out.println(myObject);

Hope somebody could help me...

Regards, Martin
-------------------------
Martin Olausson

Novia Notio, currently working for Telefónica I+D.
http://www.infosfera.com/

Work phone: +34 91 337 44 25
Mobile: +34 619 45 64 77
Email: martin....@bigfoot.com
Email to mobile: 6194...@correo.movistar.net

Private address: C/ Alcala 181 3-1 A
28009-Madrid
Spain

Kees Kuip

unread,
Apr 24, 2000, 3:00:00 AM4/24/00
to
Martin Olausson wrote:

>
>
> I would like to send an object on a Socket. Could I do that?!
> If so, how do I specify that?
>
> Like...
>
> client = new Socket(address, port);
>
> InputStreamReader isr = new
> InputStreamReader(client.getInputStream());
> in = new BufferedReader(isr);
> out = new PrintWriter(client.getOutputStream(), true);
>
> MyClass myObject = new MyClass("This is the string", otherdata);
>
> out.println(myObject);
>

Hello,

You should take a look at ObjectInputStream and ObjectOutputStream.

Kees.


Martin Olausson

unread,
Apr 24, 2000, 3:00:00 AM4/24/00
to
Hi!

I have used the classes ObjectInputStream and ObjectOutputStream
to send objects on a socket.

It works fine if I open one ObjectOutputStream on my server process
and one ObjectInputStream on my client process.

Then the server could send data to the client without any problem!

But if I want to send back the data to the server from the client,
then I need to create a ObjectOutputStream in the client process
and a ObjectInputStream in the server process.

But then the process hangs on the processes "new ObjectInputStream()"
calls...

(The code is very short and attached...)

Why?! I have, by myself, no ideas...

Greatful for any kind of suggestions...!!

/Martin

--
------------------------------------

Client.java
Server.java

Martin Olausson

unread,
Apr 24, 2000, 3:00:00 AM4/24/00
to
Strange... by changing the order of creating the in and out stream for
the client,
everything works fine...

in = new ObjectInputStream(client.getInputStream());
out = new ObjectOutputStream(new
BufferedOutputStream(client.getOutputStream()));

changed to:
out = new ObjectOutputStream(new
BufferedOutputStream(client.getOutputStream()));
in = new ObjectInputStream(client.getInputStream());

/Martin

> ------------------------------------------------------------------------
> import java.lang.*;
> import java.net.*;
> import java.io.*;
> import java.util.*;
>
> public class Client
> {
> Socket client;
> ObjectInputStream in;
> ObjectOutputStream out;
>
> Client(InetAddress address, int port)
> {
> try
> {


> client = new Socket(address, port);

> in = new ObjectInputStream(client.getInputStream());
>
> // THIS LINE IS NEVER REACHED!!!
>
> out =
> new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
>
> }
> catch (Exception e)
> {
> System.out.println("Client: Got a exception: " + e.toString());
> System.exit(-1);
> }
>
> }
>
> public static void main(String[] args)
> {
> try
> {
> Client myC = new Client(InetAddress.getByName("1.0.21.240"), 9506);
>
> // Read from the Server process.
> Object ob = myC.in.readObject();
> System.out.println("Date: " + (Date)ob);
>
> // Write to th server process.
> myC.out.writeObject("Client to server message...");
> myC.out.flush();
>
> }
> catch (Exception e)
> {
> System.out.println("Client: Got a exception: " + e.toString());
> }
> }
> }
>
> ------------------------------------------------------------------------
> import java.lang.*;
> import java.net.*;
> import java.io.*;
> import java.util.*;
>
> public class Server
> {
> ServerSocket server = null;
> Socket client = null;
> ObjectOutputStream out = null;
> ObjectInputStream in = null;
>
> Server(int port)
> {
> try
> {
> server = new ServerSocket(port);
> }
> catch (Exception e)
> {
> System.out.println("Server: Got a exception: " + e.toString());
> System.exit(-1);
> }
> }
>
> boolean accept()
> {
> try
> {
> client = server.accept();
>
> in = new ObjectInputStream(client.getInputStream());
>
> // THIS LINE IS NEVER REACHED!!!
>
> out =
> new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
>
> return true;
> }
> catch (Exception e)
> {
> System.out.println("Server: Got a exception: " + e.toString());
> System.exit(-1);
>
> }
> return true;
> }
>
> public static void main(String[] args)
> {
> try
> {
> Server myS = new Server(9506);
> myS.accept();
>
> // write to the client process.
> myS.out.writeObject(new Date());
>
> myS.out.flush();
>
> // Read from the client process.
> Object ob = myS.in.readObject();
> System.out.println((String)ob);
>
> }
> catch (Exception e)
> {
> System.out.println("Server: Got a exception: " + e.toString());

Mark Lambourne

unread,
Apr 24, 2000, 3:00:00 AM4/24/00
to
Martin Olausson <martin....@bigfoot.com> wrote in message
news:39045113...@bigfoot.com...

> Strange... by changing the order of creating the in and out stream for
> the client,
> everything works fine...

The reason for this is that Socket.getInputStream() will block until the
other end of the Socket (i.e. the machine to which the Socket is connected)
calls a Socket.getOutputStream().

In the code given, both machines are doing the Socket.getInputStream() call
first, which means both of them are blocking (and waiting for the other
machine to do the .getOutputStream(), which neither of them will do), and
you have a deadlock.

As you have noted, changing the order of the .getInputStream() and
.getOutputStream() lines on either the Client or the Server (but NOT BOTH)
fixes this, since the deadlock does not occur.

It is unfortunate that the API does not mention this.

Mark

0 new messages