如何在 Java 中通过 TCP 发送 OBJECT? [英] How to send an OBJECT over TCP in java?

查看:22
本文介绍了如何在 Java 中通过 TCP 发送 OBJECT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,将一个对象从一个类发送到另一个类.这是我的程序的一个简短示例示例来表示问题.正如您所看到的,从服务器发送到客户端的对象是 Student 类 ,它已在每个类(服务器/客户端)中单独定义.它涉及到我自己定义的类类型,我收到此错误:

I'm writing a program to send an object from one class to another class. Here is a short sample example of my program to represent the problem. As you can see the object to send from server to client is Student class which has been defined separately in each class(Server/Client). I have examined this code by sending an ArrayList which works fine but when it comes to a class type which defined by myself i'm receiving this error:

Exception in thread "main" java.lang.ClassCastException: ServerSide$1Student cannot be cast to ClientSide$1Student
    at ClientSide.main(ClientSide.java:29)

这是服务器端的代码:

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

public class ServerSide {

    public static void main(String[] args) {
        class Student implements Serializable
        {
            int id;
            public Student(int num){id=num;}
            public void setID(int num){id=num;}
            public void Print(){System.out.println("id = " + id);}
        }
        try
        {
            Student a = new Student(3);
            ServerSocket myServerSocket = new ServerSocket(9999);
            Socket skt = myServerSocket.accept();   
            try 
            {
                ObjectOutputStream objectOutput = new ObjectOutputStream(skt.getOutputStream());
                objectOutput.writeObject(a);                
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            } 
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

对于客户端是:

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientSide {

    public static void main(String[] args)
    {
        class Student implements Serializable
        {
            int id;
            public Student(int num){id=num;}        
            public void setID(int num){id=num;}
            public void Print(){System.out.println("id = " + id);}
        }
        try {       
            Socket socket = new Socket("10.1.1.2",9999);
            try {
                ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream());
                try {
                    Object object =(Student) objectInput.readObject();
                    Student tmp = (Student) object;
                    tmp.Print();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }           
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }   
}

我将它们移到同一个文件并添加了序列化 ID.它工作正常.

推荐答案

有两个同名的类是不够的.这两个类需要有

Having two classes with the same name is not enough. The two classes need to have

  • 同一个包
  • 在同一个外部类中(如果有)
  • 具有相同的 serialVersionUID.

我建议你有一个单一的、独立的类,它对客户端和服务器都是通用的.在更复杂的项目中,您可能会考虑在客户端和服务器模块都依赖的模块中构建这些通用组件.

I suggest you have a single, stand alone class which is common for both the client and the server. In a more complex project you might consider building these common components in a module which both the client and server modules depend on.

这篇关于如何在 Java 中通过 TCP 发送 OBJECT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆