Java network programming

Network programming

computer network

It refers to a computer system that connects multiple computers with independent functions in different geographical locations through communication lines (wired or wireless), and realizes resource sharing and information transmission under the management and coordination of network operating system, network management software and network communication protocol

Completion requirements:

  • How to accurately locate a host on the network: port, a certain resource on the computer
  • How to transfer data

javaweb: Web page programming B/S

Network programming: TCP/IP C/S

##Elements of network communication

How to realize network communication:

  • Address of communication parties: IP, port number

  • Rule: network communication protocol: http, ftp, smtp, tcp, udp

  • OSI seven layer network model, TCP/IP four layer conceptual model

OSI seven layer network modelTCP/IP four layer conceptual modelCorresponding network protocol
Application layerapplication layerHTTP,TFTP,FTP,NFS,WAIS,SMTP
Presentation layerTelnet,Rlogin,SNMP,Gopher
Session layerSMTP,DNS
Transport layerTransport layerTCP,UDP
Network layer (Network)network layerIP,ICMP,ARP,RARP,AKP,UUCP
Data Link layer (Data Link)data link layerFDDI,Ethernet,Arpanet,PDN,SLIP,PPP
Physical layerIEEE 802,1A,IEEE 802.2-IEEE 802.11

OSI is ideal, and TCP/IP is actually used

Network programming focuses on the transport layer

IP

ip address: InetAdress

  • Uniquely locate a computer on the network

  • 127.0.0.1: local localhost

  • Classification of ip addresses:

    • IP address classification:

      • IPV4:127.0.0.1: composed of four bytes, each byte is 0-255 long, a total of 4.2 billion, which was exhausted in 2011

      • IPV6:fe80::b045:2a9:c8e7:e171%10128 bits, 8 unsigned integers separated by colons

        Example 2022:0ba2:bbac:0012:0002:0032:1aab:1214
        
    • Public network (Internet) - private network (LAN):

      • ABCD class address

      • 192.168.xxx.xxx: specially for internal use of the organization

  • Domain name: memory IP problem

    • IP
package com.Mood;

import java.net.InetAddress;
import java.net.UnknownHostException;

//Test IP
public class TestnetAddress {
    public static void main(String[] args) {
        try{
            //Query local address
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress1);
            InetAddress inetAddress3 = InetAddress.getByName("localhost");
            System.out.println(inetAddress3);
            InetAddress inetAddress4 = InetAddress.getLocalHost();//Get local address
            System.out.println(inetAddress4);

            //Query website ip address
            InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress2);

            //common method
            //System.out.println(inetAddress2.getAddress());
            System.out.println(inetAddress2.getCanonicalHostName());//Get canonical address name
            System.out.println(inetAddress2.getHostAddress());//ip
            System.out.println(inetAddress2.getHostName());//Domain name or the name of your computer
        }
        catch (UnknownHostException e){
            e.printStackTrace();
        }

    }
}

port

The port represents the process of a program on the computer

  • Different processes have different port numbers to distinguish software. Port numbers cannot conflict

  • The port is specified in the range of 0~65536

  • TCP, udp:65535*2, tcp:80, udp:80, port numbers under a single protocol cannot conflict

  • Port classification:

    • Public port: 0~1023

      • HTTP:80 default port
      • HTTPS:443 default port
      • FTP:21 default port
      • Telent:23 default port
    • Program registration port: 1024~49151, assigned to users or programs

      • Tomcat: 8080
      • MySOL: 3306
      • Oracle: 1521
    • Dynamic and private: 49152~65535

      netstat -ano #View all ports
      netstat -ano|findstr "5900" #View the specified port
      tasklist|findstr "11604" #View specific port processes
      
      package com.Mood;
      
      import java.net.InetAddress;
      import java.net.InetSocketAddress;
      
      //
      public class TestInetSocketAddress {
          public static void main(String[] args) {
              InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080);//Construction Port
              System.out.println(socketAddress);
      
              System.out.println(socketAddress.getAddress());
              System.out.println(socketAddress.getHostName());//Get hostname
              System.out.println(socketAddress.getPort());//Get port number
          }
      }
      

communication protocol

Network communication protocol: rate, transmission code rate, code deconstruction, transmission control

TCP/IP protocol (a set of protocols):

  • TCP: user transport protocol
  • UDP: User Datagram Protocol
  • IP: network interconnection protocol

TCP and UDP comparison:

  • TCP: call; Connection, stable
    • Three handshakes and four waves: at least three times are required to ensure a stable connection; Four guarantee disconnection
    • client. Server
    • The transmission is completed, the connection is released, and the efficiency is low
  • UDP: send SMS; Not connected, unstable
    • Client and server: there is no clear boundary
    • Send directly whether prepared or not
    • DDOS: saturation attack, causing port congestion

Tags: Java network server

Posted by priya_amb on Sun, 24 Jul 2022 03:58:35 +0930