欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Socket收发异步长连接

程序员文章站 2022-06-17 19:30:48
...
  1. import java.io.IOException;  
  2. import java.io.InputStreamReader;  
  3. import java.io.OutputStreamWriter;  
  4. import java.io.PrintWriter;  
  5. import java.io.Reader;  
  6. import java.net.Socket;  
  7. import java.net.UnknownHostException;  
  8. import java.nio.CharBuffer;  
  9. import java.util.concurrent.ArrayBlockingQueue;  
  10. import java.util.concurrent.BlockingQueue;  
  11.   
  12. /*{  user:jiangwh }*/  
  13.   
  14. public class SocketClient {  
  15.   
  16.     public static final Object locked = new Object();  
  17.     public static final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(  
  18.             1024 * 100);  
  19.   
  20.     class SendThread extends Thread{  
  21.         private Socket socket;  
  22.         public SendThread(Socket socket) {  
  23.             this.socket = socket;  
  24.         }  
  25.         @Override  
  26.         public void run() {  
  27.             while(true){  
  28.                 try {  
  29.                     String send = getSend();              
  30.                     PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));  
  31.                     pw.write(send);  
  32.                     pw.flush();  
  33.                 } catch (Exception e) {  
  34.                     e.printStackTrace();  
  35.                 }  
  36.             }  
  37.         }  
  38.         public String getSend() throws InterruptedException{  
  39.             Thread.sleep(1000);  
  40.             return "<SOAP-ENV:Envelope>"+System.currentTimeMillis()+"</SOAP-ENV:Envelope>";  
  41.         }  
  42.     }  
  43.   
  44.     class ReceiveThread extends Thread{  
  45.         private Socket socket;  
  46.           
  47.         public ReceiveThread(Socket socket) {  
  48.             this.socket = socket;  
  49.         }  
  50.   
  51.         @Override  
  52.         public void run() {  
  53.             while(true){  
  54.                 try {                     
  55.                     Reader reader = new InputStreamReader(socket.getInputStream());  
  56.                     CharBuffer charBuffer = CharBuffer.allocate(8192);  
  57.                     int index = -1;  
  58.                     while((index=reader.read(charBuffer))!=-1){  
  59.                         charBuffer.flip();  
  60.                         System.out.println("client:"+charBuffer.toString());  
  61.                     }  
  62.                 } catch (Exception e) {  
  63.                     e.printStackTrace();  
  64.                 }  
  65.             }  
  66.         }  
  67.     }  
  68.       
  69.     public void start() throws UnknownHostException, IOException{  
  70.         Socket socket = new Socket("127.0.0.1",18889);  
  71.         new SendThread(socket).start();  
  72.         new ReceiveThread(socket).start();  
  73.     }  
  74.     public static void main(String[] args) throws UnknownHostException, IOException {  
  75.         new SocketClient().start();  
  76.     }  
  77. }  

 

  1. import java.io.IOException; 
  2. import java.io.InputStreamReader; 
  3. import java.io.OutputStreamWriter; 
  4. import java.io.PrintWriter; 
  5. import java.io.Reader; 
  6. import java.io.Writer; 
  7. import java.net.ServerSocket; 
  8. import java.net.Socket; 
  9. import java.nio.CharBuffer; 
  10. import java.util.Date; 
  11.  
  12. /*{user:jiangwh }*/ 
  13.  
  14. public class SocketServer { 
  15.  
  16.     private final static String SOAP_BEGIN = "<SOAP-ENV:Envelope"
  17.     private final static String SOAP_END = "</SOAP-ENV:Envelope>"
  18.  
  19.     public static void main(String[] args) throws IOException { 
  20.         SocketServer socketServer = new SocketServer(); 
  21.         socketServer.start(); 
  22.     } 
  23.  
  24.     public void start() throws IOException { 
  25.         ServerSocket serverSocket = new ServerSocket(18889); 
  26.         while (true) { 
  27.             Socket socket = serverSocket.accept(); 
  28.             new SocketThread(socket).start(); 
  29.         } 
  30.     } 
  31.  
  32.     class SocketThread extends Thread { 
  33.         private Socket socket; 
  34.         private String temp; 
  35.  
  36.         public Socket getSocket() { 
  37.             return socket; 
  38.         } 
  39.  
  40.         public void setSocket(Socket socket) { 
  41.             this.socket = socket; 
  42.         } 
  43.  
  44.         public SocketThread(Socket socket) { 
  45.             this.socket = socket; 
  46.         } 
  47.  
  48.         public void run() { 
  49.             try 
  50.                 Reader reader = new InputStreamReader(socket.getInputStream()); 
  51.                 Writer writer = new PrintWriter(new OutputStreamWriter(socket 
  52.                         .getOutputStream(), "GBK")); 
  53.                 CharBuffer charBuffer = CharBuffer.allocate(8192); 
  54.                 int readIndex = -1
  55.                 while ((readIndex = reader.read(charBuffer)) != -1) { 
  56.                     charBuffer.flip(); 
  57.                     temp += charBuffer.toString(); 
  58.                     if (temp.indexOf(SOAP_BEGIN) != -1 
  59.                             && temp.indexOf(SOAP_END) != -1) { 
  60.                         // 传送一个soap报文 
  61.                         System.out.println(new Date().toLocaleString()+"server:"+temp); 
  62.                         temp=""
  63.                         writer.write("receive the soap message"); 
  64.                         writer.flush(); 
  65.                     } else if (temp.indexOf(SOAP_BEGIN) != -1) { 
  66.                         // 包含开始,但不包含 
  67.                         temp = temp.substring(temp.indexOf(SOAP_BEGIN)); 
  68.                     }    
  69.                     if (temp.length() > 1024 * 16) { 
  70.                         break
  71.                     } 
  72.                 } 
  73.             } catch (Exception e) { 
  74.                 e.printStackTrace(); 
  75.             } finally 
  76.                 if (socket != null) { 
  77.                     if (!socket.isClosed()) { 
  78.                         try 
  79.                             socket.close(); 
  80.                         } catch (IOException e) { 
  81.                             e.printStackTrace(); 
  82.                         } 
  83.                     } 
  84.                 } 
  85.             } 
  86.  
  87.         } 
  88.     } 

                          转载自:http://blog.csdn.net/liuzesoft/article/details/30247649