Java Upgrade 01

Promotion · Network Programming and GUI

 Network programming overview

* A: Computer network
  * refers to the connection of multiple computers and external devices with independent functions in different geographical locations, through communication,
    under the management of the network operating system, network management software and network communication protocol to achieve resource sharing and information Delivery computer system

* B: Network programming
  * is to exchange data between programs running on different computers to realize network interconnection

 

IP address overview

* The unique identification of each device in the network
* Each network terminal has an independent address in the network, we use this address to transmit data in the network

* ipconfig: check local IP
* ping: test connection

* Local loop address: 127.0.0.1 
* Broadcast address: 255.255.255.255

* IPv4: 4 bytes, 4 numbers from 0 to 255, about 4.2 billion, 3 billion in North America, 400 million in Asia, has been exhausted in early 2011
* IPv6: 8 groups, each group of 4 hexadecimal numbers

 

Port number overview

* The unique identification of each program on the device
* Each network program needs to be bound to a port number. When transmitting data, in addition to determining which machine to send to, it must also be clear which program to send to

* The port number ranges from 0-65535
* When writing network applications, you need to bind a port number, try to use more than 1024, and basically less than 1024 are occupied by system programs

* Common port
  * MySQL: 3306
  * Oracle: 1521
  * web: 80
  * tomcat: 8080
  * QQ: 4000
  * feiQ: 2425

 

Network protocol overview

* Rules established for data exchange on computer networks

* UDP
  * Face no connection, data insecure, fast speed, no distinction between client and server

* TCP
  * Connection-oriented (three-way handshake), data security, slightly lower speed, divided into client and server
  * Three-way handshake: the client first initiates a request to the server, the server responds to the request, and transmits data

 

Socket communication principle

* A: Overview of Socket Socket
  * The uniquely identified IP address and port number on the network can be combined to form a uniquely identifiable socket
  * Both ends of the communication must be Socket
  * Network communication is actually between Socket Communication
  * Data is transferred between two Sockets through I / O streaming
  * Sockets are created in the application program, and a relationship is established with the driver through a binding mechanism, telling yourself the corresponding IP and port number

 

 

 

 

 

 

UDP transmission

* A: Send, Send
  * Create DatagramSocket, random port number
  * Create DatagramPacket, specify data, length, address, port
  * Use DatagramSocket, send DatagramPacket
  * Close DatagramSocket

* B: Receive, Receive
  * Create DatagramSocket, specify port number
  * Create DatagramPacket, is a fixed array, length
  * Use DatagramSocket, receive DatagramPacket
  * Close DatagramSocket
  * Get data from DatagramPacket

* C: Receiver gets ip and port number
  * String ip = packet.getAddress (). GetHostAddress ();
  * int port = packet.getPort ()

 

 

package com.heima.socket; 

import java.io.IOException;
 import java.net.DatagramPacket;
 import java.net.DatagramSocket;
 import java.net.InetAddress; 

public  class Demo1_Send { 

    public  static  void main (String [] args) throws IOException { 
        String str = "What are you doing?"; // Data to be sent 
        
        DatagramSocket socket = new DatagramSocket (); // Create a socket, which is equivalent to creating a dock, you can randomly bind 
        DatagramPacket packet = / without specifying a port number / Create a packet, which is equivalent to creating a container for storing data packets
                new DatagramPacket (str.getBytes (), str.length (), InetAddress.getByName ("192.168.0.101"), 61666 );
                                 //   effective length of the byte array array host name port number 
        
        socket.send (packet); / / Send data, equivalent to shipping 
        socket.close (); // Close the stream, equivalent to closing the terminal 
    } 
}
send
package com.heima.socket; 

import java.io.IOException;
 import java.net.DatagramPacket;
 import java.net.DatagramSocket; 


public  class Demo1_Receive { 

    public  static  void main (String [] args) throws IOException { 
        DatagramSocket socket = new DatagramSocket (61666); // Create Socket, bind port number 
        DatagramPacket packet = new DatagramPacket ( new  byte [1024], 1024); // Create Packet, specify the receiving array and its length 
        socket.receive (packet); // Receive data

        byte [] arr = packet.getData (); // Get data with an array 
        int len = packet.getLength (); // Get the number of valid bytes 
        System.out.println ( new String (arr, 0, len) ); // Call the constructor of String to convert the array to a string 
        socket.close (); 
    } 
}
receive

 

 

UDP transmission optimization

* Send, send keyboard input data

* Receive, receive and print

package com.heima.socket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Demo2_Send {

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in); // 创建键盘录入对象
        DatagramSocket socket = new DatagramSocket(); // 创建socket

        while (true) {
            String str = sc.nextLine (); // Get the string entered in the keyboard 
            
            if ("quit" .equals (str)) { 
                sc.close (); // Judge the string and exit the loop 
                break ; 
            } 
            
            DatagramPacket packet = / / Create a packet, convert the entered string into a byte array and pack 
                    new DatagramPacket (str.getBytes (), str.length (), InetAddress.getByName ("192.168.0.101"), 61666 ); 
            socket.send (packet ); // Send data 
        } 
        
        socket.close (); // Close stream 
    } 
}
send
package com.heima.socket; 

import java.io.IOException;
 import java.net.DatagramPacket;
 import java.net.DatagramSocket; 

public  class Demo2_Receive { 

    public  static  void main (String [] args) throws IOException { 
        DatagramSocket socket = new DatagramSocket (61666); // Create a Socket and bind the specified port number 
        DatagramPacket packet = new DatagramPacket ( new  byte [1024], 1024); // Create a Packet, specify the byte array and its length to receive 

        while ( true ) {// Define infinite loop 
            socket.receive (packet); // Receive data 
            
            byte [] arr = packet.getData (); // Get data 
            int len = packet.getLength (); // Get the number of valid bytes 
            
            String ip = packet.getAddress (). getHostAddress (); // Get the ip address 
            int port = packet.getPort (); // Get the port number 
            
            System.out.println (ip + ":" + port + ":" + new String (arr, 0, len)); // print data 
        } 

    } 
}
receive

 

Multithreaded UDP transmission

* Sending and receiving are done in one window

package com.heima.socket; 

import java.io.IOException;
 import java.net.DatagramPacket;
 import java.net.DatagramSocket;
 import java.net.InetAddress;
 import java.util.Scanner; 

public  class Demo3_MoreThread { 

    public  static  void main (String [] args) {
         new Receive (). Start (); // Open the receiving thread 
        new Send (). Start (); // Start the sending thread 
    } 
} 

class Receive extends Thread {   // Define the receiving class to inherit Thread 
    public  voidrun () { // Rewrite the run () method 
        try { 
            DatagramSocket socket = new DatagramSocket (61666); // Create Socket, bind port number 
            DatagramPacket packet = new DatagramPacket ( new  byte [1024], 1024); // Create Packet 

            while ( true ) { 
                socket.receive (packet); // Receive data 
                byte [] arr = packet.getData (); // Get data 
                int len = packet.getLength (); // Get the number of valid bytes 
                
                String ip= packet.getAddress (). getHostAddress (); // Get ip address 
                int port = packet.getPort (); // Get port number 
                
                System.out.println (ip + ":" + port + ":" + new String (arr, 0 , len)); 
            } 
        } catch (IOException e) { // After selecting the code block alt + shift + z one-click generation try catch 
            e.printStackTrace (); 
        } 
    } 
} 

class Send extends Thread { // define The sending class inherits Thread 
    public  void run () { // override the run () method 
        try{ 
            Scanner sc = new Scanner (System.in); // Create keyboard entry object 
            DatagramSocket socket = new DatagramSocket (); // Create socket 

            while ( true ) { 
                String str = sc.nextLine (); // Keyboard entry string 
                
                if ("quit" .equals (str)) { 
                    sc.close (); 
                    break ; 
                } 
                
                DatagramPacket packet = // Create a packet, convert the string to a byte array and pack 
                        new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName("192.168.0.101"), 61666);
                socket.send(packet); // 发送数据
            }
            
            socket.close(); // 关流
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
thread

 

 

GUI makes interface and uses UDP to realize chat room

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhaochuming/p/12726825.html