android–>Socket簡單用法

Socket通常也稱做”套接字“,用於描述IP地址和端口,廢話不多說,它就是網絡通信過程中端點的抽象表示。值得一提的是,Java在包java.net中提供瞭兩個類Socket和ServerSocket,分別用來表示雙向連接的客戶端和服務端。這是兩個封裝得非常好的類,使用起來很方便!
    下面將首先創建一個SocketServer的類作為服務端如下,該服務端實現瞭多線程機制,可以在特定端口處監聽多個客戶請求,一旦有客戶請求,Server總是會創建一個服務純種來服務新來的客戶,而自己繼續監聽。程序中accept()是一個阻塞函數,所謂阻塞性方法就是說該方法被調用後將等待客戶的請求,直到有一個客戶啟動並請求連接到相同的端口,然後accept()返回一個對應於客戶的Socket。這時,客戶方和服務方都建立瞭用於通信的Socket,接下來就是由各個Socket分別打開各自的輸入、輸出流。
SocketServer類,服務器實現:
[java] 
package HA.Socket; 
  
 import java.io.*; 
 import java.net.*; 
  
  public class SocketServer { 
      
     ServerSocket sever; 
      
     public SocketServer(int port){ 
         try{ 
             sever = new ServerSocket(port); 
         }catch(IOException e){ 
             e.printStackTrace(); 
         } 
     } 
      
     public void beginListen(){ 
         while(true){ 
             try{ 
                 final Socket socket = sever.accept(); 
                  
                 new Thread(new Runnable(){ 
                     public void run(){ 
                         BufferedReader in; 
                         try{ 
                             in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8")); 
                             PrintWriter out = new PrintWriter(socket.getOutputStream()); 
                             while (!socket.isClosed()){ 
                                 String str; 
                                 str = in.readLine(); 
                                 out.println("Hello!world!! " + str); 
                                 out.flush(); 
                                 if (str == null || str.equals("end")) 
                                     break; 
                                 System.out.println(str); 
                             } 
                             socket.close(); 
                         }catch(IOException e){ 
                             e.printStackTrace(); 
                         } 
                     } 
                 }).start(); 
             }catch(IOException e){ 
                 e.printStackTrace(); 
             } 
         } 
     } 
 } 
 
SocketClient類,客戶端實現:
[java] 
package HA.Socket; 
  
 import java.io.*; 
 import java.net.*; 
  
  public class SocketClient { 
     static Socket client; 
      
     public SocketClient(String site, int port){ 
         try{ 
             client = new Socket(site,port); 
             System.out.println("Client is created! site:"+site+" port:"+port); 
         }catch (UnknownHostException e){ 
             e.printStackTrace(); 
         }catch (IOException e){ 
             e.printStackTrace(); 
         } 
     } 
      
     public String sendMsg(String msg){ 
         try{ 
             BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
             PrintWriter out = new PrintWriter(client.getOutputStream()); 
             out.println(msg); 
             out.flush(); 
             return in.readLine(); 
         }catch(IOException e){ 
             e.printStackTrace(); 
         } 
         return ""; 
     } 
     public void closeSocket(){ 
         try{ 
             client.close(); 
         }catch(IOException e){ 
             e.printStackTrace(); 
         } 
     } 
     public static void main(String[] args) throws Exception{ 
          
     } 
  
 } 

接下來就是來測試Socket通信瞭!
[java] 
package HA.Socket; 
  
  public class TestSocketServer { 
      
     public static void main(String[] argvs){ 
         SocketServer server = new SocketServer(12345); 
         server.beginListen(); 
     } 
 } 

 
再運行TestSocketClient類:
[java] 
package HA.Socket; 
  
  public class TestSocketClient { 
  
     public static void main(String[] args){ 
          
         SocketClient client = new SocketClient("127.0.0.1",12345); 
         System.out.println(client.sendMsg("nimei1")); 
         client.closeSocket(); 
          
         SocketClient client1 = new SocketClient("127.0.0.1",12345); 
         System.out.println(client1.sendMsg("nimei1111")); 
         client1.closeSocket(); 
          
         SocketClient client11 = new SocketClient("127.0.0.1",12345); 
         System.out.println(client11.sendMsg("nimei11111111")); 
         client11.closeSocket(); 
          
         SocketClient client111 = new SocketClient("127.0.0.1",12345); 
         System.out.println(client111.sendMsg("nimei11111111111111111")); 
         client111.closeSocket(); 
          
     } 
 } 

輸出如下
服務端
Client is created! site:127.0.0.1 port:12345
Hello!world!! nimei1
Client is created! site:127.0.0.1 port:12345
Hello!world!! nimei1111
Client is created! site:127.0.0.1 port:12345
Hello!world!! nimei11111111
Client is created! site:127.0.0.1 port:12345
Hello!world!! nimei11111111111111111
客戶端:
nimei1
nimei1111
nimei11111111
nimei11111111111111

發佈留言