Java network programming related knowledge

Some basic knowledge of the network

IP address classification

IP addresses are divided into 5 types according to the network ID, type A address, type B address, type C address, type D address and type E address. Class A is reserved for government agencies, Class B is allocated to medium-sized companies, Class C is allocated to anyone in need, Class D is used for multicast, and Class E is used for experiments. The number of addresses that can be accommodated in each type is different. (The IP address consists of the network number and the host number).

  • Class A address

    A Class A IP address consists of a 1-byte network address and a 3-byte host address. The highest bit of the network address must be "0", and the address range is from 1.0.0.0 to 126.0.0.0. There are 126 available Class A networks, and each network can accommodate more than 100 million hosts. Class A addresses are generally allocated to large networks.

  1. The first byte of the class A address is the network address, the other 3 bytes are the host address, and the default subnet mask is 255.0.0.0.
  2. Class A address range: 0.0.0.0—126.255.255.255 (0.0.0.0 and 126.255.255.255 are generally not used).
  3. Private addresses and reserved addresses in class A addresses.
  4. 10.XXX is a private address (a so-called private address is an address that is not used on the Internet but used in a local area network).
  5. 127.XXX is reserved address, used for loop test.
  • Class B address

    A Class B IP address consists of a 2-byte network address and a 2-byte host address. The highest bit of the network address must be "10", and the address range is from 128.0.0.0 to 191.255.255.255. There are 16,382 available Class B networks, and each network can accommodate more than 60,000 hosts, generally allocated to medium-sized networks.

  1. The first byte and second byte of the class B address are network addresses, the other two bytes are host addresses, and the default subnet mask is 255.255.0.0.
  2. Class B address range: 128.0.0.0—191.255.255.255 (128.0.0.0 and 191.255.255.255 are generally not used).
  3. Private addresses and reserved addresses of class B addresses.
  4. 172.16.0.0—172.31.255.255 is a private address.
  5. 169.254.XX is the reserved address. If your IP address is obtained automatically, and you do not find an available DHCP server on the network. You will get one of the IP.
  • Class C address

    A type C IP address consists of a 3-byte network address and a 1-byte host address. The highest bit of the network address must be "110". The range is from 192.0.0.0 to 223.255.255.255. Class C networks can reach more than 2.09 million, and each network can accommodate 254 hosts.

  1. The first 3 bytes of the class C address are the network address, and the fourth byte is the host address. In addition, the first three bits of the first byte are fixed at 110, and the default subnet mask is 255.255.255.0.
  2. Class C address range: 192.0.0.0-223.255.255.255 (192.0.0.0 and 223.255.255.255 are generally not used).
  3. Private address in class C addresses.
  4. 192.168.XX is a private address.
  • Class D address

    The first byte of the class D IP address starts with "1110", which is a specially reserved address. It does not point to a specific network, and this type of address is currently used in Multicast. The multicast address is used to address a group of computers at a time, and it identifies a group of computers sharing the same protocol.

  1. Class D addresses do not distinguish between network addresses and host addresses, and the first four bits of its first byte are fixed at 1110.
  2. Class D address range: 224.0.0.1-239.255.255.254.
  • Class E address

    Start with "11110" and reserved for future use. All zero ("0.0.0.0") addresses correspond to the current host. The IP address of all "1" s ("255.255.255.255") is the broadcast address of the current subnet.

  1. Class E addresses also do not distinguish between network addresses and host addresses. The first five bits of its first byte are fixed at 11110.
  2. Class E address range: 240.0.0.1-255.255.255.254

Port classification

  • Well-known port (well know port): from 0 to 1023, they are tightly bound to some specific services. For example, port 80 is bound to http service, port 443 is bound to https service, port 22 is bound to ssh service, etc .;
  • Main coarse port (Registed port): 1024 to 49151, they are loosely bound to some services, and applications we write should use ports in this range;
  • Dynamic or private ports: 49152 to 65535, these ports will be used dynamically by the application.

Java basic network support

Most of the types of network support in Java are under the java.net package. Commonly used classes are URL, URLConnection, URLDecoder, URLENcoder, and InetAddress.

Use of InetAddress

InetAddress stands for IP address, it has two subclasses Inet4Address and Inet6Address.

public class InetAddressDemo {

    public static void main(String[] args) throws Exception {
        //获取本机的IP地址
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println("host address:"+localHost.getHostAddress());
        System.out.println("host name:"+localHost.getHostName());
        //根据百度的域名,随机获取百度的一个IP地址
        InetAddress baidu = InetAddress.getByName("www.baidu.com");
        System.out.println("host address:"+baidu.getHostAddress());
        System.out.println("host name:"+baidu.getHostName());
        //根据IP地址,货期InetAddress
        InetAddress loopAddress = InetAddress.getByAddress(new byte[]{127,0,0,1});
        System.out.println("host address:"+loopAddress.getHostAddress());
        System.out.println("host name:"+loopAddress.getHostName());
        //根据域名,获取域名对应的所有IP地址
        InetAddress[] baidus = InetAddress.getAllByName("www.baidu.com");
        for (InetAddress inetAddress : baidus) {
            System.out.println("host address:"+inetAddress.getHostAddress());
            System.out.println("host name:"+inetAddress.getHostName());
        }
    }
}

Use of URLEncoder and URLDecoder

When the URL address contains a string of non-Western European characters, the system will automatically encode these non-Western European characters. In our programming process, it will involve the conversion between these ordinary strings and special strings, then you need to use URLEncoder and URLDecoder.

 String encodedUrl = "https://www.baidu.com/s?wd=ip%E5%9C%B0%E5%9D%80%E5%88%86%E7%B1%BB&rsv_spt=1&rsv_iqid=0xe6273c5300071242&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=1&oq=IP%25E5%259C%25B0%25E5%259D%2580%25E5%2588%2586%25E7%25B1%25BB&rsv_t=8798Pr4JiDoBuHX8kSW6i384TlOk5p8vEQ4c4tWrc0suF31CjvBh6stq0gyq0PtETa9x&inputT=9569&rsv_sug3=24&rsv_sug1=14&rsv_sug7=100&rsv_pq=b3c08c4200035639&bs=IP%E5%9C%B0%E5%9D%80%E5%88%86%E7%B1%BB";
String decodeURL = URLDecoder.decode(encodedUrl, Charset.forName("UTF8").name());
System.out.println("decodeURL:"+decodeURL);

The two classes URLEncoder and URLDecoder only provide encode and decode methods for our use.

Use of URL, URLConnection and URLPermission

Here is the difference between URL and URI:

URI: Uniform resource identifier, uniform resource identifier, used to uniquely identify a resource. Every resource available on the Web such as HTML documents, images, video clips, programs, etc. is a URI to locate. A URI is generally composed of three parts: ①name mechanism for accessing resources ②host name for storing resources ③name of resources , Represented by the path, with an emphasis on resources.

The URL is a uniform resource locator, a uniform resource locator. It is a specific URI, that is, the URL can be used to identify a resource, and also indicates how to locate the resource. The URL generally consists of three parts: ① protocol (or called service mode) ② the IP address of the host (sometimes including the port number) where the resource is stored ③ the specific address of the host resource .

Simply put, URI is a unique identifier of a resource on the Internet (we can think of it as the id of a resource), URL is a special URI, URL can not only identify an Internet resource, but also can be obtained through the URL This resource. Two types of URI and URL in Java correspond to these two concepts respectively. The URL class can open a stream to obtain specific resources.

String[] urls = {"https://img0.pconline.com.cn/pconline/1707/21/9625301_20150814_6d20f056ee9803d9419buyemaASeB0KJ_thumb.jpg"};

        for (String url : urls) {
            URL url1 = new URL(url);
            int port = url1.getPort();
            System.out.println("port:"+port);
            String host = url1.getHost();
            System.out.println("host:"+host);
            String protocol = url1.getProtocol();
            System.out.println("protocol:"+protocol);
            String file = url1.getFile();
            System.out.println("fileName:"+file);
            //这段是否要设置权限,为什么open总是失败?
            InputStream inputStream = url1.openStream();
            FileOutputStream fos = new FileOutputStream("D:\\"+new Date()+".jpeg");
            FileCopyUtils.copy(inputStream,fos);
            inputStream.close();
            fos.close();
        }

Network programming based on TCP protocol

Use ServerSocket to establish TCP server

The class that can receive requests from other communication entities in Java is ServerSocket. This object can listen to Socket connections from the client (two sockets for each TCP connection, an IP plus a port to form a Socket). If there is no connection, it will always be in a waiting state.

  • Socket accept (): This method returns the client Socket, no connection will always be in a waiting state (synchronization), and the thread is blocked (blocking);

ServerSocket has the following constructors:

  • public ServerSocket (int port): designated port, backlog default 50;
  • public ServerSocket (int port, int backlog): backlog is used to specify the length of the connection queue, this value is also related to the operating system, tried to set up to 200 under Windows, if we set the value exceeds 200, we take 200.
  • public ServerSocket (int port, int backlog, InetAddress bindAddr): If the machine has multiple network cards, you can also specify which network card to monitor.

(Detailed explanation of netstat command)

Use Socket for communication

The following is a list of clientSocket and serverSocket that are very simple: the client sends a message to the server every second, and the server gives a response:

Socket clientSocket = new Socket("127.0.0.1",30000);
//inputStream用来接受服务端返回的消息
InputStream inputStream = clientSocket.getInputStream();
//outputStream用来给服务端发消息
OutputStream outputStream = clientSocket.getOutputStream();
while (true){
    outputStream.write(("hi, l am clinetSocket").getBytes());
    byte[] bytes = new byte[1024];
    inputStream.read(bytes);
    System.out.println("get message from server:"+new String(bytes));
    Thread.sleep(1000);
}

Server program

public class ServerSocketDemo {

    private static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(30000, 5);
        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("get socket:" + socket);
            executorService.execute(new Printer(socket));
        }
    }

    private static class Printer implements Runnable {

        private Socket socket;

        public Printer(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            OutputStream outputStream;
            InputStream inputStream;
            try {
                //对于服务端来说,inputStream用来接收客户端的报文
                inputStream = socket.getInputStream();
                //对于服务端来说,outputStream用来给客户端响应报文
                outputStream = socket.getOutputStream();
                while (true) {
                    byte[] bytes = new byte[1024];
                    inputStream.read(bytes);
                    System.out.println("wa.. l got you " + new String(bytes));
                    outputStream.write("l am serverSocket".getBytes());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Socket half closed

A semi-closed socket refers to only closing the input and output streams of the socket, but not closing the entire socket connection.

Using NIO to realize non-blocking Socket communication

A brief description of several classes used for socket communication under Java NIO:

  • Selector: It is a multiplexer of the SelectableChannel object. All Channels that wish to communicate in a non-blocking manner should be registered on the Selector object. You can call selector = Selector.open () to construct the Selector object.
  • SelectionKey: A Selector instance has three SelectionKey sets. The first type is all the SelectionKey collections returned by the selector's keys () method, representing all the Channels registered on this Selector instance; the second type is the SelectionKey collections returned by the selectedKeys () method, representing the channels that require IO processing; The third type is the Channel that has been unregistered and is generally not used.
public class NioServerSocketDemo {

    private Selector selector;
    public static final int port = 30000;
    private Charset charset = Charset.forName("UTF-8");

    public void init() throws Exception{
        selector = Selector.open();
        ServerSocketChannel server = ServerSocketChannel.open();
        InetSocketAddress address = new InetSocketAddress("127.0.0.1",port);
        server.bind(address,5);
        server.configureBlocking(false);
        //serverSocketChannel也要注册到selector上面
        server.register(selector, SelectionKey.OP_ACCEPT);
        //selector.select()会阻塞当前线程
        //selector.select(long timeout),设置超时时间
        //selector.selectNow()不会阻塞线程
        while (selector.select()>0){
            for(SelectionKey key : selector.selectedKeys()){
                //已经处理过了,将其删除
                selector.selectedKeys().remove(key);
                if(key.isConnectable()){
                    SocketChannel channel = (SocketChannel)key.channel();
                    System.out.println(channel+" has connected...");
                }
                if(key.isAcceptable()){
                    SocketChannel acceptChannel = server.accept();
                    acceptChannel.configureBlocking(false);
                    acceptChannel.register(selector,SelectionKey.OP_READ);
                    key.interestOps(SelectionKey.OP_ACCEPT);
                }
                if(key.isReadable()){
                    SocketChannel channel = (SocketChannel)key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    String content = "";
                    try{
                        while (channel.read(buffer)>0){
                            buffer.flip();
                            content+=charset.decode(buffer);
                        }
                        System.out.println("get content:"+content);
                        channel.write(buffer);
                        key.interestOps(SelectionKey.OP_READ);

                    }catch (IOException ex){
                        key.cancel();
                        if(key.channel()!=null){
                            key.channel().close();
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        new NioServerSocketDemo().init();
    }

}

Using AIO to realize non-blocking Socket communication

public class AIOServerSocket {

    private static Charset charset = Charset.forName("UTF-8");
    public static void main(String[] args) throws Exception {
        AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(30000),1000);
        while (true){
            Future<AsynchronousSocketChannel> future = serverSocketChannel.accept();
            AsynchronousSocketChannel socketChannel = future.get();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            socketChannel.read(buffer);
            System.out.println("get from client:"+charset.decode(buffer));
        }
    }
}

Guess you like

Origin www.cnblogs.com/54chensongxia/p/12678143.html