Socket--Java

來源:互聯網
上載者:User

標籤:rate   api   tin   closed   .com   equal   tor   soc   except   

Server.java

 1 package socket; 2  3 import java.io.BufferedReader; 4 import java.io.DataInputStream; 5 import java.io.DataOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.io.OutputStream;10 import java.net.ServerSocket;11 import java.net.Socket;12 13 public class Server {14     //監聽的連接埠15     public static final int PORT = 54039;16     17     public static void main(String[] args)18     {19         new Server().start();20     }21     22     //啟動監聽線程的方法23     public void start()24     {25         ServerSocket serverSocket = null;26         try {27             //建立一個ServerSocket對象,監聽PORT連接埠,可能會拋出IOException28             serverSocket  = new ServerSocket(PORT);29             System.out.println("serverScoket:" + serverSocket);30             //迴圈監聽31             while(true)32             {33                 //擷取用戶端串連,如果沒有則阻塞34                 Socket clientSocket = serverSocket.accept();35                 //啟動線程處理監聽到的資料並啟動線程36                 new Thread(new DataRunnable(clientSocket)).start();37                 38             }39         } catch (IOException e) {40             e.printStackTrace();41         } finally{42             if (null != serverSocket)43             {44                 try {45                     serverSocket.close();46                     System.out.println("close serverSocket success!");47                 } catch (IOException e) {48                     System.out.println("close serverSocket failed!");49                 }50             }51         }52         53     }54     55     /**56      * 處理監聽資訊的job57      */58     private class DataRunnable implements Runnable59     {60         Socket client;61         62         public DataRunnable(Socket client) {63             super();64             this.client = client;65         }66 67         public void run() {68             try {69                 //讀取用戶端socket取輸入資料流,封裝成DataInputStream70                 DataInputStream dataIn = new DataInputStream(client.getInputStream());71                 DataOutputStream dataOut = new DataOutputStream(client.getOutputStream());72                 String content = dataIn.readUTF();//有read其他格式的方法,具體見api73                 //列印到控制台74                 System.out.println("content read in from client:" + content);75                 76                 //向用戶端回複資訊77                 System.out.println("請輸入要回複的內容:");78                 String reply = new BufferedReader(new InputStreamReader(System.in)).readLine();79                 dataOut.writeUTF(reply);80                 81                 if("Close".equals(content))82                 {83                     System.out.println("client has been closed ,i want to be closed too");84                 }85                 dataIn.close();86                 dataOut.close();87             } catch (IOException e) {88                 e.printStackTrace();89             }90             91         }92         93     }94 }
View Code

Client.java

 1 package socket; 2  3 import java.io.BufferedReader; 4 import java.io.DataInputStream; 5 import java.io.DataOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.net.Socket;10 import java.net.UnknownHostException;11 12 public class Client {13     public static final String ADDR = "localhost";//監聽本機14     public static final int PORT = 54039;//連接埠15     16     public static void main(String[] args)17     {    18         while(true)19         {20             Socket client = null;21             DataInputStream dataIn = null;22             DataOutputStream dataout = null;23             try {24                 //建立用戶端socket串連到主機伺服器端25                 client = new Socket(ADDR, PORT);26                 27                 dataIn = new DataInputStream(client.getInputStream());28                 dataout = new DataOutputStream(client.getOutputStream());29                 30                 //向伺服器發送資料31                 System.out.println("請輸入資料:");32                 String content = new BufferedReader(new InputStreamReader(System.in)).readLine();33                 dataout.writeUTF(content);34                 35                 //讀取伺服器端返回的資料36                 String reply = dataIn.readUTF();37                 System.out.println("server reply:" + reply);38                 39             } catch (UnknownHostException e) {40                 // TODO Auto-generated catch block41                 e.printStackTrace();42             } catch (IOException e) {43                 // TODO Auto-generated catch block44                 e.printStackTrace();45             }46             finally{47                 try {48                     if(null!=dataIn) dataIn.close();49                     if(null!=dataout) dataout.close();50                     if(null!=client) client.close();51                 } catch (IOException e) {52                     // TODO Auto-generated catch block53                     e.printStackTrace();54                 }55             }56         }57     }58 }
View Code

 

Socket--Java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.