JAVA20
Network Programming Network Communication Protocol
L computers in a computer network can transmit data to each other.
L Transfer Data There is a certain rule is the network protocol
l Provision of data transfer format rate steps
L must follow rules to transfer data
L There are many kinds, the most extensive is TCP/IP and UDP
L Learn TCP/IP mainly
Network model of TCP/IP protocol
L Application Layer
protocol for the application
L Transport Layer
Enable network programs to communicate UDP and TCP
L Network Layer
TCP/IP Core layer
L Link Layer
Optical fiber, network cable Drive protocol
IP address and port number
L IP Address
- Unique identification of the computer
- To be able to specify the transfer data and send the data
- Widespread use of IPv4
- Represented in a computer with a four-byte binary number
- For ease of identification, each byte is represented by a decimal number. Separating 0-255
- More and more IP address requirements may appear saturated
- There's a IPv6.
- expressed in 16 bytes
- Is IPv4 's 8*10 28 times to 2 of 128 times a square
- Solve the problem of insufficient resources
- Native loopback Address
127.0.0.1 on behalf of the native address
L Port number
- IP address can be connected to a computer
- To access a program in your computer
- Need port number
- Assigned by the operating system
- Binary number 0-65535 with two bytes made up of 16 bits
- 0-1024 not available because it is already occupied by well-known programs or network services
- For example, browser port number 80
InetAddress class
L represents an IP address in the Internet
L Java.net.InetAddress
L can not instantiate an object
L static method gets
- Static inetaddress Getlocalhost ()
- The return value is a InetAddress object
- After the output is the IP address and host name
L Other static methods
- String gethostaddress () returns the host address
- String GetHostName () returns the host name
L Get non-native IP objects
- Static InetAddress Getbyname (String hostnmae)
- Delivery host name Get IP address object
UDP protocol
L is one of two important advanced protocols in the Transport layer
L Non-connected communication protocol
L No logical connection at both ends of data transfer
L The sender does not confirm whether the receiving end is present or not, and does not feedback whether it receives data
L Consumption of resources small communication efficiency is often used for audio and video transmission of ordinary data
L such as video conference space message
L loss of one or two packets will not have a significant impact on receiving results
L For example, computer video synchronization is just a bit of a card and it won't make much difference.
L Data size within 64KB
TCP protocol
L connection-oriented is the communication protocol
L provides reliable, error-free data transfer between two computers
L must clear the client and server
L Need "three handshake" per connection
L Client sends connection request service side feedback allows connection client feedback connection successful
• Slow connection but high security
L is widely used
L download file must use TCP
L Two Computers
L
UDP communication packets and sending objects
L Two Computers
L One send one receive
L The sender needs to know the receiving method IP and port number
L data needs to be packaged to send
Datagrampacket
L packing data equivalent to container of wharf
L encapsulate the address and port of the data receiving end
L Receive data-bound port number and sender-side consistency
Datagramsocker
L Send/Receive packaged data equivalent to the role of the terminal
Implementation steps
L Send End Create Datagrampacket object wrapper data and set the address and port of the receiving end
- Constructor method passed in parameter
L send a packet to the sending side to create the Datagramsocker object
- Call the Send method to pass in the Datagrampacket object for delivery
L send-side close resource Send Object Datagramsocker
l Example
L receive End Object binding port number for creating Datagrampacket
L Receive-side create byte array plus receive data sent
L The receiver creates an incoming array of Datagramsocker objects and arrays of lengths
L Call the Receive method to receive the data incoming parameter Datagrampacket object
L Unpacking
The IP address sent
Number of bytes Received
Sender's port number
L Close Resources
l Example
No end after running because receive has a thread waiting to detect the sending of UDP if the data is not sent it will always be detected
Split Package
But the length of the array is too large to cause waste
So call Datagrampacket's GetLength method to return the received data length
Unpacking IP Address
Call Datagrampacket's getaddress to get the IP object to the sending end
The IP address and hostname can be obtained by this object
Unpacking send port port number
Method of calling Datagrampacket Getport
TCP Communication
L have clear client and server
L Service End Class ServerSocket
L Client class Scoket
L Client actively connects to server
L Server pre-boot wait for client connection
L Client Pull Server
L Server agrees to connect
L The client and the server form a pathway. One of the objects is set up.
L This object is Io stream object byte stream
• Data exchange is possible
L Client Output service-side input service-side output client input
Scoket class
L This class implements a socket socket for a client that is the endpoint of two machine communication
L Java.net.Socket
L Construction Method
- Socket (Stringhost,int Port)
- Delivery server IP and port number
- The construction method fails to throw an exception if it runs with a connection to the server
- OutputStream Getouputstream () returns the output stream of the socket
Output the data to the server
- InputStream getInputStream () returns the input stream of a socket
Input data, read data from the server
- Example
Serverscoket
L This class implements a server-side socket
L Construction Method
Serverscoket (int port) passing the port number required by the client
The server needs to know who is connected to the server and therefore needs to get the server's socket object
L Get method
Accept () returns the client's socket object
l Example
Exchange data between client and server
L Client
L Service Side
Stream objects in TCP
Shutdownoutput
L read a byte array in the network stream is not read-1
L if the loop reads, the loop will die.
L This is the way to use it.
L DISABLE the output stream for this socket
L If this method is called, an exception is thrown
L Read thrown exception will return-1
L stopped the loop.
Java self-learning path-day20