關於TCP的兩個小練習_第一個部落格~,tcp練習第一個部落格

來源:互聯網
上載者:User

關於TCP的兩個小練習_第一個部落格~,tcp練習第一個部落格

先來一個本地的,用戶端發送請求,服務端接收請求的簡單代碼

 1 package com.TCP.java; 2  3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.InetAddress; 7 import java.net.ServerSocket; 8 import java.net.Socket; 9 import org.junit.Test;10 11 //用戶端給服務端發送資訊。服務端輸出此資訊到控制台上12 public class TestTCP {13     // 用戶端14     @Test15     public void client() {16         Socket socket = null;17         OutputStream os = null;18         try {19             // 1.建立一個Socket的對象,通過構造器指明伺服器端的IP地址,以及其接收程式的連接埠號碼20             socket = new Socket(InetAddress.getByName("192.168.1.101"), 9090);21             // 2.getOutputStream():發送資料,方法返回OutputStream的對象22             os = socket.getOutputStream();23             // 3.具體的輸出過程24             os.write("我是用戶端".getBytes());25         } catch (IOException e) {26             e.printStackTrace();27         } finally {28             // 關閉相應的流和Socket對象29             try {30                 if (os != null) {31                     os.close();32                 }33                 if (socket != null) {34                     socket.close();35                 }36             } catch (IOException e) {37                 e.printStackTrace();38             }39         }40     }41 42     // 服務端43     @Test44     public void server() {45         ServerSocket ss = null;46         Socket s = null;47         InputStream is = null;48         try {49             // 1.建立一個ServerSocket的對象,通過構造器指明自身的連接埠號碼50             ss = new ServerSocket(9090);51             // 2.調用其accept()方法,返回一個Socket對象52             s = ss.accept();53             // 3.調用Socket對象的getInputStream()方法,擷取一個從用戶端發送過來的輸入資料流54             is = s.getInputStream();55             // 4.對擷取的流進行操作56             byte[] b = new byte[20];57             int length;58             while ((length = is.read(b)) != -1) {59                 String str = new String(b, 0, length);60                 System.out.println(str);61             }62             System.out.println("收到來自於" + s.getInetAddress().getHostName() + "的串連");63         } catch (IOException e) {64             e.printStackTrace();65         } finally {66             // 關閉相應的流,Socket,ServerSocket對象67             if (is != null) {68                 try {69                     is.close();70                 } catch (IOException e) {71                     e.printStackTrace();72                 }73             }74             if (s != null) {75                 try {76                     s.close();77                 } catch (IOException e) {78                     e.printStackTrace();79                 }80             }81             if (ss != null) {82                 try {83                     ss.close();84                 } catch (IOException e) {85                     e.printStackTrace();86                 }87             }88         }89     }90 }

 

再來一個稍複雜的,在伺服器收到用戶端發送的請求時,給一個友好的回饋

 

  1 package com.TCP.java;  2   3 import java.io.IOException;  4 import java.io.InputStream;  5 import java.io.OutputStream;  6 import java.net.InetAddress;  7 import java.net.ServerSocket;  8 import java.net.Socket;  9 import java.net.UnknownHostException; 10  11 import org.junit.Test; 12  13 //用戶端給服務端發送資訊,服務端列印資訊到控制台上,同時發送“已收到資訊”給用戶端 14 public class TestTCP2 { 15     //用戶端 16     @Test 17     public void client(){ 18         Socket s = null; 19         OutputStream os = null; 20         InputStream is = null; 21         try { 22             s = new Socket(InetAddress.getByName("192.168.1.101"),9797); 23             os = s.getOutputStream(); 24             os.write("我是用戶端".getBytes()); 25              26             //shutdownOutput():執行此方法,可以顯式的告訴服務端發送完畢 27             s.shutdownOutput(); 28              29             is = s.getInputStream(); 30             byte[] b = new byte[20]; 31             int length; 32             while((length = is.read(b)) != -1){ 33                 String str = new String(b,0,length); 34                 System.out.println(str); 35             } 36         } catch (UnknownHostException e) { 37             // TODO Auto-generated catch block 38             e.printStackTrace(); 39         } catch (IOException e) { 40             // TODO Auto-generated catch block 41             e.printStackTrace(); 42         } 43         if(is != null){ 44             try { 45                 is.close(); 46             } catch (IOException e) { 47                 // TODO Auto-generated catch block 48                 e.printStackTrace(); 49             } 50         } 51         if(os != null){ 52             try { 53                 os.close(); 54             } catch (IOException e) { 55                 // TODO Auto-generated catch block 56                 e.printStackTrace(); 57             } 58         } 59         if(s != null){ 60             try { 61                 s.close(); 62             } catch (IOException e) { 63                 // TODO Auto-generated catch block 64                 e.printStackTrace(); 65             } 66         } 67     } 68     //服務端 69     @Test 70     public void server(){ 71         ServerSocket ss = null; 72         Socket s = null; 73         InputStream is = null; 74         OutputStream os = null; 75         try { 76             ss = new ServerSocket(9797); 77             s = ss.accept(); 78             is = s.getInputStream(); 79              80             byte[] b = new byte[20]; 81             int length; 82             while((length = is.read(b)) != -1){ 83                 String str = new String(b, 0, length); 84                 System.out.println(str); 85             } 86              87             os = s.getOutputStream(); 88             os.write("已收到請求".getBytes()); 89         } catch (IOException e) { 90             // TODO Auto-generated catch block 91             e.printStackTrace(); 92         } 93         finally{ 94             if(os != null){ 95                 try { 96                     os.close(); 97                 } catch (IOException e) { 98                     // TODO Auto-generated catch block 99                     e.printStackTrace();100                 }101             }102             if(is != null){103                 try {104                     is.close();105                 } catch (IOException e) {106                     // TODO Auto-generated catch block107                     e.printStackTrace();108                 }109             }110             if(s != null){111                 try {112                     s.close();113                 } catch (IOException e) {114                     // TODO Auto-generated catch block115                     e.printStackTrace();116                 }117             }118             if(ss != null){119                 try {120                     ss.close();121                 } catch (IOException e) {122                     // TODO Auto-generated catch block123                     e.printStackTrace();124                 }125             }126         }127     }128 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.