JAVA I/O(四)网络Socket和ServerSocket

 《Thinking in Enterprise Java》中第一章描述了用Socket和Channel的网络编程,核心即为Socket和Channel,本文简单讲述Socket的应用。java

Socket能够认为是两个互联机器终端应用软件的抽象,即对于一个网络链接,两端都有一个Socket,应用能够经过套接字进行交互通讯。编程

在Java中,建立Socket链接另外一台机器,能够从Socket中获取InputStream和OutputStream,将其做为输入输出流,使应用程序与操做本地文件IO相似。存在2个基于流的Socket类:ServerSocket和Socket。缓存

  • ServerSocket用于服务器端,监听客户端链接
  • Socket用于客户端与服务端交互
  • 服务段accept()方法处于阻塞状态,直到有客户端链接,建立一个服务端Socket,与客户端交互

另外,当建立ServerSocket时,只须要提供一个端口号,IP信息为本机默认信息;建立Socket时,必须提供IP和端口号;由ServerSocket.accept( )建立的不须要,其已包含全部信息。服务器

1. 简单客户端和服务端

服务器端:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class JabberServer {

    public static final int PORT = 8080;
    public static void main(String[] args) throws IOException{

        ServerSocket server = new ServerSocket(PORT);
        System.out.println("开始: " + server);
        try {
            Socket socket = server.accept();
            System.out.println("Connection socket: " + socket);
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                //Output is automatically flushed by PrintWrite
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
                while(true) {
                    String str = in.readLine();
                    if("END".equals(str))
                        break;
                    System.out.println("Echoing: " + str);
                    out.println(str);
                }
            } finally {
                System.out.println("CLosing....");
                socket.close();
            }
        } finally {
            server.close();
        }
    }
}

输出:网络

开始: ServerSocket[addr=0.0.0.0/0.0.0.0,localport=8080]

大体步骤:app

  • 建立ServerSocket,绑定端口8080
  • 调accept()方法监听链接,并返回套接字Socket
  • 获取输入流,并经过InputStreamReader转为字符,缓存在BufferdReader中
  • 获取输出流,经过OutputStreamWriter将BufferedWriter中的字符转换为字节,并经过PrintWriter格式化输出,同时自动flush
  • 根据输入流读取的字符,若是是END则结束会话
  • 关闭套接字和ServerSocket

客户端:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 根据服务器ip和端口/服务器地址等,建立Socket
 * Socket能够获取输入和输出流,默认是使用AbstractPlainSocketImpl类中的SocketInputStream和SocketOutputStream
 *
 */
public class JabberClient {

    public static void main(String[] args) throws Exception{

        //服务器端信息,address和8080;后台链接服务器,还会绑定客户端
        InetAddress address = InetAddress.getByName(null);
        System.out.println("address = " + address);
        Socket socket = new Socket(address, 8080);
        
        try {
            System.out.println("Socket = " +socket);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            
            for(int i = 0; i < 10; i++) {
                out.println("hello " + i);
                String str = in.readLine();
                System.out.println(str);
            }
            out.println("END");
        } finally {
            System.out.println("Closing socket...");
            socket.close();
        }
    }
}

 大体步骤与服务端类似,不一样之处在于建立客户端套接字,须要指定服务端地址和端口号。less

服务器端输出:socket

Connection socket: Socket[addr=/127.0.0.1,port=35702,localport=8080]
Echoing: hello 0
Echoing: hello 1
Echoing: hello 2
Echoing: hello 3
Echoing: hello 4
Echoing: hello 5
Echoing: hello 6
Echoing: hello 7
Echoing: hello 8
Echoing: hello 9
CLosing....

客户端输出:ide

address = localhost/127.0.0.1
Socket = Socket[addr=localhost/127.0.0.1,port=8080,localport=35702]
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
Closing socket...

 2. 源码分析

不管是建立ServerSocket仍是Socket,都须要与底层实现SocketImpl关联,以实现具体网络交互的逻辑。源码分析

(1)服务端建立ServerSocket

ServerSocket server = new ServerSocket(PORT);

 具体构造过程以下,构造参数为服务端监听端口:

 /**
     * Creates a server socket, bound to the specified port. A port number
     * of {@code 0} means that the port number is automatically
     * allocated, typically from an ephemeral port range. This port
     * number can then be retrieved by calling {@link #getLocalPort getLocalPort}.
     * <p>
     * The maximum queue length for incoming connection indications (a
     * request to connect) is set to {@code 50}. If a connection
     * indication arrives when the queue is full, the connection is refused.
     * <p>
     * If the application has specified a server socket factory, that
     * factory's {@code createSocketImpl} method is called to create
     * the actual socket implementation. Otherwise a "plain" socket is created.
     * <p>
     * If there is a security manager,
     * its {@code checkListen} method is called
     * with the {@code port} argument
     * as its argument to ensure the operation is allowed.
     * This could result in a SecurityException.
     *
     *
     * @param      port  the port number, or {@code 0} to use a port
     *                   number that is automatically allocated.
     *
     * @exception  IOException  if an I/O error occurs when opening the socket.
     * @exception  SecurityException
     * if a security manager exists and its {@code checkListen}
     * method doesn't allow the operation.
     * @exception  IllegalArgumentException if the port parameter is outside
     *             the specified range of valid port values, which is between
     *             0 and 65535, inclusive.
     *
     * @see        java.net.SocketImpl
     * @see        java.net.SocketImplFactory#createSocketImpl()
     * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
     * @see        SecurityManager#checkListen
     */
    public ServerSocket(int port) throws IOException {
        this(port, 50, null);
    }

/**
     * Create a server with the specified port, listen backlog, and
     * local IP address to bind to.  The <i>bindAddr</i> argument
     * can be used on a multi-homed host for a ServerSocket that
     * will only accept connect requests to one of its addresses.
     * If <i>bindAddr</i> is null, it will default accepting
     * connections on any/all local addresses.
     * The port must be between 0 and 65535, inclusive.
     * A port number of {@code 0} means that the port number is
     * automatically allocated, typically from an ephemeral port range.
     * This port number can then be retrieved by calling
     * {@link #getLocalPort getLocalPort}.
     *
     * <P>If there is a security manager, this method
     * calls its {@code checkListen} method
     * with the {@code port} argument
     * as its argument to ensure the operation is allowed.
     * This could result in a SecurityException.
     *
     * The {@code backlog} argument is the requested maximum number of
     * pending connections on the socket. Its exact semantics are implementation
     * specific. In particular, an implementation may impose a maximum length
     * or may choose to ignore the parameter altogther. The value provided
     * should be greater than {@code 0}. If it is less than or equal to
     * {@code 0}, then an implementation specific default will be used.
     * <P>
     * @param port  the port number, or {@code 0} to use a port
     *              number that is automatically allocated.
     * @param backlog requested maximum length of the queue of incoming
     *                connections.
     * @param bindAddr the local InetAddress the server will bind to
     *
     * @throws  SecurityException if a security manager exists and
     * its {@code checkListen} method doesn't allow the operation.
     *
     * @throws  IOException if an I/O error occurs when opening the socket.
     * @exception  IllegalArgumentException if the port parameter is outside
     *             the specified range of valid port values, which is between
     *             0 and 65535, inclusive.
     *
     * @see SocketOptions
     * @see SocketImpl
     * @see SecurityManager#checkListen
     * @since   JDK1.1
     */
    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;
        }
    }

实际调用的构造方法为ServerSocket(int port, int backlog, InetAddress bindAddr),port表示端口号,backlog表示服务端请求链接队列最大数(默认为50),bindAddr表示服务器要绑定的本地地址(默认为null)。

  • setImpl(),设置系统默认类型SocketImpl,其是服务端和客户端套接字建立、链接、交互等操做的核心
  • bind(new InetSocketAddress(bindAddr, port), backlog),绑定对应的地址和端口,并设置最大链接数(超过链接数,服务器拒绝链接)

(2)服务端accept

Socket socket = server.accept();

 服务端会阻塞等待客户端链接,直到有客户端链接,并建立一个服务端Socket,与客户端交互。

/**
     * Listens for a connection to be made to this socket and accepts
     * it. The method blocks until a connection is made.
     *
     * <p>A new Socket {@code s} is created and, if there
     * is a security manager,
     * the security manager's {@code checkAccept} method is called
     * with {@code s.getInetAddress().getHostAddress()} and
     * {@code s.getPort()}
     * as its arguments to ensure the operation is allowed.
     * This could result in a SecurityException.
     *
     * @exception  IOException  if an I/O error occurs when waiting for a
     *               connection.
     * @exception  SecurityException  if a security manager exists and its
     *             {@code checkAccept} method doesn't allow the operation.
     * @exception  SocketTimeoutException if a timeout was previously set with setSoTimeout and
     *             the timeout has been reached.
     * @exception  java.nio.channels.IllegalBlockingModeException
     *             if this socket has an associated channel, the channel is in
     *             non-blocking mode, and there is no connection ready to be
     *             accepted
     *
     * @return the new Socket
     * @see SecurityManager#checkAccept
     * @revised 1.4
     * @spec JSR-51
     */
    public Socket accept() throws IOException {
        if (isClosed())
            throw new SocketException("Socket is closed");
        if (!isBound())
            throw new SocketException("Socket is not bound yet");
        Socket s = new Socket((SocketImpl) null);
        implAccept(s);
        return s;
    }

① 指定SocketImpl,建立一个非链接的Socket构造方法,以下:

 /**
     * Creates an unconnected Socket with a user-specified
     * SocketImpl.
     * <P>
     * @param impl an instance of a <B>SocketImpl</B>
     * the subclass wishes to use on the Socket.
     *
     * @exception SocketException if there is an error in the underlying protocol,
     * such as a TCP error.
     * @since   JDK1.1
     */
    protected Socket(SocketImpl impl) throws SocketException {
        this.impl = impl;
        if (impl != null) {
            checkOldImpl();
            this.impl.setSocket(this);
        }
    }

 ② implAccept(s)方法

上一步建立Socket的参数SocketImpl为null,该方法为Socket建立具体的SocketImpl,绑定地址和文件描述符,具体可见源码。

(3)客户端建立套接字

Socket socket = new Socket(address, 8080);

具体建立过程以下,构造参数为InetAddress和port:

/**
     * Creates a stream socket and connects it to the specified port
     * number at the specified IP address.
     * <p>
     * If the application has specified a socket factory, that factory's
     * {@code createSocketImpl} method is called to create the
     * actual socket implementation. Otherwise a "plain" socket is created.
     * <p>
     * If there is a security manager, its
     * {@code checkConnect} method is called
     * with the host address and {@code port}
     * as its arguments. This could result in a SecurityException.
     *
     * @param      address   the IP address.
     * @param      port      the port number.
     * @exception  IOException  if an I/O error occurs when creating the socket.
     * @exception  SecurityException  if a security manager exists and its
     *             {@code checkConnect} method doesn't allow the operation.
     * @exception  IllegalArgumentException if the port parameter is outside
     *             the specified range of valid port values, which is between
     *             0 and 65535, inclusive.
     * @exception  NullPointerException if {@code address} is null.
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
     * @see        java.net.SocketImpl
     * @see        java.net.SocketImplFactory#createSocketImpl()
     * @see        SecurityManager#checkConnect
     */
    public Socket(InetAddress address, int port) throws IOException {
        this(address != null ? new InetSocketAddress(address, port) : null,
             (SocketAddress) null, true);
    }

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;
        }
    }
  • 首先,setImpl(),与服务端类似,设置系统默认类型SocketImpl,其是服务端和客户端套接字建立、链接、交互等操做的核心
  • 其次,createImpl(stream),根据stream布尔值建立socket实现,true时建立基于流的socket(或者面向链接),false时建立无链接UDP套接字
  • 最后,connect(address),链接服务器,链接一直处于阻塞状态,直到链接成功,或者超时或报错等

(4)SocketImpl类

SocketImpl类是服务器和客户端链接的核心,源码以下,包含Socket、ServerSocket、文件描述符、IP地址、端口号和套接字链接的本地端口号:

/**
 * The abstract class {@code SocketImpl} is a common superclass
 * of all classes that actually implement sockets. It is used to
 * create both client and server sockets.
 * <p>
 * A "plain" socket implements these methods exactly as
 * described, without attempting to go through a firewall or proxy.
 *
 * @author  unascribed
 * @since   JDK1.0
 */
public abstract class SocketImpl implements SocketOptions {
    /**
     * The actual Socket object.
     */
    Socket socket = null;
    ServerSocket serverSocket = null;

    /**
     * The file descriptor object for this socket.
     */
    protected FileDescriptor fd;

    /**
     * The IP address of the remote end of this socket.
     */
    protected InetAddress address;

    /**
     * The port number on the remote host to which this socket is connected.
     */
    protected int port;

    /**
     * The local port number to which this socket is connected.
     */
    protected int localport;

 类结构以下:

(5)套接字输入输出流

SocketImpl默认子类是AbstractPlainSocketImpl,大部分套接字的建立、链接等操做都经过该类进行。套接字经过获取输入输出流,使应用能够像操做本地I/O流同样,操做网络数据。

Socket获取输入输出流的方法是getInputStream()和getOutputStream(),底层调AbstractPlainSocketImpl的方法获取,实际流对象为SocketInputStream和SocketOutputStream,具体细节此处不阐述。

socket.getInputStream()//获取输入流
socket.getOutputStream()//获取输出流

AbstractPlainSocketImpl类中包含2个套接字输入输出流属性,以下:

 private SocketInputStream socketInputStream = null;
 private SocketOutputStream socketOutputStream = null;

 他们分别继承自FileInputStream和FileOutputStream,以表示输入输出源。而且他们都不是public类型的,通常不会直接使用。

class SocketInputStream extends FileInputStream
class SocketOutputStream extends FileOutputStream 

3. 总结

1. 网络链接的核心是套接字Socket,服务端ServerSocket监听链接,建立套接字;客户端建立套接字,绑定服务端ip和端口

2. SocketImpl类和子类AbstractPlainSocketImpl是服务端和客户端套接字建立、链接、交互的核心

3. 经过套接字获取输入输出流,应用程序能够与本地I/O流操做同样

相关文章
相关标签/搜索