2, TCP network programming

catalogue

1, Network programming for TCP

1. Example 1: the client sends information to the server, and the server displays the data on the console

① Client

② Server

2. Example 2: the client sends the file to the server, and the server saves the file locally

① Client

② Server

3. Example 3: Send a file from the client to the server, the server saves it locally, returns "send successfully" to the client, and closes the corresponding connection

2, Client server

1. Client

① Browser

② Custom

2. Server

① Tomcat server

② Custom

3, UDP network programming

1.UDP network communication

2. Sender

3. Receiving end

1, Network programming for TCP

1. Example 1: the client sends information to the server, and the server displays the data on the console

① Client

    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            //1. Create a Socket object to indicate the IP and port number of the server
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet,8899);
            //2. Obtain an output stream for outputting data
            os = socket.getOutputStream();
            //3. Write data
            os.write("Hello, this is the client".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4. Closure of resources
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

② Server

    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1. Create a ServerSocket on the server side and indicate its own port number
            ss = new ServerSocket(8899);
            //2. Call accept() to receive the socket from the client
            socket = ss.accept();
            //3. Get input stream
            is = socket.getInputStream();
            //It is not recommended to write like this. There may be garbled code
//        byte[] buffer = new byte[1024];
//        int len;
//        while ((len = is.read(buffer)) != -1){
//            String str = new String(buffer,0,len);
//            System.out.println(str);
//        }
            //4. Read the data in the input stream
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[20];
            int len;
            while ((len = is.read(buffer)) != -1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5. Close resources
            if(baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

2. Example 2: the client sends the file to the server, and the server saves the file locally

① Client

    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);

            os = socket.getOutputStream();

            fis = new FileInputStream(new File("2390865548.jpg"));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

② Server

 @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            ss = new ServerSocket(9090);

            socket = ss.accept();

            is = socket.getInputStream();

            fos = new FileOutputStream("2390865548(1).jpg");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

3. Example 3: Send a file from the client to the server, the server saves it locally, returns "send successfully" to the client, and closes the corresponding connection

    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),8080);

            os = socket.getOutputStream();

            fis = new FileInputStream(new File("2390865548.jpg"));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1){
                os.write(buffer,0,len);
            }

            socket.shutdownOutput();

            //Receive data from the server and display it on the console
            InputStream is = socket.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer2 = new byte[1024];
            int len2;
            while ((len2 = is.read(buffer2)) != -1){
                baos.write(buffer2,0,len2);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                if(baos != null){
//                    baos.close();
//                }
            }
        }
    }
    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            ss = new ServerSocket(8080);

            socket = ss.accept();

            is = socket.getInputStream();

            fos = new FileOutputStream("2390865548(2).jpg");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
            //The server gives feedback to the client
            OutputStream os = socket.getOutputStream();
            os.write("Hello, the photo has been received!".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
//            if (os != null){
//                os.close();
//            }
        }
    }

2, Client server

1. Client

① Browser

② Custom

2. Server

① Tomcat server

② Custom

3, UDP network programming

1.UDP network communication

  • Classes datagram socket and datagram packet implement network programs based on UDP protocol.
  • UDP datagrams are sent and received through datagram socket. The system does not guarantee that UDP datagrams can be safely sent to the destination, nor can it determine when they can arrive.
  • The datagram packet object encapsulates UDP datagrams, which contain the IP address and port number of the sender and the lIP address and port number of the receiver.
  • Each datagram in UDP protocol gives complete address information, so there is no need to establish a connection between the sender and the receiver. It's like sending an express package.

2. Sender

    @Test
    public void send() throws IOException {
        DatagramSocket socket = new DatagramSocket();
        String str = "I am UDP Sent by~";
        byte[] data = str.getBytes(StandardCharsets.UTF_8);
        InetAddress inet = InetAddress.getLocalHost();
        DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
        socket.send(packet);
        socket.close();
    }

3. Receiving end

    @Test
    public void receiver() throws IOException {
        DatagramSocket socket = new DatagramSocket(9090);
        byte[] buffer = new byte[20];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length)
        socket.receive(packet);

        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }

Keywords: Java network socket Network Protocol TCP/IP

Added by RoundPorch on Mon, 24 Jan 2022 03:59:37 +0200