java Socket長連接與消息推送源碼與演示

演示視頻以下(在服務端輸入消息,客戶端接收消息) : java

視頻連接
緩存


 

服務端代碼 MyServer.java 源碼以下socket

  1 import java.io.BufferedReader;
  2 import java.io.IOException;
  3 import java.io.InputStreamReader;
  4 import java.io.OutputStream;
  5 import java.io.PrintWriter;
  6 import java.net.ServerSocket;
  7 import java.net.Socket;
  8 import java.util.HashMap;
  9 import java.util.Iterator;
 10 import java.util.Map;
 11 import java.util.Scanner;
 12 
 13 
 14 public class MyServer {
 15     
 16     private static final Map<String,Socket> connectMap = new HashMap<String,Socket>(); //連接信息
 17     
 18     private static ServerSocket server = null ;
 19     
 20     private static final int PORT = 8888 ;
 21     
 22     //SOCKET 服務線程類
 23     static class ReciveService extends Thread{
 24         
 25         Socket client = null ; 
 26         
 27         private InputStreamReader is = null;
 28         private BufferedReader in = null; 
 29         
 30         public ReciveService(Socket client){
 31             this.client = client ;
 32         }
 33 
 34         public void run() {
 35 
 36             long tid =Thread.currentThread().getId() ;//當前線程ID
 37             try{
 38                 String IP = client.getInetAddress().getHostAddress() ;
 39                 
 40                 System.out.println("已經連接 : "+IP+"["+tid+"]");
 41                 connectMap.put(IP+"#"+tid,client);
 42                 is = new InputStreamReader(client.getInputStream(), "UTF-8");//規定編碼
 43                 in = new BufferedReader(is);//接收請求的流
 44                 
 45                 int len = 0;//監聽到的字符串長度
 46                 char[] buf = new char[1024];
 47                 while ((len = in.read(buf)) != -1) {
 48                     String s = new String(buf, 0, len);
 49                     s = new String(s.getBytes(),"utf-8");
 50                     System.out.println("-> "+s);
 51                 }
 52             }catch(Exception e){
 53                 e.printStackTrace() ;
 54             }finally{
 55                 //關閉Socket
 56                 if(null != client){
 57                     try {
 58                         client.close() ;
 59                     } catch (IOException e) {
 60                         e.printStackTrace();
 61                     }
 62                 }
 63             }
 64         
 65         }
 66         
 67         
 68     }
 69     
 70     //向客戶端發送消息
 71     private static Thread tSend = new Thread(new Runnable(){
 72         Scanner scanner =new Scanner(System.in);
 73         public void run() {
 74             try {
 75                 while (true) {
 76                     String data = scanner.next() ;
 77                     for(Iterator<String> it = connectMap.keySet().iterator();it.hasNext();){
 78                         String key = it.next() ;//IP+線程ID
 79                         Socket client = connectMap.get(key);
 80                         if(null != client && client.isConnected()){
 81                             OutputStream os= client.getOutputStream();            
 82                             PrintWriter pw=new PrintWriter(os);//轉化爲打印流  
 83                             pw.write(data);  
 84                             pw.flush();//刷新緩存
 85                         }
 86                     }
 87                     Thread.sleep(1000);
 88 //                    System.out.println("...");
 89                 }
 90             } catch (IOException e) {
 91                 e.printStackTrace();
 92             } catch (InterruptedException e) {
 93                 e.printStackTrace();
 94             }
 95         }
 96     });
 97     
 98     
 99     
100     
101     public static void main(String[] args) throws IOException {
102         System.out.println("==============SOCKET 服務端===============");
103         startServer() ;
104     }
105     
106     public static void startServer() throws IOException{
107         server = new ServerSocket(PORT);
108         tSend.start();                                            //啓動發送消息線程
109         while(true){
110             try{
111                 Socket client = server.accept() ;                 //主線程獲取客戶端鏈接
112                 ReciveService service = new ReciveService(client) ;
113                 service.start();                                //啓動接收消息線程
114             }catch(Exception e){
115                 e.printStackTrace() ;
116             }
117         }
118     }
119 }

客戶端代碼 MyClient.java 源碼以下 :this

  1 import java.io.IOException;
  2 import java.io.InputStream;
  3 import java.io.OutputStream;
  4 import java.net.Socket;
  5 import java.net.UnknownHostException;
  6 import java.util.Scanner;
  7 
  8 public class MyClient {
  9 
 10     private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket>(); 
 11     
 12     private static final String HOST = "localhost";
 13 
 14     private static final int PORT = 8888;
 15     
 16     private static Socket client;
 17     
 18     private static OutputStream outStr = null;
 19     
 20     private static InputStream inStr = null;
 21     
 22     //數據接收線程
 23     private static Thread tRecv = new Thread(new Runnable(){
 24         public void run() {
 25             try {
 26 //              System.out.println("==============開始接收數據===============");
 27                 while (true) {
 28                     byte[] b = new byte[1024];
 29                     int r = inStr.read(b);
 30                     if(r>-1){
 31                         String str = new String(b,0,r,"utf-8");
 32                         System.out.println( str );
 33                     }
 34                 }
 35             } catch (IOException e) {
 36                 e.printStackTrace();
 37             }
 38         }
 39     });
 40     
 41     //消息發送線程
 42     private static Thread tSend = new Thread(new Runnable(){
 43         Scanner scanner =new Scanner(System.in);
 44         public void run() {
 45 //            System.out.println("=====================開始發送數據==============");
 46             try {
 47                 while (true) {
 48                     String data = scanner.next() ;
 49                     outStr.write(data.getBytes());
 50                 }
 51             } catch (IOException e) {
 52                 e.printStackTrace();
 53             }
 54         }
 55         
 56     });
 57     
 58     //心跳包發送線程
 59     private static Thread tKeep = new Thread(new Runnable(){
 60         public void run() {
 61             try {
 62 //              System.out.println("=====================開始發送心跳包==============");
 63                 while (true) {
 64                     try {
 65                         //http://blog.csdn.net/qq_23167527/article/details/54290726
 66                         Thread.sleep(30000);//心跳頻率 30秒 
 67 //                      System.out.println(".");
 68                     } catch (InterruptedException e) {
 69                         e.printStackTrace();
 70                     }
 71                   //發送一個心跳數據包
 72                     outStr.write(new byte[]{});//發送非空包也能夠
 73                 }
 74             } catch (IOException e) {
 75                 e.printStackTrace();
 76             }
 77         }
 78     });
 79     
 80     /**
 81      * @param args
 82      */
 83     public static void main(String[] args) {
 84         System.out.println("==============SOCKET 客戶端===============");
 85         
 86         try {
 87             connect();
 88             tRecv.start() ;
 89         } catch (UnknownHostException e) {
 90             e.printStackTrace();
 91         } catch (IOException e) {
 92             e.printStackTrace();
 93         }
 94         
 95     }
 96     
 97     public static void connect() throws UnknownHostException, IOException  {
 98         client = threadConnect.get();
 99         if(client == null){
100             client = new Socket(HOST, PORT);
101             threadConnect.set(client);
102             tKeep.start();//啓動長鏈接心跳保活線程
103             tSend.start() ;
104 //          System.out.println("========連接開始!========");
105         }
106         outStr = client.getOutputStream();
107         inStr = client.getInputStream();
108     }
109     
110 }
相關文章
相關標籤/搜索