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
  • 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
//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 }

Java Network programming - More relevant articles summarizing the Overview

  1. 1.Java An overview of network programming

    Black horse programmer _ Bi Xiangdong _Java Basic video tutorial No 23 God -01- Network programming ( summary ) Learning notes There are three elements of network communication : 1.IP Address I. Identification of devices in the network   II. It's hard to remember , Available host names www The World Wide Web Organization ,baidu host ...

  2. JAVA Network programming 【 turn 】 The source is unknown

    Network programming Network programming for many beginners , It's a programming skill I'm looking forward to , But many beginners give up learning this part of technology because they can't enter the door of network programming for a long time . stay Before learning network programming , Many beginners may think that network editing ...

  3. 【 turn 】JAVA Network programming

    Network programming Network programming for many beginners , It's a programming skill I'm looking forward to , But many beginners give up learning this part of technology because they can't enter the door of network programming for a long time . stay Before learning network programming , Many beginners may think that network editing ...

  4. Java - Network programming is a complete summary

    This paper is the summary of my study in network programming , First, it mainly introduces the related content of computer network , Including the basis of computer network ,OSI Reference model ,TCP/IP Protocol cluster , Common network protocols and so on , On this basis , Introduce Java Network programming in . One . summary ...

  5. 【 Reprint 】Java Network programming

      This paper is the summary of my study in network programming , First, it mainly introduces the related content of computer network , Including the basis of computer network ,OSI Reference model ,TCP/IP Protocol cluster , Common network protocols and so on , On this basis , Introduce Java Network programming in . One . ...

  6. Java Network programming and NIO Detailed explanation 9: be based on NIO Network programming framework of Netty

    Java Network programming and NIO Detailed explanation 9: be based on NIO Network programming framework of Netty from https://sylvanassun.github.io/2017/11/30/2017-11-30-netty_introd ...

  7. Java Network programming and NIO Detailed explanation 7: Talking about Linux in NIO Selector Implementation principle of

    Java Network programming and NIO Detailed explanation 7: Talking about Linux in NIO Selector Implementation principle of from :https://www.jianshu.com/p/2b71ea919d49 This series of articles first appeared in my personal blog ...

  8. Java Network programming and simple chat program

    Network programming technology is one of the mainstream programming technologies in Internet technology , It is necessary to understand some basic operations . This chapter mainly explains network programming ,UDP and Socket Programming , And the use of Socket Make a simple chat software . Download all the code : link 1. Network editor ...

  9. java Network programming + Understanding of communication protocol

    Reference resources : http://blog.csdn.net/sunyc1990/article/details/50773014 Network programming for many beginners , It's a programming skill I'm looking forward to , But a lot of beginners because it is very difficult ...

  10. Java Network programming UDP Communication principle

    Preface Continue with today's Java Network programming --TCP and UDP signal communication One .TCP and UDP summary The transport layer usually consists of TCP and UDP Protocol to control communication between endpoints   TCP UDP Name of agreement Transmission control protocol User packet protocol yes ...

Random recommendation

  1. Distributed system design tradeoffs CAP

    Write at the top : 1. Why learn and document a series of related things about distributed design ideas During system design review in daily work , Some colleagues often throw out some concepts , High availability , Consistency and so on , They use these basic concepts to refute the original design of the system , however ...

  2. Data paging series 3 :Neo4j Graph data paging processing

      Let's start with a brief introduction Neo4j,Neo4j Is a high-performance NOSQL Graphic database , It stores structured data on the network instead of tables , It's an embedded . Disk based . With complete transaction characteristics Java Persistence engine , But it will structure numbers ...

  3. LCA Recent public ancestor Tarjan Offline algorithm

    Learning blog :  http://noalgo.info/476.html  It's very clear ! For a tree ,dfs Ergodic time , Go down first , And maintain the collection of current node and parent node with union lookup set . So if it's about the current node (A) The associated nodes of ( ...

  4. How to uniformly modify Altium Designer Character size in

    Here's the picture 1 Shown : Q1. Q2. C1. C2. R1 And so on. You want to uniformly change their size . Turned out to be Text Height( 100mil), Text Width( 12mil), Want to change to Text ...

  5. python Climb a single web page html Page crawl

    python Climb a single web page html Page crawl author :vpoet mail:[email protected] notes : at will copy Don't tell me #coding:utf-8 import urllib2 Re ...

  6. php Set the character set to utf-8

    header("Content-Type:text/html;charset=utf-8");

  7. 21.Linux- Write USB Keyboard drive ( Detailed explanation )

    The purpose of this section : According to the last section USB Mouse drive , Let's write the keyboard driver 1. First of all, let's modify it through the code in the previous section , To print out the data of the keyboard driver Let's first recall , We wrote about mouse driven id_table It's like this : therefore ...

  8. hdu 6231 -- K-th Number( Two points + Take with ruler )

    Topic link Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...

  9. css Add blur effect to the picture

    img{ -webkit-filter: blur(5px); -moz-filter: blur(5px); -ms-filter: blur(5px); filter: blur(5px); }

  10. Stocks K Line graph

    Code link address : Click to open the link