socket, nio socket,及xml传递object 方法

来源:互联网 发布:吉利知豆d1怎么样 编辑:程序博客网 时间:2024/05/16 00:26
 

1:如何通过socket代理来访问服务端:

  1. String proxyHost = "192.168.204.212";   
  2. String proxyPort = "1080";   
  3.   
  4. //通知Java要通过代理进行连接。   
  5. System.getProperties().put("socksProxySet","true");   
  6. //指定代理所在的机器   
  7. System.getProperties().put("socksProxyHost",proxyHost);   
  8. //指定代理监听的端口。   
  9. System.getProperties().put("socksProxyPort",proxyPort);    
  10.   
  11. String host = "134.01.69.80";   
  12. int port = 12086;   
  13. System.out.println("connetioning:" + host + ":" + port);   
  14. server = new Socket(host, port);  

二:老socket传递Object对象:

要传递的对象:

  1. public class Employee implements Serializable {    
  2.   
  3.        private int employeeNumber;    
  4.        private String employeeName;    
  5.   
  6.        Employee(int num, String name) {    
  7.           employeeNumber = num;    
  8.           employeeName= name;    
  9.        }    
  10.   
  11.         public int getEmployeeNumber() {    
  12.           return employeeNumber ;    
  13.        }    
  14.   
  15.        public void setEmployeeNumber(int num) {    
  16.           employeeNumber = num;    
  17.        }    
  18.   
  19.        public String getEmployeeName() {    
  20.           return employeeName ;    
  21.        }    
  22.   
  23.        public void setEmployeeName(String name) {    
  24.           employeeName = name;    
  25.        }    
  26.     }   

client:

  1. public class Client {   
  2.     public static void main(String[] arg) {   
  3.         try {   
  4.             Employee joe = new Employee(150"Joe");   
  5.             System.out.println("employeeNumber= " + joe.getEmployeeNumber());   
  6.             System.out.println("employeeName= " + joe.getEmployeeName());   
  7.             Socket socketConnection = new Socket("127.0.0.1"11111);   
  8.             ObjectOutputStream clientOutputStream = new ObjectOutputStream(   
  9.                     socketConnection.getOutputStream());   
  10.             ObjectInputStream clientInputStream = new ObjectInputStream(   
  11.                     socketConnection.getInputStream());   
  12.             clientOutputStream.writeObject(joe);   
  13.             joe = (Employee) clientInputStream.readObject();   
  14.             System.out.println("employeeNumber= " + joe.getEmployeeNumber());   
  15.             System.out.println("employeeName= " + joe.getEmployeeName());   
  16.             clientOutputStream.close();   
  17.             clientInputStream.close();   
  18.         } catch (Exception e) {   
  19.             System.out.println(e);   
  20.         }   
  21.     }   
  22. }  

server端:

java 代码
  1. public class Server {   
  2.     public static void main(String[] arg) {   
  3.         Employee employee = null;   
  4.         try {   
  5.             ServerSocket socketConnection = new ServerSocket(11111);   
  6.             System.out.println("Server Waiting");   
  7.             Socket pipe = socketConnection.accept();   
  8.             ObjectInputStream serverInputStream = new ObjectInputStream(pipe   
  9.                     .getInputStream());   
  10.             ObjectOutputStream serverOutputStream = new ObjectOutputStream(pipe   
  11.                     .getOutputStream());   
  12.             employee = (Employee) serverInputStream.readObject();   
  13.             employee.setEmployeeNumber(256);   
  14.             employee.setEmployeeName("li");   
  15.             serverOutputStream.writeObject(employee);   
  16.             serverInputStream.close();   
  17.             serverOutputStream.close();   
  18.         } catch (Exception e) {   
  19.             System.out.println(e);   
  20.         }   
  21.     }   
  22. }  

三:nio socket传递Object:

client:

  1. public class Client {   
  2.     private String hostname;   
  3.        
  4.     private int port;   
  5.        
  6.     public Client(String hostname, int port)   
  7.     {   
  8.         this.hostname = hostname;   
  9.         this.port = port;   
  10.     }   
  11.   
  12.     public static void main(String[] args) {   
  13.         String hostname = "192.168.0.81";   
  14.         int port = 8234;   
  15.         Student stu = new Student();   
  16.         stu.setId(849);   
  17.         stu.setName("Squall");   
  18.         Client client = new Client(hostname, port);   
  19.         try {   
  20.             client.write(stu);   
  21.         } catch (IOException e) {   
  22.             // TODO Auto-generated catch block   
  23.             e.printStackTrace();   
  24.         }   
  25.     }   
  26.   
  27.     public void write(Object obj) throws IOException {   
  28.         SocketChannel channel = null;   
  29.         try {   
  30.             channel = SocketChannel.open(new InetSocketAddress(hostname, port));   
  31.             ByteBuffer buf = Client.getByteBuffer(obj);   
  32.             channel.write(Client.getByteBuffer(obj));   
  33.             channel.write(Client.getByteBuffer(obj));   
  34.         } catch (Exception e) {   
  35.             e.printStackTrace();   
  36.         } finally {   
  37.             channel.close();   
  38.         }   
  39.     }   
  40.        
  41.     public static ByteBuffer getByteBuffer(Object obj) throws IOException   
  42.     {   
  43.         ByteArrayOutputStream bOut = new ByteArrayOutputStream();   
  44.         ObjectOutputStream out = new ObjectOutputStream(bOut);   
  45.         out.writeObject(obj);   
  46.         out.flush();   
  47.         byte[] arr = bOut.toByteArray();   
  48.         System.out.println("Object in " + arr.length + " bytes");   
  49.         ByteBuffer bb = ByteBuffer.wrap(arr);   
  50.         out.close();   
  51.            
  52.         return bb;   
  53.     }   
  54. }  

server端:

java 代码
  1. public class Server {   
  2.   
  3.     public static void main(String[] args) {   
  4.         System.out.println("in server!");   
  5.         ServerThread server = new ServerThread();   
  6.         new Thread(server).start();   
  7.     }   
  8.   
  9.     static class ServerThread implements Runnable {   
  10.   
  11.         public void run() {   
  12.             try {   
  13.                 ServerSocketChannel sc = ServerSocketChannel.open();   
  14.   
  15.                 ServerSocket s = sc.socket();   
  16.                 s.bind(new InetSocketAddress(8234));   
  17.                 while (true) {   
  18.                     Socket incoming = s.accept();   
  19.                     Runnable r = new GetObjThread(incoming);   
  20.                     Thread t = new Thread(r);   
  21.                     t.start();   
  22.                 }   
  23.             } catch (Exception e) {   
  24.                 e.printStackTrace();   
  25.             }   
  26.         }   
  27.     }   
  28.   
  29.     static class GetObjThread implements Runnable {   
  30.         public GetObjThread(Socket s) {   
  31.             incoming = s;   
  32.         }   
  33.   
  34.         public void run() {   
  35.             try {   
  36.                 SocketChannel sc = incoming.getChannel();   
  37.                 ByteBuffer bbIn = ByteBuffer.allocate(1024);   
  38.                 sc.read(bbIn);   
  39.                    
  40.                 sc.close();   
  41.                 bbIn.flip();   
  42.                 ByteArrayInputStream bIn = new ByteArrayInputStream(bbIn   
  43.                         .array());   
  44.                 ObjectInputStream in = new ObjectInputStream(bIn);   
  45.                 Student nStu = (Student) in.readObject();   
  46.                 System.out.println("student id is " + nStu.getId() + "/n"  
  47.                         + "student name is " + nStu.getName());   
  48.             } catch (IOException e) {   
  49.                 e.printStackTrace();   
  50.             } catch (ClassNotFoundException e) {   
  51.                 e.printStackTrace();   
  52.             }   
  53.         }   
  54.   
  55.         private Socket incoming;   
  56.     }   
  57. }  

四:备份一个有用的util class:对象序列化,反序列化(序列化对象转byte[],ByteBuffer, byte[]转object: 

java 代码
  1. public class ByteUtil {   
  2.     public static byte[] getBytes(Object obj) throws IOException   
  3.     {   
  4.         ByteArrayOutputStream bout = new ByteArrayOutputStream();   
  5.         ObjectOutputStream out = new ObjectOutputStream(bout);   
  6.         out.writeObject(obj);   
  7.         out.flush();   
  8.         byte[] bytes = bout.toByteArray();   
  9.         bout.close();   
  10.         out.close();   
  11.            
  12.         return bytes;   
  13.     }   
  14.        
  15.     public static Object getObject(byte[] bytes) throws IOException, ClassNotFoundException   
  16.     {   
  17.            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);   
  18.            ObjectInputStream oi = new ObjectInputStream(bi);   
  19.            Object obj = oi.readObject();   
  20.            bi.close();   
  21.            oi.close();   
  22.         return obj;   
  23.     }   
  24.        
  25.     public static ByteBuffer getByteBuffer(Object obj) throws IOException   
  26.     {   
  27.         byte[] bytes = ByteUtil.getBytes(obj);   
  28.         ByteBuffer buff = ByteBuffer.wrap(bytes);   
  29.            
  30.         return buff;   
  31.     }   
  32. }  

五:如何通过xml传递Object对象:

可以先把object转成一个byte[]数组,然后用base64编码成一个base64格式的String,放入xml的CDATA中,就可以传了。

接收方,收到该xml后,把CDATA中的String用base64解码为byte[],进而根据四中的方法,还原为object:

java 代码
  1. public class Base64 {   
  2.   
  3.     public static String getEncodedText(byte[] bytes) {   
  4.   
  5.         try {   
  6.             BASE64Encoder encoder = new BASE64Encoder();   
  7.             String text = encoder.encode(bytes);   
  8.             return text;           
  9.         } catch (Exception e) {   
  10.             e.printStackTrace();   
  11.             return null;   
  12.         }          
  13.   
  14.     }   
  15.        
  16.     public static byte[] decode(String src)    
  17.     {   
  18.         BASE64Decoder decoder = new BASE64Decoder();   
  19.         try {   
  20.             return decoder.decodeBuffer(src);   
  21.         } catch (IOException e) {   
  22.             // TODO Auto-generated catch block   
  23.             e.printStackTrace();   
  24.             return null;   
  25.         }   
  26.     }   
  27.        
  28.     public static void main(String[] args) {   
  29.         String s = "ly89";   
  30.         byte[] bytes = s.getBytes();   
  31.         String  encode = Base64.getEncodedText(bytes);   
  32.         System.out.println("the encode string is: " + encode);   
  33.   
  34.         byte[] dbytes = Base64.decode(encode);   
  35.         for (int i = 0; i < bytes.length; i++) {   
  36.             System.out.println(dbytes[i]);             
  37.         }   
  38.     }   
  39. }