求助!JAVA中 利用socket传递Vector集合对象
我想利用ObjectInputStream 在Socket中传递Vector对象。可是结果却不能达到我预期结果。在服务端向客户端传递一个Vector对象的时候,客户端可以接收到的Vector。但是当服务端在次传递一个内容有改变的Vector对象时,客户端接受到的Vector的内容和第一次接收到的一样。没有任何改变。
这是为嘛呢?有没有解决的办法呢? vector socket java --------------------编程问答-------------------- 代码给一下,通过Socket发送Object,至于Object内容是不是Vector应该没关系
例子
import java.net.*;--------------------编程问答--------------------
import java.io.*;
class testobject implements Serializable {
int value ;
String id;
public testobject(int v, String s ){
this.value=v;
this.id=s;
}
}
public class SimpleServer {
public static void main(String args[]) {
int port = 2002;
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject to = (testobject)ois.readObject();
if (to!=null){System.out.println(to.id);}
System.out.println((String)ois.readObject());
is.close();
s.close();
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]){
try{
Socket s = new Socket("localhost",2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
testobject to = new testobject(1,"object from client");
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);}
}
}
传输 对象这种方法我知道怎么写。
我的疑问在于:我把一个对象,从服务器端传到客户端。当我把服务端这个对象里的某些值修改过后,再次将它重新传到客户端。结果客户端第二次收到的这个对象里的值,没有改变。和第一次一样。 --------------------编程问答--------------------
所以我让你把代码发一下,好查看具体什么原因,这样光说分析不出,因为按理是不应该这样的 --------------------编程问答--------------------
不好意思,没贴代码。
现在我将就你给我的示例,做了些修改。
import java.io.Serializable;
import java.util.Vector;
public class testobject implements Serializable {
private int value;
private String id;
private Vector<String> vector ;
public testobject(int v, String s) {
this.value = v;
this.id = s;
}
public void setValue(int value) {
this.value = value;
}
public void setId(String id) {
this.id = id;
}
public void setVector(Vector<String> vector) {
this.vector = vector;
}
public int getValue() {
return value;
}
public String getId() {
return id;
}
public Vector<String> getVector() {
return vector;
}
}
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Vector;
public class SimpleClient {
private Socket s;
private OutputStream os;
private ObjectOutputStream oos;
private testobject to;
private Vector<String> testV = new Vector<>();
public SimpleClient() {
// TODO Auto-generated constructor stub
try {
s = new Socket("127.0.0.1", 10000);
os = s.getOutputStream();
oos = new ObjectOutputStream(os);
//第一次传递对象
to = new testobject(1, "object from client");
testV.add(" first ");
to.setVector(testV);
System.out.println(" id = "+to.getId()+"; value = "+to.getValue());
oos.writeObject(to);
System.out.println("--------------");
//修改同一对象的值都,继续传同一对象
to.setId("object from client 第二次");
to.setValue(100);
testV.add("scend ");
to.setVector(testV);
System.out.println(" id = "+to.getId()+" value = "+to.getValue()); //对象内的值已经修改
oos.writeObject( to);
while(true){
}
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
new SimpleClient();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleServer {
private InputStream is;
private ObjectInputStream ois;
private ServerSocket ss;
private Socket s;
public SimpleServer() {
// TODO Auto-generated constructor stub
int port = 10000;
try {
ss = new ServerSocket(port);
while (true) {
s = ss.accept();
is = s.getInputStream();
ois = new ObjectInputStream(is);
newThread();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void newThread() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
testobject to;
while (true) {
try {
to = (testobject) ois.readObject();
//输出接收到对象的值
System.out.println(" Server : value = " + to.getValue()
+ " id = " + to.getId());
System.out.println(" Server : Vector ==>");
for (String s : to.getVector()) {
System.out.println("===> " + s);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
public static void main(String args[]) {
new SimpleServer();
}
}
可以看到,服务器端,两次收到的值都是一样的。而客户端确实是已经更改成功了的。
--------------------编程问答--------------------
试过了 确实是这样,如果再新建一个发送对象就不是这样了
这个貌似是writeObject故意实现成这样的,只要是同一个对象的引用,就会使用按第一次发送时保留下来的
Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written. --------------------编程问答--------------------
感谢大大解答。
问题终于解决了。
我把
ois = new ObjectInputStream(is);这个语句放到线程里就OK了。 --------------------编程问答--------------------
怎么放的,会报错的吧
@Override
public void run() {
// TODO Auto-generated method stub
testobject to;
while (true) {
try {
ois = new ObjectInputStream(is);
to = (testobject) ois.readObject();
object=writeobjectstilloriginal.testobject@443226
Server : value = 1 id = object from client
Server : Vector ==>
===> first
java.io.StreamCorruptedException: invalid stream header: 71007E00
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at writeobjectstilloriginal.SimpleServer$1.run(SimpleServer.java:43)
at java.lang.Thread.run(Thread.java:619) --------------------编程问答--------------------
不要放到while()循环里,不然每次服务端都会产生一个新的通道,而客户端还是老的通道。这样就会出错。
应该放到while()循环之前。 --------------------编程问答--------------------
我试过了,如果其它保持不变,仅仅只是移动ois = new ObjectInputStream(is);
结果没有变化
object=writeobjectstilloriginal.testobject@443226
Server : value = 1 id = object from client
Server : Vector ==>
===> first
object=writeobjectstilloriginal.testobject@443226
Server : value = 1 id = object from client
Server : Vector ==>
===> first
--------------------编程问答--------------------
Vector似乎是没办法了。只能用基本数据类型 --------------------编程问答--------------------
这里包含了基本类型的,那个value,第二次设100,无效
补充:Java , Java相关