Java 客户端/服务器聊天室将消息作为对象发送

标签 java sockets chat serializable

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

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

Message对象有一个String,以及其他字体信息。

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

想法是让服务器对客户端透明——这可能吗,即:服务器对 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;
    }
}

感谢您的宝贵时间!

编辑: 这是创建的输出:

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

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

最佳答案

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

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

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

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

关于Java 客户端/服务器聊天室将消息作为对象发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19650558/

相关文章:

java - 在java中实现接口(interface)

java - Spark : memory issues (GC overhead limit exceeded) when using cogroup with ListBuffer in Scala

c - 如何修复 GCC 错误 : "asm/socket.h: No such file" on Sli-Taz Linux?

android - 音频流输入 block 掉线了吗?

java - 如何在其他 JTextField 下找到 JTextField?

java - 使用itextpdf为对齐的pdf文本添加下划线

java - 如何估算总 IO

node.js - Express.io 中的连接客户端

google-app-engine - 如何在Google App Engine中使用XMPP协议(protocol)获取聊天消息发送者的IP地址?

multithreading - 这个 Haskell 聊天代码中的同步缺陷是什么,修复方法是什么?