Socket網絡編程基礎

網絡編程中兩個主要的問題:一個是如何準確的定位網絡上一臺或多臺主機,另外一個就是找到主機後如何可靠高效的進行數據傳輸。
在TCP/IP協議中IP層主要負責網絡主機的定位,數據傳輸的路由,由IP地址能夠惟一地肯定Internet上的一臺主機。而TCP層則提供面向應用的可靠(tcp)的或非可靠(UDP)的數據傳輸機制,這是網絡編程的主要對象,通常不須要關心IP層是如何處理數據的。 編程

一個Socket由一個IP地址和一個端口號惟一肯定。0~1023的端口號爲系統所保留,例如http服務的端口號爲80,ftp服務的端口號爲23,telnet服務的端口號爲21,因此咱們在選擇端口號時,最好選擇一個大於1023的數以防止發生衝突。 網絡

public class SocketServer {
 public static void main(String args[]) {
  try {
   ServerSocket server = null;
   try {
    server = new ServerSocket(4700);
    // 建立一個ServerSocket在端口4700監聽客戶請求
   } catch (Exception e) {
    System.out.println("can not listen to:" + e);
    // 出錯,打印出錯信息
   }
   Socket socket = null;
   try {
    socket = server.accept();
    // 使用accept()阻塞等待客戶請求,有客戶請求到來則產生一個Socket對象,並繼續執行
   } catch (Exception e) {
    System.out.println("Error." + e);
    // 出錯,打印出錯信息
   }
   
   String line;
   // 由Socket對象獲得輸入流,並構造相應的BufferedReader對象
   BufferedReader is = new BufferedReader(new InputStreamReader(socket
     .getInputStream()));
   // 由Socket對象獲得輸出流,並構造PrintWriter對象
   PrintWriter os = new PrintWriter(socket.getOutputStream());
   // 由系統標準輸入設備構造BufferedReader對象
   BufferedReader sin = new BufferedReader(new InputStreamReader(
     System.in));
   // 在標準輸出上打印從客戶端讀入的字符串
   System.out.println("Client:" + is.readLine());//只是接收了一次
   
   // 從標準輸入讀入一字符串
   line = sin.readLine();
   // 若是該字符串爲 "bye",則中止循環
   while (!line.equals("bye")) {
    // 向客戶端輸出該字符串
    os.println(line);
    // 刷新輸出流,使Client立刻收到該字符串
    os.flush();
    
    // 在系統標準輸出上打印讀入的字符串
    System.out.println("Server:" + line);
    // 從Client讀入一字符串,並打印到標準輸出上
    System.out.println("Client:" + is.readLine());
    // 從系統標準輸入讀入一字符串
    line = sin.readLine();
   } // 繼續循環
   
   os.close(); // 關閉Socket輸出流
   is.close(); // 關閉Socket輸入流
   socket.close(); // 關閉Socket
   server.close(); // 關閉ServerSocket
  } catch (Exception e) {
   System.out.println("Error:" + e);
   // 出錯,打印出錯信息
  }
 }
}

 

 

//socket一般也稱做"套接字",用於描述IP地址和端口,是一個通訊鏈的句柄。
public class SocketClient {
 public static void main(String args[]) {
  try {
   // 向本機的4700端口發出客戶請求
   Socket socket = new Socket("127.0.0.1", 4700);
   // 由系統標準輸入設備構造BufferedReader對象
   BufferedReader sin = new BufferedReader(new InputStreamReader(
     System.in));
   // 由Socket對象獲得輸出流,並構造PrintWriter對象
   PrintWriter os = new PrintWriter(socket.getOutputStream()); socket

   // 由Socket對象獲得輸入流,並構造相應的BufferedReader對象
   BufferedReader is = new BufferedReader(new InputStreamReader(socket
     .getInputStream()));
   
   String readline;
   // 從系統標準輸入讀入一字符串
   readline = sin.readLine();
   // 若從標準輸入讀入的字符串爲 "bye"則中止循環
   while (!readline.equals("bye")) {
    // 將從系統標準輸入讀入的字符串輸出到Server
    os.println(readline);
    // 刷新輸出流,使Server立刻收到該字符串
    os.flush();
    
    // 在系統標準輸出上打印讀入的字符串
    System.out.println("Client:" + readline);
    // 從Server讀入一字符串,並打印到標準輸出上
    System.out.println("Server:" + is.readLine());
    // 從系統標準輸入讀入一字符串
    readline = sin.readLine();
   } // 繼續循環
   os.close(); // 關閉Socket輸出流
   is.close(); // 關閉Socket輸入流
   socket.close(); // 關閉Socket
  } catch (Exception e) {
   System.out.println("Error" + e); // 出錯,則打印出錯信息
  }
 }
} tcp

相關文章
相關標籤/搜索