Java套接字编程

实现了基于TCP的Java Socket编程实例代码:简单实现了服务器和客户端的套接字编程,并传递简单的字符串。(在服务器声明套接字时需要绑定服务器的端口,端口为临界资源只能一个套接字使用。服务器编程时调用accept()方法,服务器进入等待连接状态。)

Java中TCP/IP服务端连接建立的源码实现(socket->bind->listen->accept):

(1)在进行套接字编程的时候,首先我们要建立一个服务端,并在服务端建立套接字,为套接字绑定接口,然后监听客户端可能发来的连接建立请求。这一套流程均在ServerSocket的构造时完成(这里需要说明的是Java中的Socket和ServerSocket均是实现的java.io.Closeable接口。下面我们来看下构造函数中是如何实现端口的绑定和连接的监听的: 

public ServerSocket(int port) throws IOException {
        this(port, 50, null);
    }

  对于只有端口输入的构造函数来说,我们复用另一个构造函数:ServerSocket(int port, int backlog, InetAddress bindAddr)。其中port是绑定的端口号,backlog是最大连接并发数,默认为50。bindAddr是被绑定的当地IP地址。

public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
        setImpl();
        if (port < 0 || port > 0xFFFF)
            throw new IllegalArgumentException(
                       "Port value out of range: " + port);
        if (backlog < 1)
          backlog = 50;
        try {
            bind(new InetSocketAddress(bindAddr, port), backlog);
        } catch(SecurityException e) {
            close();
            throw e;
        } catch(IOException e) {
            close();
            throw e;
        }
    }
View Code

  我们可以看到在这个构造函数中有bind(new InetSocketAddress(bindAddr, port), backlog)这句代码,这就是用来进行IP地址和端口绑定的,我们在使用套接字时可以只设置端口,则IP地址默认为null。进入到bind方法中:

public void bind(SocketAddress endpoint, int backlog) throws IOException {
       ......
        try {
            SecurityManager security = System.getSecurityManager();
            if (security != null)
                security.checkListen(epoint.getPort());
            getImpl().bind(epoint.getAddress(), epoint.getPort());
            getImpl().listen(backlog);
            bound = true;
        } catch(SecurityException e) {
            bound = false;
            throw e;
        } catch(IOException e) {
            bound = false;
            throw e;
        }
    }

  被我省略的代码主要是进行安全检查的工作,查看端口是否已经关闭或被占用,传进来的地址是否合法等,并提供了相应的处理方法。而try包裹的就是核心的东西了。这里涉及到SecurityManager、SocketImpl类,目前并不了解。不过我们可以看到的bind和listen字段应该就是套接字准备进入阻塞状态的代码了(因为后面还要调用accept,所以我猜测到这一步并没有进入阻塞状态)。

  后面的accept方法又涉及到不知道的类,我等查硕就先不找虐了,先写到这,后面看熟了再总结。

(2)至于服务套接字写好后,客户端是如何进行连接的,其实也是在构造阶段就完成的(socket->connect),下面列出相关源码,以后在做分析。(connect是主动建立连接的方法,这说明每个类型的请求都需要编程一个套接字,客户端与服务端在这时是多对一的关系)

public Socket(String host, int port)
        throws UnknownHostException, IOException
    {
        this(host != null ? new InetSocketAddress(host, port) :
             new InetSocketAddress(InetAddress.getByName(null), port),
             (SocketAddress) null, true);
    }

   其中host是IP地址,port是端口号。

private Socket(SocketAddress address, SocketAddress localAddr,
                   boolean stream) throws IOException {
        setImpl();

        // backward compatibility
        if (address == null)
            throw new NullPointerException();

        try {
            createImpl(stream);
            if (localAddr != null)
                bind(localAddr);
            connect(address);
        } catch (IOException | IllegalArgumentException | SecurityException e) {
            try {
                close();
            } catch (IOException ce) {
                e.addSuppressed(ce);
            }
            throw e;
        }
    }
View Code

  见到connect方法足矣,就不往下跟踪了。

原文地址:https://www.cnblogs.com/liujinyao/p/4746613.html