Summary of Basic Learning of java-Network Communication

Keywords: socket Java network Mobile

Connection-oriented TCP

"Connection-oriented" means that a connection must be established with the other party before formal communication. For example, when you call someone, you have to wait until the line is connected and the other person picks up the microphone to talk to each other.

TCP (Transmission Control Protocol) is a connection-based protocol, that is to say, reliable connections must be established with each other before data can be sent and received formally. A TCP connection has to go through three "dialogues" before it can be established. The process is very complicated. We only do a simple and visual introduction here. You just need to understand the process. Let's take a look at the simple process of these three dialogues: Host A sends a connection request packet to Host B: "I want to send you data, okay?" This is the first conversation. Host B sends a packet to Host A that agrees to connect and asks for synchronization (synchronization means two hosts are sending one, receiving one, coordinating work): "Yes, when will you send it?" This is the second conversation. Host A sends another packet to confirm the synchronization of Host B's request: "I'll send it now, you go on!" This is the third dialogue. The purpose of three dialogues is to synchronize the sending and receiving of data packets. After three dialogues, host A sends data to host B formally.
TCP protocol can provide a reliable communication connection for applications, so that the byte stream sent by a computer can be sent to other computers on the network without error. The data communication system which requires high reliability often uses TCP protocol to transmit data.

Connectionless UDP protocol

"Connection-oriented" means that before formal communication, there is no need to establish a connection with the other party, regardless of the other party's status, it is sent directly. This is very similar to the popular mobile phone text messages: when you send a text message, you just need to enter the phone number of the other party to OK.
UDP (User Data Protocol) is a protocol corresponding to TCP. It is a non-connection-oriented protocol. It does not establish a connection with the other party, but sends data packets directly to the other party.

Socket

   Two JAVA applications implement data exchange through a two-way network communication connection, and a segment of this two-way link becomes a Socket.
   Socket is usually used to implement client-server connection
   Two classes of Socket and Server Socket defined in the java.net package are used to implement two connected client and server ends, respectively.
   The address information needed to establish the connection is the IP address and Port number of the remote computer.

   TCP port and UDP port are separated, each has 65535 ports.

Communication Process of Socket

   1. Create Socket connections between communication parties, that is, create Socket objects for server and client respectively, and establish Socket connections.
   2. Open the input and output streams of sockets connected.
   3. Read and write Socket according to certain protocols.
   4. Close the Socket connection after the read-write operation.

TCP Communication Model

A simple example
MyServer.java

import  java.io.*;
import  java.net.*;

public class MyServer{
    public static void main(String[] args){
        try{
            String s;
            //Register Services on Port 2005
            ServerSocket server = new ServerSocket(2005);
            System.out.println("Wait for a connection with the client!");
            Socket socket = server.accept();//Blocking listens for connection requests and waits for connections
            System.out.println("Successful connection with client!");
            //Get the input and output stream of the corresponding Socket
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();
            //Establishing data streams
            DataInputStream din = new DataInputStream(in);
            DataOutputStream dout = new DataOutputStream(out);
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            while(true){
                s = din.readUTF();//Read in strings from client
                System.out.println("The message received from the client is:" + s);
                if(s.equals("bye")){
                    System.out.println("Successful exit from the server!");
                    break;
                }
                System.out.println("Please enter the message to be sent:");
                s = sin.readLine();//Read the string entered by the user
                dout.writeUTF(s);//Pass the read string to client
                if (s.equals("bye")){
                    System.out.println("Successful exit from the server!");
                    break;
                }
            }

            //Close connection
            din.close();
            dout.close();
            in.close();
            out.close();
            socket.close();
        }catch(Exception e){
            System.out.println("Error:" + e);
        }
    }
}

MyClient.java

import  java.io.*;
import  java.net.*;

public class MyClient{
    public static void main(String[] args){
        try{
            String s;
            //The port number should be the same as the server port number.
            Socket socket = new Socket("127.0.0.1", 2005);
            System.out.println("Successful connection with server!");
            //Get the input and output stream of the corresponding socket
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();
            //Establishing data streams
            DataInputStream din = new DataInputStream(in);
            DataOutputStream dout = new DataOutputStream(out);
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));//Blocking Waiting for Display Input
            while (true){
                System.out.println("Please enter the information to be sent (enter) bye Exit):");
                s = sin.readLine();//Read the string entered by the user
                dout.writeUTF(s);//Pass the read string to the server
                if (s.equals("bye")){
                    System.out.println("Successfully exit the client!");
                    break;
                }
                s = din.readUTF();//Get strings from the server
                if (s.equals("bye")){
                    System.out.println("Successfully exit the client!");
                    break;
                }
                System.out.println("The message received from the server is:" + s);
            }
            //Close connection
            din.close();
            dout.close();
            in.close();
            socket.close();
        }catch (Exception e)
        {
            System.out.println(e);
        }
    }
}

Operation result diagram:

Original address: http://sheer.iteye.com/blog/1892347

Posted by goobers on Wed, 27 Mar 2019 18:54:28 -0700