java socket 长连接

最近做SSO的项目,其中用到了socket长连接.一直都是看代码的,但是要求socket做成SSL的,不得不动手写写代码了.下面我给出一个简单的socket长连接.

Java代码   java 短链接生成 java 长链接_Java

  1. SocketClient.java  

Java代码   java 短链接生成 java 长链接_Java

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

Java代码   java 短链接生成 java 长链接_Java

  1. SocketServer.java  

Java代码   java 短链接生成 java 长链接_Java

  1. import
  2. import
  3. import
  4. import
  5. import
  6. import
  7. import
  8. import
  9. import
  10. import
  11.   
  12. /*{user:jiangwh }*/
  13.   
  14. public class
  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
  20. new
  21.         socketServer.start();  
  22.     }  
  23.   
  24. public void start() throws
  25. new ServerSocket(18889);  
  26. while (true) {  
  27.             Socket socket = serverSocket.accept();  
  28. new
  29.         }  
  30.     }  
  31.   
  32. class SocketThread extends
  33. private
  34. private
  35.   
  36. public
  37. return
  38.         }  
  39.   
  40. public void
  41. this.socket = socket;  
  42.         }  
  43.   
  44. public
  45. this.socket = socket;  
  46.         }  
  47.   
  48. public void
  49. try
  50. new
  51. new PrintWriter(new
  52. "GBK"));  
  53. 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. 1) {  
  60. // 传送一个soap报文
  61. new Date().toLocaleString()+"server:"+temp);  
  62. "";  
  63. "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
  74.                 e.printStackTrace();  
  75. finally
  76. if (socket != null) {  
  77. if
  78. try
  79.                             socket.close();  
  80. catch
  81.                             e.printStackTrace();  
  82.                         }  
  83.                     }  
  84.                 }  
  85.             }  
  86.   
  87.         }  
  88.     }  
  89. }