Java网络对象序列化
我试图将一个对象从客户端发送到服务器,其中一个对象状态是向量,另一个是字符串。我可以在服务器端访问该字符串,但服务器端的向量内容为零。有人可以帮我吗?
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
testobject
的构造函数未使用vector
参数。它是将vectorX
ivar 分配给一个新实例,忽略调用者提供的参数:您应该使用:
注意: Java 中的类名不常见全部小写,或者以小写字母开头。顺便说一句,我认为您应该将您的类重命名为
TestObject
。The constructor for your
testobject
is not using thevector
argument. It is rather assigning thevectorX
ivar to a new instance, ignoring the parameter provided by the caller:You should instead use:
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
.