博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java长连接socket【转】http://jiewo.iteye.com/blog/1562168
阅读量:5142 次
发布时间:2019-06-13

本文共 3948 字,大约阅读时间需要 13 分钟。

博客分类:
  •  
 
最近做SSO的项目,其中用到了socket长连接.一直都是看代码的,但是要求socket做成SSL的,不得不动手写写代码了.下面我给出一个简单的socket长连接.
Java代码  
  1. SocketClient.java  
Java代码  
  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("10.10.148.40",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. }  
Java代码  
  1. SocketServer.java  
Java代码  
  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.     }  
  89. }  

转载于:https://www.cnblogs.com/songtzu/archive/2013/02/04/2891858.html

你可能感兴趣的文章