当前位置: 技术问答>java相关
如何通过Socket传输对象?
来源: 互联网 发布时间:2015-08-19
本文导语: 有一个Vector v对象,需要通过Socket从客户端传给服务器端,服务器端再把它还原为Vector。该如何实现呢? | //server import java.net.*; import java.io.*; class ObjectServer{ public static void main(String[] arg...
有一个Vector v对象,需要通过Socket从客户端传给服务器端,服务器端再把它还原为Vector。该如何实现呢?
|
//server
import java.net.*;
import java.io.*;
class ObjectServer{
public static void main(String[] args)
throws IOException{
ServerSocket server=new ServerSocket(5918);
Socket client=server.accept();
ObjectOutputStream out=new ObjectOutputStream(client.getOutputStream());
MyClass myObject=new MyClass();
out.writeObject(myObject);
out.flush();
out.close();
}
}
//classOne==============
imort java.io.*;
public class MyClass implements Serializable{
private String str="ok";
private int i=10;
private MyMember mySubObject=new MyMember();
public String getStr(){
return str;
}
public int getInt(){
return i;
}
public MyMember getSubObject(){
return mySubObject;
}
}
//classOne's member================
import java.io.*;
public class MyMember implements Serializable{ //the member var must implement Serializable
private char[] myArray=new char[]{'a','b','c'};
public char[] getArray(){
return myArray;
}
}
//client================
import java.io.*;
import java.net.*;
class ObjectClient{
public static void main(String[] args)
throws Exception{
Socket client=new Socket("localhost",5918);
ObjectInputStream in=new ObjectInputStream(client.getInputStream());
MyClass myObject=(MyClass)in.readObject();
System.out.println(myObject.getInt());
System.out.println(myObject.getStr());
char[] myArry=((MyMember)myObject.getSubObject()).getArray();
System.out.println(new String(myArry));
in.close();
}
}
import java.net.*;
import java.io.*;
class ObjectServer{
public static void main(String[] args)
throws IOException{
ServerSocket server=new ServerSocket(5918);
Socket client=server.accept();
ObjectOutputStream out=new ObjectOutputStream(client.getOutputStream());
MyClass myObject=new MyClass();
out.writeObject(myObject);
out.flush();
out.close();
}
}
//classOne==============
imort java.io.*;
public class MyClass implements Serializable{
private String str="ok";
private int i=10;
private MyMember mySubObject=new MyMember();
public String getStr(){
return str;
}
public int getInt(){
return i;
}
public MyMember getSubObject(){
return mySubObject;
}
}
//classOne's member================
import java.io.*;
public class MyMember implements Serializable{ //the member var must implement Serializable
private char[] myArray=new char[]{'a','b','c'};
public char[] getArray(){
return myArray;
}
}
//client================
import java.io.*;
import java.net.*;
class ObjectClient{
public static void main(String[] args)
throws Exception{
Socket client=new Socket("localhost",5918);
ObjectInputStream in=new ObjectInputStream(client.getInputStream());
MyClass myObject=(MyClass)in.readObject();
System.out.println(myObject.getInt());
System.out.println(myObject.getStr());
char[] myArry=((MyMember)myObject.getSubObject()).getArray();
System.out.println(new String(myArry));
in.close();
}
}
|
把传输对象序列化,你Vector中的对象能序列化吗?
|
Vector vec = new Vector();
vec.add("你的对象");
try{
ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());
oos.writeObject(vec);
oos.flush();
oos.close();
}catch(IOException e){
throw new ServletException("Failed to return.",e);
}
看看这段代码有什么启发.res.getOutputStream())其中的res是Response对象
vec.add("你的对象");
try{
ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());
oos.writeObject(vec);
oos.flush();
oos.close();
}catch(IOException e){
throw new ServletException("Failed to return.",e);
}
看看这段代码有什么启发.res.getOutputStream())其中的res是Response对象
|
Please try the "Java Serialization for XML"
http://freshmeat.net/projects/jsx/?topic_id=868%2C45%2C50%2C19%2C866
Java Serialization for XML (JSX) enables all objects to be written and read as XML, using Java's standard Serialization API.
The receiver can validate if the received the XML message is complete.
http://freshmeat.net/projects/jsx/?topic_id=868%2C45%2C50%2C19%2C866
Java Serialization for XML (JSX) enables all objects to be written and read as XML, using Java's standard Serialization API.
The receiver can validate if the received the XML message is complete.
|
还原不用等到全部接收完毕啊,ObjectInputStream.readObject()在没接收完时是阻塞的。
|
就不用socket传输沙!用rmi非常简单
|
学习!
|
可以用rmi实现,另外还要看你的Vector中存储的对象是什么类型的?
|
对象需要序列化!!!!!!!!!!!,也就是implements java.io.Serializable