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

来源:互联网 发布:人工智能用途 编辑:程序博客网 时间:2024/06/05 06:57
 

最近在弄关于socket的东东,现总结如下:

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. }  
原创粉丝点击