Network communication protocol:
- Application layer:
- HTTP: Hypertext Transfer Protocol
- FTP: Text Transfer Protocol
- SMTP: mail delivery protocol
- POP: mail receiving protocol
- Transport layer:
TCP: transmission control protocol (three handshakes)
UDP: user packet protocol
- network layer
- IP: network protocol
- data link layer
- physical layer
IP:
- Network address: uniquely identifies each computer on the network
- Address composition: 32 bits, consisting of 4 8-bit binary numbers.
- Manifestation of local ip:
- 127.0.0.1
- localhost
- LAN ip (you can use the ipconfig command to query)
- Wan ip (Baidu direct search ip)
- Common instructions:
- ipconfig: view the configuration of all local networks
- ping: you can test whether the network is unobstructed and check the ip mapped by the domain name
DNS:
- Domain Name System
- DNS is a distributed server system, which realizes the insinuation of domain name and ip address
- 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
- Construction method
URL url = new URL("http://www.longge.vip/study.java"); System.out.println(url);
- encode() encrypts Chinese characters in the website
String encodeStr = URLEncoder.encode("http://www.longge.vip/study","utf-8");
- Decode the garbled code in the web address;
String decoderStr = URLDecoder.decode(emcoderStr,"utd-8")
- View the ip address of this machine
InetAddress localHost = InetAddress.getLocalHost();
- View ip of domain name mapping
InetAddress ip1 = InetAddress.getByName("www.baidu.com");
- View all ip addresses of domain name mapping
InetAddress[] ips = InetAddress.getAllByName("www.taovao.com");
- 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
TCP | UDP |
---|---|
Connection oriented | Non connection oriented |
It's safe | It's not safe |
Relatively slow | Relatively fast |
Suitable for transferring large files | Suitable for transferring small files |
be careful:
Socket closing caused by output stream closing
- 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
- Through socket Shutdownoutput() closes the output stream, but the socket is still connected and the connection is not closed
- 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