-------Android Training, Java training, look forward to communicating with you! ----------
I. Network model
OSI Reference Model
Data is passed from top to bottom to encapsulate the layer-specific information into the packet, finally through the bottom of the physical transmission, after receiving from the bottom to the upper layer of the corresponding split packet resolution data. Each layer has its own rules and protocols.
1, Application layer, 2, presentation layer, 3, Session layer, 4, transport layer, 5, network layer, 6, Data link layer, 7, physical layer.
TCP/IP Reference Model
The TCP/IP reference model is a simplification of the OSI reference Model and is consistent in principle.
1, Application layer, 2, transport layer, 3, internetwork layer, 4, host to network layer.
Application layer HTTP protocol, Transport layer TCP/IP protocol, Internet Layer IP protocol.
Basic three elements of network communication
1.IP Address
The identity of the device in the network, not easy to remember, the available host name, local loopback address; 127.0.0.1, localhost, hostname, and IP address. The end is 0 o'clock is the network segment, at the end of 255 is the broadcast segment, you can receive
2. Port number
The identity of the different processes used to identify the logical address of the process.
Valid ports 0 through 65535, where 0 to 1024 are system reserved ports.
3. Transfer Protocol
The communication rule is the transport Protocol, the international Organization defines the universal Protocol (Internet Protocol) TCP/IP protocol, the UDP protocol
Two. TCP and UDP
1.UDP
Encapsulates data and sources and purposes into packets without establishing a connection
The size of each packet is within the limit of 64K
Because there is no connection, it is an unreliable agreement.
No need to establish a connection, fast.
2.TCP
Establish a connection to form a channel for transmitting data
Make large data transfers in a connection
Complete connection via three handshake, is a reliable protocol
The connection must be established and the efficiency will be slightly lower.
3.Socket (English translation socket)
A socket is a mechanism for network services
Sockets on both ends of each communication
Network communication is actually the communication between sockets
Data is transmitted via IO between two sockets.
Three. UDP transmission
Socket service for UDP transmissions
Datagramsocket class, Datagrampacket packet class
send-side requirements; Send a piece of text data over a UDP transmission
Ideas
1, establish Udpsocket service
2, providing data and encapsulating the data in a packet
3, send the data packets through the socket service sending function
4, close the resource
Receive-side requirements; Define an application to receive data transmitted by the UDP protocol and process the data
Ideas
1, define the Udpsocket service, listen to a port define a digital ID if it is not defined automatically assigns a
2, define a packet, because the received byte data is stored, because there are more functions in the packet object to extract different data information in the byte data
3, the received data is stored in a defined packet via the Receive method of the socket service. The method is a blocking method.
4, through the unique features of the packet object. Take these different amounts of data out and print them in the console
5, close the resource.
Part of the data to be collected and the part of the data sent
These two parts need to be executed simultaneously,
Then you need to use multithreaded technology.
A thread control to receive, a line programmed to send.
Import java.net.*;classudpsend{ Public Static voidMain (string[] args) throws Exception {//1, create the UDP service. Through the Datagramsocket object. Datagramsocket ds =NewDatagramsocket (8888); //2. Identify the data and encapsulate it into a packet. Datagrampacket (byte[] buf, int length, inetaddress address, int port) byte[] buf ="udp ge men lai le". GetBytes (); Datagrampacket DP=NewDatagrampacket (Buf,buf.length,inetaddress.getbyname ("192.168.1.254"),10000); //3, through the socket service, the existing data packets sent out. Through the Send method. Ds.send (DP); //4, close the resource. Ds.close (); }}classudprece{ Public Static voidMain (string[] args) throws Exception {//1, create the UDP socket and set up the endpoint. Datagramsocket ds =NewDatagramsocket (10000); while(true) { //2. Define the packet. Used to store data. byte[] buf =New byte[1024x768]; Datagrampacket DP=NewDatagrampacket (buf,buf.length); //3, the received data is stored in the packet via the Receive method of the service. Ds.receive (DP);//blocking method. //4, the data is obtained through the method of the packet. String IP =dp.getaddress (). gethostaddress (); String Data=NewString (Dp.getdata (),0, Dp.getlength ()); intPort =Dp.getport (); System. out. println (ip+"::"+data+"::"+port); } //5, close the resource//ds.close (); }}
Four. TCP Transmission
Client
By looking at the socket object, it is found that when the object is established, it is possible to connect to the specified host.
Because TCP is connection-oriented. Therefore, in the establishment of the socket service, there must be a service side, and the connection is successful, the formation of Tonglu after the transmission of data in the channel.
Steps
1, create the socket service and specify the host and port to connect to. Once the channel is established, it can get the socket stream, which encapsulates the input and output streams.
2, in order to send data, you should get the output stream in the socket stream
Service side
Define the endpoint to receive data and print it on the console
1, establish the service side of the Cocket service. ServerSocket () and listen on a port
2, get the link to the client object, through the Serversoket Accpet method, no connection will wait, so this method blocking
3, the client sends the data, then the server uses the corresponding client object and gets the read stream to the object.
4, close the client, optionally close the service side
Import java.io.*; import java.net.*;classtcpclient{ Public Static voidMain (string[] args) throws Exception {//Create the client socket service. Specify destination host and PortSocket s =NewSocket ("192.168.1.254",10003); //In order to send data, you should get the output stream in the socket stream. OutputStream out=S.getoutputstream (); out. Write ("tcp ge men lai le". GetBytes ()); S.close (); }}classtcpserver{ Public Static voidMain (string[] args) throws Exception {//set up a service-side socket service. And listen for a port. ServerSocket SS =NewServerSocket (10003); //The Accept method gets the client object that is connected. while(true) {Socket s=ss.accept (); String IP=s.getinetaddress (). gethostaddress (); System. out. println (ip+"... .. connected"); //to get the data sent by the client, the read stream of the client object is used to read the data. InputStreaminch=S.getinputstream (); byte[] buf =New byte[1024x768]; intLen =inch. Read (BUF); System. out. println (NewString (BUF,0, Len)); S.close ();//close the client. } //ss.close (); }}
The most likely problem with TCP transmission
Client connection on the service side, both sides are waiting, no
There is no data transfer.
Through routine analysis:
Because the Read method or the ReadLine method is a blocking type.
Workaround:
Custom end Tag
Use the Shutdowninput,shutdownoutput method.
Black Horse programmer _java Network programming