socket, nio socket,及xml传递object 方法
来源:互联网 发布:如何看懂单片机原理图 编辑:程序博客网 时间:2024/06/05 03:32
1:如何通过socket代理来访问服务端:
String proxyHost = "192.168.204.212";
String proxyPort = "1080";
//通知Java要通过代理进行连接。
System.getProperties().put("socksProxySet","true");
//指定代理所在的机器
System.getProperties().put("socksProxyHost",proxyHost);
//指定代理监听的端口。
System.getProperties().put("socksProxyPort",proxyPort);
String host = "134.01.69.80";
int port = 12086;
System.out.println("connetioning:" + host + ":" + port);
server = new Socket(host, port);
String proxyPort = "1080";
//通知Java要通过代理进行连接。
System.getProperties().put("socksProxySet","true");
//指定代理所在的机器
System.getProperties().put("socksProxyHost",proxyHost);
//指定代理监听的端口。
System.getProperties().put("socksProxyPort",proxyPort);
String host = "134.01.69.80";
int port = 12086;
System.out.println("connetioning:" + host + ":" + port);
server = new Socket(host, port);
二:老socket传递Object对象:
要传递的对象:
public class Employee implements Serializable ...{
private int employeeNumber;
private String employeeName;
Employee(int num, String name) ...{
employeeNumber = num;
employeeName= name;
}
public int getEmployeeNumber() ...{
return employeeNumber ;
}
public void setEmployeeNumber(int num) ...{
employeeNumber = num;
}
public String getEmployeeName() ...{
return employeeName ;
}
public void setEmployeeName(String name) ...{
employeeName = name;
}
}
private int employeeNumber;
private String employeeName;
Employee(int num, String name) ...{
employeeNumber = num;
employeeName= name;
}
public int getEmployeeNumber() ...{
return employeeNumber ;
}
public void setEmployeeNumber(int num) ...{
employeeNumber = num;
}
public String getEmployeeName() ...{
return employeeName ;
}
public void setEmployeeName(String name) ...{
employeeName = name;
}
}
client:
public class Client ...{
public static void main(String[] arg) ...{
try ...{
Employee joe = new Employee(150, "Joe");
System.out.println("employeeNumber= " + joe.getEmployeeNumber());
System.out.println("employeeName= " + joe.getEmployeeName());
Socket socketConnection = new Socket("127.0.0.1", 11111);
ObjectOutputStream clientOutputStream = new ObjectOutputStream(
socketConnection.getOutputStream());
ObjectInputStream clientInputStream = new ObjectInputStream(
socketConnection.getInputStream());
clientOutputStream.writeObject(joe);
joe = (Employee) clientInputStream.readObject();
System.out.println("employeeNumber= " + joe.getEmployeeNumber());
System.out.println("employeeName= " + joe.getEmployeeName());
clientOutputStream.close();
clientInputStream.close();
} catch (Exception e) ...{
System.out.println(e);
}
}
}
public static void main(String[] arg) ...{
try ...{
Employee joe = new Employee(150, "Joe");
System.out.println("employeeNumber= " + joe.getEmployeeNumber());
System.out.println("employeeName= " + joe.getEmployeeName());
Socket socketConnection = new Socket("127.0.0.1", 11111);
ObjectOutputStream clientOutputStream = new ObjectOutputStream(
socketConnection.getOutputStream());
ObjectInputStream clientInputStream = new ObjectInputStream(
socketConnection.getInputStream());
clientOutputStream.writeObject(joe);
joe = (Employee) clientInputStream.readObject();
System.out.println("employeeNumber= " + joe.getEmployeeNumber());
System.out.println("employeeName= " + joe.getEmployeeName());
clientOutputStream.close();
clientInputStream.close();
} catch (Exception e) ...{
System.out.println(e);
}
}
}
server端:
public class Server ...{
public static void main(String[] arg) ...{
Employee employee = null;
try ...{
ServerSocket socketConnection = new ServerSocket(11111);
System.out.println("Server Waiting");
Socket pipe = socketConnection.accept();
ObjectInputStream serverInputStream = new ObjectInputStream(pipe
.getInputStream());
ObjectOutputStream serverOutputStream = new ObjectOutputStream(pipe
.getOutputStream());
employee = (Employee) serverInputStream.readObject();
employee.setEmployeeNumber(256);
employee.setEmployeeName("li");
serverOutputStream.writeObject(employee);
serverInputStream.close();
serverOutputStream.close();
} catch (Exception e) ...{
System.out.println(e);
}
}
}
public static void main(String[] arg) ...{
Employee employee = null;
try ...{
ServerSocket socketConnection = new ServerSocket(11111);
System.out.println("Server Waiting");
Socket pipe = socketConnection.accept();
ObjectInputStream serverInputStream = new ObjectInputStream(pipe
.getInputStream());
ObjectOutputStream serverOutputStream = new ObjectOutputStream(pipe
.getOutputStream());
employee = (Employee) serverInputStream.readObject();
employee.setEmployeeNumber(256);
employee.setEmployeeName("li");
serverOutputStream.writeObject(employee);
serverInputStream.close();
serverOutputStream.close();
} catch (Exception e) ...{
System.out.println(e);
}
}
}
三:nio socket传递Object:
client:
public class Client ...{
private String hostname;
private int port;
public Client(String hostname, int port)
...{
this.hostname = hostname;
this.port = port;
}
public static void main(String[] args) ...{
String hostname = "192.168.0.81";
int port = 8234;
Student stu = new Student();
stu.setId(849);
stu.setName("Squall");
Client client = new Client(hostname, port);
try ...{
client.write(stu);
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(Object obj) throws IOException ...{
SocketChannel channel = null;
try ...{
channel = SocketChannel.open(new InetSocketAddress(hostname, port));
ByteBuffer buf = Client.getByteBuffer(obj);
channel.write(Client.getByteBuffer(obj));
channel.write(Client.getByteBuffer(obj));
} catch (Exception e) ...{
e.printStackTrace();
} finally ...{
channel.close();
}
}
public static ByteBuffer getByteBuffer(Object obj) throws IOException
...{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bOut);
out.writeObject(obj);
out.flush();
byte[] arr = bOut.toByteArray();
System.out.println("Object in " + arr.length + " bytes");
ByteBuffer bb = ByteBuffer.wrap(arr);
out.close();
return bb;
}
}
private String hostname;
private int port;
public Client(String hostname, int port)
...{
this.hostname = hostname;
this.port = port;
}
public static void main(String[] args) ...{
String hostname = "192.168.0.81";
int port = 8234;
Student stu = new Student();
stu.setId(849);
stu.setName("Squall");
Client client = new Client(hostname, port);
try ...{
client.write(stu);
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(Object obj) throws IOException ...{
SocketChannel channel = null;
try ...{
channel = SocketChannel.open(new InetSocketAddress(hostname, port));
ByteBuffer buf = Client.getByteBuffer(obj);
channel.write(Client.getByteBuffer(obj));
channel.write(Client.getByteBuffer(obj));
} catch (Exception e) ...{
e.printStackTrace();
} finally ...{
channel.close();
}
}
public static ByteBuffer getByteBuffer(Object obj) throws IOException
...{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bOut);
out.writeObject(obj);
out.flush();
byte[] arr = bOut.toByteArray();
System.out.println("Object in " + arr.length + " bytes");
ByteBuffer bb = ByteBuffer.wrap(arr);
out.close();
return bb;
}
}
server端:
public class Server ...{
public static void main(String[] args) ...{
System.out.println("in server!");
ServerThread server = new ServerThread();
new Thread(server).start();
}
static class ServerThread implements Runnable ...{
public void run() ...{
try ...{
ServerSocketChannel sc = ServerSocketChannel.open();
ServerSocket s = sc.socket();
s.bind(new InetSocketAddress(8234));
while (true) ...{
Socket incoming = s.accept();
Runnable r = new GetObjThread(incoming);
Thread t = new Thread(r);
t.start();
}
} catch (Exception e) ...{
e.printStackTrace();
}
}
}
static class GetObjThread implements Runnable ...{
public GetObjThread(Socket s) ...{
incoming = s;
}
public void run() ...{
try ...{
SocketChannel sc = incoming.getChannel();
ByteBuffer bbIn = ByteBuffer.allocate(1024);
sc.read(bbIn);
sc.close();
bbIn.flip();
ByteArrayInputStream bIn = new ByteArrayInputStream(bbIn
.array());
ObjectInputStream in = new ObjectInputStream(bIn);
Student nStu = (Student) in.readObject();
System.out.println("student id is " + nStu.getId() + " "
+ "student name is " + nStu.getName());
} catch (IOException e) ...{
e.printStackTrace();
} catch (ClassNotFoundException e) ...{
e.printStackTrace();
}
}
private Socket incoming;
}
}
public static void main(String[] args) ...{
System.out.println("in server!");
ServerThread server = new ServerThread();
new Thread(server).start();
}
static class ServerThread implements Runnable ...{
public void run() ...{
try ...{
ServerSocketChannel sc = ServerSocketChannel.open();
ServerSocket s = sc.socket();
s.bind(new InetSocketAddress(8234));
while (true) ...{
Socket incoming = s.accept();
Runnable r = new GetObjThread(incoming);
Thread t = new Thread(r);
t.start();
}
} catch (Exception e) ...{
e.printStackTrace();
}
}
}
static class GetObjThread implements Runnable ...{
public GetObjThread(Socket s) ...{
incoming = s;
}
public void run() ...{
try ...{
SocketChannel sc = incoming.getChannel();
ByteBuffer bbIn = ByteBuffer.allocate(1024);
sc.read(bbIn);
sc.close();
bbIn.flip();
ByteArrayInputStream bIn = new ByteArrayInputStream(bbIn
.array());
ObjectInputStream in = new ObjectInputStream(bIn);
Student nStu = (Student) in.readObject();
System.out.println("student id is " + nStu.getId() + " "
+ "student name is " + nStu.getName());
} catch (IOException e) ...{
e.printStackTrace();
} catch (ClassNotFoundException e) ...{
e.printStackTrace();
}
}
private Socket incoming;
}
}
四:备份一个有用的util class:对象序列化,反序列化(序列化对象转byte[],ByteBuffer, byte[]转object: 五:如何通过xml传递Object对象:
public class ByteUtil ...{
public static byte[] getBytes(Object obj) throws IOException
...{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(obj);
out.flush();
byte[] bytes = bout.toByteArray();
bout.close();
out.close();
return bytes;
}
public static Object getObject(byte[] bytes) throws IOException, ClassNotFoundException
...{
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
Object obj = oi.readObject();
bi.close();
oi.close();
return obj;
}
public static ByteBuffer getByteBuffer(Object obj) throws IOException
...{
byte[] bytes = ByteUtil.getBytes(obj);
ByteBuffer buff = ByteBuffer.wrap(bytes);
return buff;
}
}
public static byte[] getBytes(Object obj) throws IOException
...{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(obj);
out.flush();
byte[] bytes = bout.toByteArray();
bout.close();
out.close();
return bytes;
}
public static Object getObject(byte[] bytes) throws IOException, ClassNotFoundException
...{
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
Object obj = oi.readObject();
bi.close();
oi.close();
return obj;
}
public static ByteBuffer getByteBuffer(Object obj) throws IOException
...{
byte[] bytes = ByteUtil.getBytes(obj);
ByteBuffer buff = ByteBuffer.wrap(bytes);
return buff;
}
}
可以先把object转成一个byte[]数组,然后用base64编码成一个base64格式的String,放入xml的CDATA中,就可以传了。
接收方,收到该xml后,把CDATA中的String用base64解码为byte[],进而根据四中的方法,还原为object:
public class Base64 ...{
public static String getEncodedText(byte[] bytes) ...{
try ...{
BASE64Encoder encoder = new BASE64Encoder();
String text = encoder.encode(bytes);
return text;
} catch (Exception e) ...{
e.printStackTrace();
return null;
}
}
public static byte[] decode(String src)
...{
BASE64Decoder decoder = new BASE64Decoder();
try ...{
return decoder.decodeBuffer(src);
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static void main(String[] args) ...{
String s = "ly89";
byte[] bytes = s.getBytes();
String encode = Base64.getEncodedText(bytes);
System.out.println("the encode string is: " + encode);
byte[] dbytes = Base64.decode(encode);
for (int i = 0; i < bytes.length; i++) ...{
System.out.println(dbytes[i]);
}
}
}
public static String getEncodedText(byte[] bytes) ...{
try ...{
BASE64Encoder encoder = new BASE64Encoder();
String text = encoder.encode(bytes);
return text;
} catch (Exception e) ...{
e.printStackTrace();
return null;
}
}
public static byte[] decode(String src)
...{
BASE64Decoder decoder = new BASE64Decoder();
try ...{
return decoder.decodeBuffer(src);
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static void main(String[] args) ...{
String s = "ly89";
byte[] bytes = s.getBytes();
String encode = Base64.getEncodedText(bytes);
System.out.println("the encode string is: " + encode);
byte[] dbytes = Base64.decode(encode);
for (int i = 0; i < bytes.length; i++) ...{
System.out.println(dbytes[i]);
}
}
}
- socket, nio socket,及xml传递object 方法
- socket, nio socket,及xml传递object 方法
- socket, nio socket,及xml传递object 方法
- socket, nio socket,及xml传递object 方法
- socket, nio socket,及xml传递object 方法
- socket, nio socket,及xml传递object 方法
- socket, nio socket,及xml传递object 方法
- socket, nio socket,及xml传递object 方法
- socket, nio socket 及nio socket框架MINA总结
- socket, nio socket 及nio socket框架MINA总结
- socket, nio socket 及nio socket框架MINA总结
- socket, nio socket 及nio socket框架MINA总结 (转)
- socket, nio socket 及nio socket框架MINA总结
- NIO socket
- NIO socket
- Java NIO 与 Nio Socket
- Java Socket 通信中传递Object对象注意的问题
- Java Socket 通信中传递Object对象注意的问题
- 基于.NET的AOP开源框架PostSharp 1.0 beta发布
- 给初学Java者的五点建议
- v在JAVA文件中获取该项目的相对路径
- 1.3.2 PHP标记风格
- .net下AOP框架资料
- socket, nio socket,及xml传递object 方法
- 1.3.3 PHP语句
- .net绝对技术资料
- 1.3.4 空格
- Attribute在拦截机制上的应用
- DIV 样式设置和位置定位设置,图片变小手
- Acegi学习小结
- C++面向对象程序设计
- AOP技术介绍--(引言)