Socket編程基礎篇

Socket又稱"套接字",應用程序一般經過「套接字」向網絡發生請求或者應答網絡請求。java

  Socket和ServerSocket類庫位於java.net包中,ServerSocket用於服務端,Socket是創建網絡鏈接時使用的。在鏈接成功時,應用程序兩端都會產生一個Socket實例。操做這個實例,完成所需的會話。對於一個網絡鏈接來講,套接字是平等的,不由於在服務器端或在客戶端而產生級別。無論Socket仍是ServerSocket它們的工做都是經過SocketImpl類及其子類完成的。編程

首先咱們先看一段代碼,瞭解Socket編程鏈接,而後再作詳細的介紹:服務器

服務端:網絡

package bhz.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {

    final static int PROT = 8765;
    
    public static void main(String[] args) {
        
        ServerSocket server = null;
        try {
            server = new ServerSocket(PROT);
            System.out.println(" server start .. ");
            //進行阻塞
            Socket socket = server.accept();
            //新建一個線程執行客戶端的任務
            new Thread(new ServerHandler(socket)).start();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(server != null){
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            server = null;
        }
        
        
        
    }
}
package bhz.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerHandler implements Runnable{

    private Socket socket ;
    
    public ServerHandler(Socket socket){
        this.socket = socket;
    }
    
    @Override
    public void run() {
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            out = new PrintWriter(this.socket.getOutputStream(), true);
            String body = null;
            while(true){
                body = in.readLine();
                if(body == null) break;
                System.out.println("Server :" + body);
                out.println("服務器端回送響的應數據.");
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket = null;
        }
        
        
    }

}

客戶端:socket

package bhz.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

    final static String ADDRESS = "127.0.0.1";
    final static int PORT = 8765;
    
    public static void main(String[] args) {
        
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        
        try {
            socket = new Socket(ADDRESS, PORT);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            
            //向服務器端發送數據
            out.println("接收到客戶端的請求數據...");
            out.println("接收到客戶端的請求數據1111...");
            String response = in.readLine();
            System.out.println("Client: " + response);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket = null;
        }
    }
}

套接字鏈接的過程能夠分爲四個步驟,服務器監聽,客戶端請求服務器,服務器確認,客戶端確認,進行通訊。ide

  (1)服務器監聽:是服務端套接字並不定位具體的客戶端套接字,而是出於等待鏈接的狀態,實時監控網絡狀態。this

  (2)客戶端請求:是指由客戶端的套接字提出鏈接請求,要鏈接的目標是服務器端的套接字。爲此,客戶端的套接字必須首先描述他要鏈接的服務器的套接字,指出服務器端套接字的地址和端口號,而後就向服務器端套接字提出鏈接請求。spa

  (3)服務器端鏈接確認:是指當服務器端套接字監聽到或者接收到客戶端套接字的鏈接請求,他就響應客戶端套接字的請求,創建一個新的線程,把服務器端套接字的描述發送給客戶端。.net

  (4)客戶端鏈接確認:一旦客戶確認了此描述,鏈接就創建好了。雙方開始通訊,二服務器端套接字繼續處於監聽狀態,繼續接收其餘客戶端套接字的鏈接請求。線程

相關文章
相關標籤/搜索