2

I have two classes X and Y, like this:

class X implements Serializable
{
  int val1;
  Y val2;
}

class Y implements Serializable
{
  int val;
}

I want to transmit an object of type X from a client to server but i can't because the class X has a field of type Y. I replaced the field of type Y with a field of type X in class X and it works.

EDIT These are my classes:

class Y implements Serializable
{
  int val;
  Y()
  {
    val = 3;
  }
}

class X implements Serializable
{
  int val;
  Y ob;

  X(int i, Y o)
  {
    val = i;
    ob = o;
  }
}

public class Server
{
  public static void main(String[] s)
  {
    ServerSocket ss = null;
    Socket cs = null;
    ObjectInputStream ois = null;
    ObjectOutputStream oos = null;

    try
    {
    ss = new ServerSocket(1234);
    System.out.println("Server pornit!");
    cs = ss.accept();

    oos = new ObjectOutputStream(cs.getOutputStream());
    ois = new ObjectInputStream(cs.getInputStream());
    }
    catch(Exception e)
    {
      System.out.println("Exceptie!");
    }

    System.out.println("Asteapta mesaj...");
    X x;

    try
    {
    x = (X) ois.readObject();
    System.out.println(x.val);

    }
    catch(Exception e)
    {
      System.out.println(e.toString());
    }
    try
    {
    ss.close();
    cs.close();
    }
    catch(Exception e)
    {
    }

  }
}
public class Client
{
  public static void main(String[] s)
  {
    Socket cs;
    ObjectInputStream ois = null;
    ObjectOutputStream oos = null;

    System.out.println("Connect...");
    try
    {
    cs = new Socket("127.0.0.1",1234);

    oos = new ObjectOutputStream(cs.getOutputStream());
    ois = new ObjectInputStream(cs.getInputStream());
    }
    catch(Exception e)
    {
      System.out.println("Exceptie!");
    }


    try
    {
    oos.writeObject(new X(8,new Y()));
    }
    catch(Exception e)
    {
      System.out.println(e.getMessage());
    }
  }
}
6
  • Are you using ObjectOutputStream and ObjectInputStream? What kind of errors do you see? If you are using 2 processes: Are you sure you provided the same version of compiled classes to both of them? Commented Apr 22, 2010 at 21:25
  • 2
    How about some serialization code? Stack traces? You can't transmit an X instance? Why not? What happens when you try? When you change Y to X and "it works," what does that mean, exactly? Commented Apr 22, 2010 at 21:28
  • Yes i use ObjectOutputStream and ObjectInputStream and yes the same classes are in client and server. The exception is thrown in server at receiving the object of type X. The exception is : "java.net.SocketException: Connection reset" Commented Apr 22, 2010 at 21:41
  • Are you sure that the client is not throwing an exception? If it does, that may be able to reset the connection... Commented Apr 22, 2010 at 21:42
  • 1
    This is something else; can you further explain what is the error? Connection reset does not sound like a serialization issue. Commented Apr 22, 2010 at 21:46

4 Answers 4

1

Ok I think I found the problem. The client process terminates prematurely, before closing the output stream. As a result, the server gets an unexpected disconnection. Add oos.close() to the client code.

Sign up to request clarification or add additional context in comments.

Comments

0

Check that the remote client has access to the .class files for both X and (in particular) Y?

In particular, if new Y() does not succeed you have a problem :-)

(What is the error you're getting?)

Comments

0

Note by oreyes: moved to the original post

Comments

0

It works on my machine:

$javac X.java  
$java Server & 
[1] 14037
$Server pornit!
java Client 
Connect...
Asteapta mesaj...
$8

I wonder if your are not killing the server when you launch the client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.