I. Network Basics
Network Communication Protocol: for speed and transmissionCodeCode structure, transmission control steps, error control, and other standards.
Network Communication Interface: hardware device: Implements information transmission between nodes.
Software device: Agreement for communications between the parties.
Software developers should master the layer-4 model on the right.
Ii. TCP/IP protocol
IP protocol: provides unique IP addresses.
-| IPv4
Subnet Mask
Gateway
-| IPv6
TCP: a connection-oriented protocol. Reliable, end-to-end byte stream communication protocol. Byte stream.
That is, data can be transmitted in place. Data Packets arrive in order without any disordered order. Such as phone number.
Relatively low efficiency than UDP
UDP: a method for sending encapsulated original IP datagram, and no connection is required during sending. Is an unreliable connection.
Only sends data, regardless of whether the recipient receives the data. Such as a telegram.
TCP is reliable and slow.
UDP is not reliable, fast, and efficient.
Iii. Socket
1. Used for client-server connection
2. The two socket classes and serversocket classes defined in the java.net package are respectively used to implement two-way connection between the client and server.
3. The addressing information required for establishing a connection is the IP address and port number of the remote computer (port number after 1024 during development)
-| The TCP port is different from the UDP port.
Each port has 65536 ports.
Import Java. Io. datainputstream; Import Java. Io. ioexception; Import Java.net. serversocket; Import Java.net. Socket; Public Class Tcpserver { Public Static Void Main (string ARGs []) Throws Ioexception {serversocket SS = New Serversocket (6666 ); While ( True ) {Socket s = SS. Accept (); datainputstream dis =New Datainputstream (S. getinputstream (); system. Out. println (DIS. readutf (); DIS. Close (); S. Close ();}}} Import Java. Io. dataoutputstream; Import Java. Io. ioexception; Import Java. Io. outputstream; Import Java.net. Socket; Import Java.net. unknownhostexception; Public Class Tcpclient { Public Static Void Main (string ARGs []) Throws Unknownhostexception, ioexception {socket s = New Socket ("127.0.0.1", 6666 ); Outputstream OS = S. getoutputstream (); dataoutputstream DoS = New Dataoutputstream (OS); dos. writeutf ( "Hello gay! "); Dos. Flush (); dos. Close (); S. Close ();}}