Java Socket收发异步长连接

最近做SSO的项目,其中用到了socket长连接.一直都是看代码的,但是要求socket做成SSL的,不得不动手写写代码了.下面我给出一个简单的socket长连接.
Java代码 复制代码 收藏代码
  1. SocketClient.java 
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.     } 
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/*{  user:jiangwh }*/

public class SocketClient {

	public static final Object locked = new Object();
	public static final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(
			1024 * 100);

	class SendThread extends Thread{
		private Socket socket;
		public SendThread(Socket socket) {
			this.socket = socket;
		}
		@Override
		public void run() {
			while(true){
				try {
					String send = getSend();			
					PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
					pw.write(send);
					pw.flush();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		public String getSend() throws InterruptedException{
			Thread.sleep(1000);
			return "<SOAP-ENV:Envelope>"+System.currentTimeMillis()+"</SOAP-ENV:Envelope>";
		}
	}

	class ReceiveThread extends Thread{
		private Socket socket;
		
		public ReceiveThread(Socket socket) {
			this.socket = socket;
		}

		@Override
		public void run() {
			while(true){
				try {					
					Reader reader = new InputStreamReader(socket.getInputStream());
					CharBuffer charBuffer = CharBuffer.allocate(8192);
					int index = -1;
					while((index=reader.read(charBuffer))!=-1){
						charBuffer.flip();
						System.out.println("client:"+charBuffer.toString());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void start() throws UnknownHostException, IOException{
		Socket socket = new Socket("10.10.148.40",18889);
		new SendThread(socket).start();
		new ReceiveThread(socket).start();
	}
	public static void main(String[] args) throws UnknownHostException, IOException {
		new SocketClient().start();
	}
}
Java代码 复制代码 收藏代码
  1. SocketServer.java 
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.     } 
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值