Socket网络通信——基础概念

一 基本概念

Socket又称”套接字“,应用程序通常通过”套接字“向网路发出请求或者应答网络请求。

Socket和ServerSocket类位于java.net包中。ServerSocket用于服务器端,Socket是建立网络连接时使用的。在连接成功时,应用程序两端都会产生一个Socket实例,操作这个实例,完成所需的会话,对于一个网络连接来说,套接字是平等的,不因为在服务器端或在客户端而产生不同级别,不管是Socket还是ServerSocket它们的工作都是通过SocketImpl类及其子类完成的。

套接字之间的连接过程可以分为四个步骤:服务器监听、客户端请求服务、服务器确认、客户端确认、进行通信。

  1. 服务器监听: 是服务端套接字并不定位具体的客户端套接字,而是处于等待连接的状态,实时监控网络状态
  2. 客户端请求:是指由客户端的套接字发出连接请求,要连接的目标是服务器的套接字。为此,客户端的套接字必须首先描述它要连接的服务器的套接字,指出服务器端套接字的地址和端口号,然后就向服务器套接字提出连接请求。
  3. 服务器端连接确认:是指当服务器套接字监听或者说接收到客户端套接字的连接请求,它就响应客户端套接字的请求,建立一个新的线程,把服务器套接字的描述发给客户端。
  4. 客户端连接确认:一旦客户确认了此描述,连接就建立好了,双方开始建立通信,有服务器套接字继续处于监听状态,继续接收其他客户端套接字的连接请求。
public class Server {

    final static int PORT = 8763;
    public static void main(String[] args) {
        ServerSocket server = null;
        try {
            server = new ServerSocket(PORT);
            System.out.println("server start...");
            Socket socket = server.accept();
            //新建一个线程执行客户端任务
            new Thread(new ServerHandler(socket)).start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

public class Client {

    final static String ADDRESS = "127.0.0.1";
    final static int PORT = 8763;
    
    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("接收到客户端的请求数据...");
            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) {
                out.flush();
                out.close();
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            socket = null;
        }
    }
}


public class ServerHandler implements Runnable{

    private Socket socket;
    
    public ServerHandler(Socket socket) {
        super();
        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) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (out != null) {
                out.flush();
                out.close();
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

运行结果:
Server端:  
server start...
Server : 接收到客户端的请求数据...
Client端:
Client : 服务器端回送响应的数据...

1.1 同步阻塞式I/O编程

1.2 伪异步IO实现

1.3 基于NIO的同步非阻塞编程

1.4 基于NIO 2.0的异步非阻塞AIO编程

猜你喜欢

转载自www.cnblogs.com/zys-blog/p/9469470.html