سؤال

My goal is to create a server which receives a Message object from ObjectInputStream, and then echos that Message object to ObjectOutputStream.

Upon writing a message, the client sends the Message type object to the server, the received Message type object (as well as the Message type object from other clients connected to the server) is then received and decoded to the client's gui.

The Message object has a String, and other font information.

My problem is that the server does not echo back the Message type object. I have a feeling that I am not casting properly, but at this point I'm just fishing with trying different things.

The idea is to make the server transparent from the client - is this possible, ie: server does not know anything about the TestObject class? Is there a way around this?

Server code:

import java.net.*;
import java.io.*;

public class Server
{
    public SimpleServer() throws IOException, ClassNotFoundException
    {
        ServerSocket ss = new ServerSocket(4000);
        Socket s = ss.accept();

        ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

        ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

        // the 'echo' functionality of the server
               Object to = null;
        try 
        {
            to = ois.readObject();
        } 
        catch (ClassNotFoundException e)
        {
            System.out.println("broke");
            e.printStackTrace();
        }

        oos.writeObject(to);
        oos.flush();

        // close the connections
        ois.close();
        oos.close();

        s.close();
        ss.close();
    }

    public static void main(String args[]) throws IOException, ClassNotFoundException {
        new SimpleServer();
    }
}

Client code:

import java.net.*;
import java.io.*;

public class Client
{
    public SimpleClient() throws IOException, ClassNotFoundException
    {
        Socket s = new Socket( "localhost", 4000 );

        ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
        ObjectInputStream  ois = new ObjectInputStream( s.getInputStream());

        TestObject to = new TestObject( 1, "object from client" );

        // print object contents
        System.out.println( to );

        oos.writeObject(to);
        oos.flush();
        Object received = ois.readObject();

        // should match original object contents
        System.out.println(received);

        oos.close();
        ois.close();
    }

    public static void main(String args[]) throws IOException, ClassNotFoundException
    {
        new SimpleClient();
    }
}

class TestObject implements Serializable
{
    int value ;
    String id;

    public  TestObject( int v, String s )
    {
        this.value=v;
        this.id=s;
    }

    @Override
    public String toString()
    {
        return  "value=" + value +
                ", id='" + id;
    }
}

Thank you for your time!

EDIT: This is the output created:

I get this message when the client connects to the server: Exception in thread "main" java.lang.ClassNotFoundException: TestObject

And this is the client output when I run the client: value=1, id='object from client Exception in thread "main" java.net.SocketException: Connection reset

هل كانت مفيدة؟

المحلول

First thing : TestObject class is not visible in your Server code. That's why it is throwing ClassNotFoundException.

Solution to this : Put your TestObject class in seperate file and then use import statement to import that class.

Second: Try to print the value of received object at server side. And made changes to your client code as below

Object received = null;
while(received==null)
{
    received = ois.readObject();
}
System.out.println(received);

نصائح أخرى

I don't see any problem with your code except you need to flush the oos stream after you write to it in both the server and the client.

oos.flush();

Also for making the server transparent of the client, you can avoid casting on server(which is you are already doing) and the end point should worry about the TestObject which is your client.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top