IP Address
IP Address IntAddress
- Only locate a computer on the network
- 127.0.0.1: Local localhost
- IP Classification of addresses
- ipV4/ipV6
- ipV4:127.0.0.1,4 Byte composition ;0~255,42 Billion ~;30 Billion in North America , Asia 4 Billion ;2011 It ran out in
- ipV6:128 position .8 Signed integers
- Public network ( Internet )- The private network ( LAN )
- ABCD Class address
- 192.168 .xx.xx, Specifically for use within an organization
- ipV4/ipV6
- domain name : Aspect memory , No records IP The problem of
1 // test IP 2 public class TestInetAddress { 3 public static void main(String[] args) { 4 try { 5 // Look up the local address 6 InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); 7 System.out.println(inetAddress); 8 InetAddress localhost = InetAddress.getByName("localhost"); 9 System.out.println(localhost); 10 InetAddress localHost = InetAddress.getLocalHost(); 11 System.out.println(localHost); 12 13 // Check the website ip Address 14 InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com"); 15 System.out.println(inetAddress1); 16 17 // Common methods 18 System.out.println(inetAddress1.getHostAddress());//ip 19 System.out.println(inetAddress1.getHostName());// domain name , Or your own name 20 } catch (UnknownHostException e) { 21 e.printStackTrace(); 22 } 23 } 24 }
port
ip Equivalent to province / City / District / Street, / floor , The port is the house number ; A port represents the process of a program on a computer
- Different processes have different port numbers ! It's used to distinguish software !
- Be regulated 0~65535
- TCP,UDP:65535*2;tcp:80;udp:80
- Port classification
- Public port 0~1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telent:23
- Program registration port :1024~49151, Assign users or programs
- Tomcat:8080
- MySQL:3306
- Orcal:1521
- dynamic 、 private :49152~65535
- Public port 0~1023
//CMD
netstat -ano # Look at all the ports
netstat -ano|findstr "5900" # View the specified port
tasklist|findstr "8696" # View the process of the specified port
1 // port 2 public class TestInetSocketAddress { 3 public static void main(String[] args) { 4 InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080); 5 System.out.println(socketAddress); 6 7 System.out.println(socketAddress.getAddress()); 8 System.out.println(socketAddress.getHostName());// Address 9 System.out.println(socketAddress.getPort());// port 10 } 11 }
Communication protocol
agreement : Appointment , Joint compliance , Can understand
Network communication protocol : rate , Transmission rate , The code structure , Transmission control ....
TCP/IP Protocol cluster : It's actually a set of agreements
important
- TCP: User transport protocol
- UDP: User datagram protocol
TCP UDP contrast
TCP: Make a phone call
- Connect , Stable
- Three handshakes , Four waves
- client 、 Server side
- Transmission complete , Release the connection , Low efficiency
UDP: texting
- Not connected , unstable
- client 、 Server side : There are no clear boundaries
- Ready or not , Can be sent to you
TCP Realize chat
1 // Server side 2 public class TcpServerDemo01 { 3 public static void main(String[] args) { 4 ServerSocket serverSocket = null; 5 Socket accept=null; 6 InputStream is=null; 7 ByteArrayOutputStream baos=null; 8 try { 9 //1. You have to have an address 10 serverSocket = new ServerSocket(9999); 11 12 while (true){ 13 //2. Wait for the client to connect 14 accept = serverSocket.accept(); 15 //3. Read the message from the client 16 is = accept.getInputStream(); 17 18 // Pipe flow 19 baos = new ByteArrayOutputStream(); 20 byte[] bytes = new byte[1024]; 21 int len; 22 while ((len=is.read(bytes))!=-1){ 23 baos.write(bytes,0,len); 24 } 25 System.out.println(baos.toString()); 26 } 27 28 } catch (IOException e) { 29 e.printStackTrace(); 30 }finally { 31 // Closed flow 32 try { 33 baos.close(); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 try { 38 is.close(); 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } 42 try { 43 accept.close(); 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } 47 try { 48 serverSocket.close(); 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 53 } 54 } 55 }
1 // client 2 public class TcpClientDemo01 { 3 public static void main(String[] args) { 4 Socket socket=null; 5 OutputStream os=null; 6 7 try { 8 //1. Until the server's address 9 InetAddress serverIP= InetAddress.getByName("127.0.0.1"); 10 int port=9999; 11 //2. Create a socker Connect 12 try { 13 socket = new Socket(serverIP,port); 14 //3. Send a message IO flow 15 os = socket.getOutputStream(); 16 os.write("Hello".getBytes()); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 21 22 } catch (UnknownHostException e) { 23 e.printStackTrace(); 24 }finally { 25 try { 26 os.close(); 27 } catch (IOException e) { 28 e.printStackTrace(); 29 } 30 try { 31 socket.close(); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 } 36 } 37 }
TCP Upload files
1 // Server side 2 public class TcpServerDemo02 { 3 public static void main(String[] args) throws Exception{ 4 //1. Create services 5 ServerSocket serverSocket = new ServerSocket(9000); 6 //2. Listen for client connections 7 Socket accept = serverSocket.accept();// Blocking monitoring , Will be waiting for the client to connect 8 //3. Get input stream 9 InputStream is = accept.getInputStream(); 10 11 //4. File output 12 FileOutputStream fos = new FileOutputStream("receive.jpg"); 13 byte[] by = new byte[1024]; 14 int len; 15 while ((len=is.read(by))!=-1){ 16 fos.write(by,0,len); 17 } 18 19 // Inform the client that I have finished receiving 20 OutputStream os = accept.getOutputStream(); 21 os.write(" End of reception ".getBytes()); 22 23 os.close(); 24 fos.close(); 25 is.close(); 26 accept.close(); 27 serverSocket.close(); 28 } 29 }
1 // client 2 public class TcpClientDemo02 { 3 public static void main(String[] args) throws Exception{ 4 //1. Create a socket Connect 5 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000); 6 //2. Create an output stream 7 OutputStream os = socket.getOutputStream(); 8 9 //3. Read the file 10 FileInputStream fis = new FileInputStream("D:\\WorkSpace\\JavaSE\\ Basic grammar \\111.png"); 11 //4. Write a document 12 byte[] by = new byte[1024]; 13 int len; 14 while ((len=fis.read(by))!=-1){ 15 os.write(by,0,len); 16 } 17 18 // Notification server , I've finished the transmission 19 socket.shutdownOutput(); 20 21 // Confirm that the server has received , To disconnect 22 InputStream is = socket.getInputStream(); 23 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 24 25 byte[] bytes = new byte[1024]; 26 int leng; 27 while ((leng=is.read(bytes))!=-1){ 28 baos.write(bytes,0,leng); 29 } 30 System.out.println(baos.toString()); 31 32 baos.close(); 33 is.close(); 34 os.close(); 35 fis.close(); 36 socket.close(); 37 } 38 }
UDP message sending
1 // The sender 2 public class UdpClientDemo01 { 3 public static void main(String[] args) throws Exception{ 4 //1. Build a Socket 5 DatagramSocket datagramSocket = new DatagramSocket(); 6 7 //2. Build a package 8 String msg=" How do you do , The server !"; 9 InetAddress localhost = InetAddress.getByName("localhost"); 10 int port = 9090; 11 12 // data 、 The length of the data starts with 、 To whom 13 DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port); 14 15 // Send package 16 datagramSocket.send(datagramPacket); 17 18 //4. Closed flow 19 datagramSocket.close(); 20 } 21 }
1 // The receiving party 2 public class UdpServerDemo01 { 3 public static void main(String[] args) throws Exception{ 4 // Open ports 5 DatagramSocket datagramSocket = new DatagramSocket(9090); 6 // receive data 7 byte[] bytes = new byte[1024]; 8 DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length); 9 10 datagramSocket.receive(datagramPacket);// Blocking reception 11 12 System.out.println(datagramPacket.getAddress()); 13 System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength())); 14 } 15 }
UDP Chat to achieve
1 // The sender 2 public class UdpSenderDemo01 { 3 public static void main(String[] args) throws Exception{ 4 5 DatagramSocket datagramSocket = new DatagramSocket(8888); 6 7 // Prepare the data : The console reads System.in 8 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 9 10 while (true){ 11 String data=reader.readLine(); 12 byte[] bytes = data.getBytes(); 13 DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress("localhost",6666)); 14 datagramSocket.send(datagramPacket); 15 if(bytes.equals("byebye")){ 16 break; 17 } 18 } 19 datagramSocket.close(); 20 } 21 }
1 // The receiving party 2 public class UdpReceiveDemo01 { 3 public static void main(String[] args) throws Exception{ 4 DatagramSocket datagramSocket = new DatagramSocket(6666); 5 6 while (true){ 7 // Ready to receive the package 8 byte[] bytes = new byte[1024]; 9 DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length); 10 11 // disconnect byebye 12 byte[] data = datagramPacket.getData(); 13 String string = new String(data, 0, data.length); 14 System.out.println(string); 15 if(string.equals("byebye")){ 16 break; 17 } 18 } 19 20 datagramSocket.close(); 21 22 } 23 }