A good memory is better than a pen.
Two common types of network transport models:
I. OSI model layering
Professionals generally do not say that each layer of the name of the second is to say the first layer, which is equivalent to the level of the rank should be remembered clearly.
7 Application Layer-provides application services to end users.
6 Presentation Layer--Provides a representation of the data for the application. For example, the presentation layer tells the application layer where there is encryption or whether it is a. jpg picture.
5 Session Layer-manage sessions between users.
4 The Transport Layer--defines the data segment in the source and numbers, transmits the data, and reorganizes the data at the destination. Increase (source, destination) process number.
3 Network Layer--------to build and address packets for end-to-end delivery through intermediate devices on other networks. Increase (source, destination) logical network address.
(2) Data Link layer-----------Create and address frames for sending between devices on the local area network to host or WAN. Increase (source, destination) physical address.
1 Physical layer-transfer bit data between devices. The physical layer protocol defines the media specification. Timing and synchronization bit.
Second, TCP/IP model tiering
Note that no matter what the model is, the lower level is the physical layer, and the top layer is the content layer
4 Application Layer-represents the application data for the user.
3 Transport Layer-support communication between devices and perform error correction.
2 The Internet Layer--determine the best path through the network.
1 network interface Layer-------the hardware device and media
Network transport needs to know: address, port, protocol
----------------------------------------------------------Client Code---------------------------------------------------------- Package com.
Ckinghan.socket;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.DatagramPacket;
Import Java.net.DatagramSocket;
Import java.net.InetAddress;
Import java.net.SocketException;
Import java.net.UnknownHostException;
public class UdpClient {public static void main (string[] args) {/** * client program that initiates simple UDP traffic * *
UdpClient (); /** * @ Description: Client program for simple UDP Communication * creation time: */public static void UdpClient () {//create Dategramsocket Pair
Like datagramsocket datagramsocket = null;
Creates an input stream, reads data from the console bufferedreader bufferedreader = null;
Set server address string string IP = "192.168.0.178";
Set IP information inetaddress inetaddress; try {//instantiated inteaddress inetaddress = Inetaddress.getbynaMe (IP);
Instantiate datagramsocket Datagramsocket = new Datagramsocket ();
Reads data from the console (the default character encoding is GBK) BufferedReader = new BufferedReader (new InputStreamReader (system.in, "GBK"));
Create the message sent by the user String sendstring = null; Loop waits for user input while (true) {//Read the user's input sendstring = Bufferedreader.readline
(). Trim ();
Converts the information to be sent to an array of bytes byte[] sendbytes = sendstring.getbytes ("UTF-8"); Create and instantiate the Datagrampacket, specify the data to send, the length of data sent, the server IP address, the communication port datagrampacket Datagrampacket = new Datagrampacket (SE
Ndbytes, Sendbytes.length, inetaddress, 10000);
Send data Datagramsocket.send (datagrampacket) to the server;
If the user enters close, exit the chat client if ("Close". Equals (sendstring)) break;
} catch (Unknownhostexception e) {e.printstacktrace (); catch (SocketException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
}finally {//close stream if (datagramsocket!= null) {datagramsocket.close (); //Close Stream if (BufferedReader!= null) {try {Bufferedreader.close
();
catch (IOException e) {e.printstacktrace (); ----------------------------------------------------------Service-side code--------------------- -------------------------------------package com.
Ckinghan.socket;
Import java.io.IOException;
Import Java.net.DatagramPacket;
Import Java.net.DatagramSocket;
Import java.net.InetAddress;
Import java.net.SocketException;
public class Udpserver {public static void main (string[] args) {/** * server-side program to start simple UDP traffic * *
Socketserver ();
}
/** * Description: Server-side program for simple UDP Communication * creation time:/public static void Socketserver () {Datagramsocket Datagramsocke
t = null;
try {//instantiated dategramsocket Datagramsocket = new Datagramsocket (10000);
Byte[] Data for receiving data byte[] GetDate = new byte[1024];
Used to receive user-saved data datagrampacket datagrampacket = new Datagrampacket (getDate, getdate.length); Loops listen for information sent by the client while (true) {///Dategrampacket The data sent by the user into the Datagramsocket.rec
Eive (Datagrampacket);
Gets the received data byte length int len = Datagrampacket.getlength ();
Gets the received data byte[] date = Datagrampacket.getdata ();
Get request Source InetAddress inetaddress = datagrampacket.getaddress ();
Converts received bytes to characters (restores the information sent by the user) string receive = new string (date, 0, Len, "UTF-8"); If the input character is close, the user is prompted to exit theThe user of the ring if ("Close". Equals (Receive)) {System.out.println (inetaddress.gethostaddress () +) has exited Session.
");
Break
}//Output user sent the information System.out.println (inetaddress.gethostaddress () + ":" +receive);
} catch (SocketException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace (); }finally {//If Dategramsocket is instantiated, close stream if (datagramsocket!= null) {Datagramsocket.
Close ();
}
}
}
}
Execution effect: