【问题标题】:Java Client/Server chat room sending messages as objectsJava 客户端/服务器聊天室将消息作为对象发送
【发布时间】:2013-11-08 03:44:14
【问题描述】:

我的目标是创建一个从 ObjectInputStream 接收 Message 对象的服务器,然后将该 Message 对象回显到 ObjectOutputStream。

客户端写入消息后,将消息类型对象发送给服务器,然后接收到的消息类型对象(以及来自连接到服务器的其他客户端的消息类型对象)并解码到客户端的 gui。

Message 对象有一个字符串和其他字体信息。

我的问题是服务器没有回显消息类型对象。我感觉我施法不正确,但此时我只是在尝试不同的东西。

这个想法是让服务器对客户端透明 - 这可能吗,即:服务器对 TestObject 类一无所知?有没有办法解决这个问题?

服务器代码:

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();
    }
}

客户端代码:

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;
    }
}

感谢您的宝贵时间!

编辑: 这是创建的输出:

当客户端连接到服务器时,我收到此消息: 线程“主”java.lang.ClassNotFoundException 中的异常:TestObject

这是我运行客户端时的客户端输出: value=1, id='object from client Exception in thread "main" java.net.SocketException: Connection reset

【问题讨论】:

  • 你得到什么输出?
  • 当客户端连接到服务器时收到此消息:线程“main”中的异常 java.lang.ClassNotFoundException: TestObject 这是我运行客户端时的客户端输出:value=1, id ='来自线程“主”java.net.SocketException中的客户端异常的对象:连接重置

标签: java sockets chat serializable


【解决方案1】:

第一件事:TestObject 类在您的服务器代码中不可见。这就是为什么它抛出ClassNotFoundException

解决方案:将您的 TestObject 类放在单独的文件中,然后使用 import 语句导入该类。

第二:尝试在服务器端打印接收到的对象的值。并对您的客户端代码进行了如下更改

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

【讨论】:

  • 我想避免服务器知道任何关于 TestObject 的信息。这可以使用 Object IO 吗?
  • 如果不是,解决方案(尽可能减少服务器)是否使用一些字节流库?
  • 您可以使用 Bytestream lib 的读取方法。参考this
  • 感谢您的宝贵时间。
【解决方案2】:

我认为您的代码没有任何问题,除非您需要在服务器和客户端写入 oos 流后刷新它。

oos.flush();

另外,为了使服务器对客户端透明,您可以避免在服务器上进行强制转换(您已经在这样做),并且端点应该担心您的客户端 TestObject

【讨论】:

  • 当客户端连接到服务器时收到此消息:线程“main”中的异常 java.lang.ClassNotFoundException: TestObject
  • 抱歉,无法理解您的意思。
  • 这是我运行客户端时的客户端输出:value=1, id='object from client Exception in thread "main" java.net.SocketException: Connection reset
  • 哦,我错了,您可能必须在从客户端发送消息之前将消息转换为 Object。客户端Connection reset的错误是由于服务器在读取消息后立即关闭连接。
  • oos.writeObject((Object)to);像这样?我一直在尝试,但没有运气,我的编译器告诉我这是多余的。
猜你喜欢
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多