Java网络对象序列化

发布于 2024-12-22 04:01:15 字数 2433 浏览 0 评论 0原文

我试图将一个对象从客户端发送到服务器,其中一个对象状态是向量,另一个是字符串。我可以在服务器端访问该字符串,但服务器端的向量内容为零。有人可以帮我吗?

    // Server
import java.net.*;   
import java.util.Vector;
import java.io.*;
public class SimpleServer {

    public static void main(String args[]) {
        int port = 2002;
        try {
            System.out.println("Hello");
            ServerSocket ss = new ServerSocket(port);
            Socket s = ss.accept();
            System.out.println("Hello 2");
            InputStream is = s.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            testobject to = (testobject)ois.readObject();
            System.out.println("Vector size : " + to.vectorX.size() + " and object.id : "
                    + to.id);
/*          if (to != null) {
                for(int i = 0; i < to.vectorX.size(); ++i )
                System.out.println("Output 1 : " + to.vectorX.elementAt(i));
            }   */
            is.close();
            s.close();
            ss.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


// Client
import java.net.*;
import java.io.* ;
import java.util.Vector;

public class SimpleClient {

    protected static Vector<String> vectorX = new Vector<String>();

    public SimpleClient(){
        vectorX.addElement("hello");
        vectorX.add("goodbye");
        vectorX.add("finally");   
    }

    public static void main(String args[]) {
        try {
            new SimpleClient();
            Socket s = new Socket("localhost", 2002);
            OutputStream os = s.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            testobject to = new testobject(1, "theID", vectorX );
            System.out.println(vectorX.size());
            oos.writeObject(to);
//          oos.writeObject(new String("another object from the client"));
            oos.close();
            os.close();
            s.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}


//testobject
import java.net.*;
import java.io. * ;
import java.util.Vector;
class testobject implements Serializable {
    int value;
    String id;
    Vector<String> vectorX; 

    public testobject(int v, String s, Vector<String> vector) {
        this.value = v;
        this.id = s;
        this.vectorX = new Vector<String>();
    }
}

Im trying to send an object from client to server and one of the object states is a vector and the other is a string. I can access the string on the Server side, but the vector contents is zero on the server side..Can someone help me out please..

    // Server
import java.net.*;   
import java.util.Vector;
import java.io.*;
public class SimpleServer {

    public static void main(String args[]) {
        int port = 2002;
        try {
            System.out.println("Hello");
            ServerSocket ss = new ServerSocket(port);
            Socket s = ss.accept();
            System.out.println("Hello 2");
            InputStream is = s.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            testobject to = (testobject)ois.readObject();
            System.out.println("Vector size : " + to.vectorX.size() + " and object.id : "
                    + to.id);
/*          if (to != null) {
                for(int i = 0; i < to.vectorX.size(); ++i )
                System.out.println("Output 1 : " + to.vectorX.elementAt(i));
            }   */
            is.close();
            s.close();
            ss.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


// Client
import java.net.*;
import java.io.* ;
import java.util.Vector;

public class SimpleClient {

    protected static Vector<String> vectorX = new Vector<String>();

    public SimpleClient(){
        vectorX.addElement("hello");
        vectorX.add("goodbye");
        vectorX.add("finally");   
    }

    public static void main(String args[]) {
        try {
            new SimpleClient();
            Socket s = new Socket("localhost", 2002);
            OutputStream os = s.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            testobject to = new testobject(1, "theID", vectorX );
            System.out.println(vectorX.size());
            oos.writeObject(to);
//          oos.writeObject(new String("another object from the client"));
            oos.close();
            os.close();
            s.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}


//testobject
import java.net.*;
import java.io. * ;
import java.util.Vector;
class testobject implements Serializable {
    int value;
    String id;
    Vector<String> vectorX; 

    public testobject(int v, String s, Vector<String> vector) {
        this.value = v;
        this.id = s;
        this.vectorX = new Vector<String>();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2024-12-29 04:01:15

testobject 的构造函数未使用 vector 参数。它是将 vectorX ivar 分配给一个新实例,忽略调用者提供的参数:

public testobject(int v, String s, Vector<String> vector) {
        this.value = v;
        this.id = s;
        this.vectorX = new Vector<String>(); // This is bad
    }

您应该使用:

public testobject(int v, String s, Vector<String> vector) {
        this.value = v;
        this.id = s;
        this.vectorX = vector;
    }

注意: Java 中的类名不常见全部小写,或者以小写字母开头。顺便说一句,我认为您应该将您的类重命名为 TestObject

The constructor for your testobject is not using the vector argument. It is rather assigning the vectorX ivar to a new instance, ignoring the parameter provided by the caller:

public testobject(int v, String s, Vector<String> vector) {
        this.value = v;
        this.id = s;
        this.vectorX = new Vector<String>(); // This is bad
    }

You should instead use:

public testobject(int v, String s, Vector<String> vector) {
        this.value = v;
        this.id = s;
        this.vectorX = vector;
    }

Note: It's not common for class names in Java to be all lowercase, or to start with a lowercase letter. As an aside, I think you should rename your class to TestObject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文