[advanced features of Java] java learning journey 36 - network programming

Network communication protocol:

  1. Application layer:
  • HTTP: Hypertext Transfer Protocol
  • FTP: Text Transfer Protocol
  • SMTP: mail delivery protocol
  • POP: mail receiving protocol
  1. Transport layer:

TCP: transmission control protocol (three handshakes)
UDP: user packet protocol

  1. network layer
  • IP: network protocol
  1. data link layer
  2. physical layer

IP:

  1. Network address: uniquely identifies each computer on the network
  2. Address composition: 32 bits, consisting of 4 8-bit binary numbers.
  3. Manifestation of local ip:
  1. 127.0.0.1
  2. localhost
  3. LAN ip (you can use the ipconfig command to query)
  4. Wan ip (Baidu direct search ip)
  1. Common instructions:
  1. ipconfig: view the configuration of all local networks
  2. ping: you can test whether the network is unobstructed and check the ip mapped by the domain name

DNS:

  1. Domain Name System
  2. DNS is a distributed server system, which realizes the insinuation of domain name and ip address
  3. When we request the domain name (www.baidu.com), we will first resolve the DNS, get the ip address, and then continue to request the host corresponding to the ip address.

java.net package

1. URL class:

url: uniform resource locator, for example: http://www.longge.vip//study/javacore.avi ;

Analysis: protocol: / / domain name (ip) / resource path / resource name

  1. Construction method
URL url = new URL("http://www.longge.vip/study.java");
System.out.println(url);
  1. encode() encrypts Chinese characters in the website
String encodeStr = URLEncoder.encode("http://www.longge.vip/study","utf-8");
  1. Decode the garbled code in the web address;
String decoderStr = URLDecoder.decode(emcoderStr,"utd-8")
  1. View the ip address of this machine
InetAddress localHost = InetAddress.getLocalHost();
  1. View ip of domain name mapping
InetAddress ip1 = InetAddress.getByName("www.baidu.com");
  1. View all ip addresses of domain name mapping
InetAddress[] ips = InetAddress.getAllByName("www.taovao.com");
  1. Creating objects from ip addresses
InetAddress ip2 = InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, 1, (byte)172});

Realize network programming:

  • Network programming with TCP + Socket
  • UDP + Socket to realize network programming
TCPUDP
Connection orientedNon connection oriented
It's safeIt's not safe
Relatively slowRelatively fast
Suitable for transferring large filesSuitable for transferring small files

be careful:
Socket closing caused by output stream closing

  1. Then the client or server passes the socket Shutdownoutput() is closed individually, that is, closing the output stream of the client does not close the output stream of the server, so it is a one-way closing stream
  2. Through socket Shutdownoutput() closes the output stream, but the socket is still connected and the connection is not closed
  3. If the input stream or output stream is directly closed, that is: in Close() or out close(). The socket will be closed directly

TCP + Socket network programming:

//TCP client
private static Scanner sc = new Scanner(System.in);

public stataic void main(Stringp[[ args) throes Exception{
    
    //1. Create a Socket object and specify the host address and port number
    Socket client = new Socket("localhost" , 1904);
    System.out.println("Client startup completed!");
    
    //2. Create an output stream to send data to the host
    DataOuputStream dos = new DataOutputStream(client.getOutputStream());
    
    //3. Send data
    dos.writeUTF(sc.next());
    
    //4. Create an input stream to receive the data sent by the server
    DataInputStream  dis = new DataInputStream(client.getInputStream());
    
    //5. Receive the request sent by the server
    String message = dis.readUTF();
    System.out.println(message);
}
//TCP server

//1. Create a ServerSocket object and specify the port number
ServerSocket server = new ServerSocket(1904);
System.out.println("Server startup completed");

//2. Listen for client requests
Socket client = server.accept();
System.out.println("The server listens to the client connection!")

//3. Create an input stream to receive the data sent by the client
DataInputStream dis = new DataInputStream(client.getInputStream());

//4. Get the data sent by the client
String message = dis,readUTF();
System.out.println(message);

//5. Create an output stream to send messages to the client
DataOutputStream dos = new DataOutputStream(client.getOutputStream());

//6. Send data
dos.writeUTF("Hello, this is the server");

//7. Close resources
dos.close();
dis.close();
client.close();
server.close();

UDP + Socket realizes network programming:

//client1
public static void main(String[] args) throws IOException{
    
    //1. Create DatagramSocket object and specify port number
    DatagramSocket client1 = new DatagramSocket(6666);
    
    //2. Create datagrampactlet object to wrap data
    String message = "hello, I'm client 1";
    DatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length, InetAddress.getByName("localhost"), 8888);
    
    //3. Send packaged data
    client1.send(packet);
    
    //4. Close resources
    client1.close();
}
//client2
public static void main(String[] args) throws Exception{
    //1. Create DatagramSocket object
    DatagramSocket client2 = new DatagramSocket(8888);
    
    //2. Receive the request from client 1
    byte[] buf = new byte[1024];
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    client2.receive(packet);
    
    //3. View the received data
    byte[] data =  packet.getData();
    System.out.println(new String(data);
    
    //close resource
    client2.close();
}

Job:
Realize the console group chat function

Keywords: Java

Added by Jramz on Wed, 09 Mar 2022 18:10:27 +0200