Java Scoket for two-way communication (code details)

Hello, my name is Chenle. I'm glad you can read it. This article summarizes the knowledge of Java Scoket class, and organizes the code related to two-way communication. There are also cases to achieve screenshots, share new knowledge and make progress together.

Article Directory

1. Network Communication

Network communication, data interaction between programs (senders) and programs (recipients) in the network.

Communication Elements 1 ip +Port Number 2 Transport Protocol

Java.netPackage: Contains the classes Java needs for network communication.

ServerSocket class, used to represent network services

Create a network service (create a ServerSocket object)

//constructor
public ServerSocket(int port)

//Create Network Service
ServerSocket server = new ServerSocket(9999);

Receive network requests

//Method of receiving network requests
public Socket accept()

//Network Services Receive Requests (Blocked)
Socket socket = server.accept();

Socket class, network socket, representing network connection

Send network requests
//constructor
public Socket(String ip, int port)
//Get the input stream from the network connection
public InputStream getInputStream()
//Get the output stream from the network connection
public OuputStream getOutputStream()

2. Receiver

The code is implemented as follows:The recipient of the information

public class Receiver {

    public static void main(String[] args) {

        try {
            ServerSocket server = new ServerSocket(8888);
            Socket socket = server.accept();
            System.out.println("----The program is already connected++++");
            InputStream is = socket.getInputStream();
            OutputStream os = socket.getOutputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            PrintWriter pw = new PrintWriter(os);
            Runnable r1 = new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        // Accept Information
                        String str;
                        try {
                            str = br.readLine();
                            System.out.println("sender Information" + str);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            Runnable r2 = new Runnable() {
                @Override
                public void run() {
                    // Return information
                    while (true) {
                        Scanner scan = new Scanner(System.in);
                        String msg = scan.nextLine();
                        pw.println(msg);
                        pw.flush();
                    }
                }
            };
            
            Thread t1 = new Thread(r1);
            Thread t2 = new Thread(r2);
            t1.start();
            t2.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. Sender

The code is implemented as follows:Sender of information

public class Sender {

    public static void main(String[] args) {
        try {
            Socket socket = new Socket("192.168.1.6", 8888);
            InputStream is = socket.getInputStream();
            OutputStream os = socket.getOutputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            PrintWriter pw = new PrintWriter(os);
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        // Send information

                        try {
                            Scanner scan = new Scanner(System.in);
                            String msg = scan.nextLine();
                            pw.println(msg);
                            pw.flush();
                        } catch (Exception e) {
                        }

                    }

                }
            };

            Runnable r2 = new Runnable() {
                @Override
                public void run() {
                    // Accept the message sent
                    while (true) {
                        String str;
                        try {
                            str = br.readLine();
                            System.out.println("recipient receiver:" + str);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            Thread t1 = new Thread(r);
            Thread t2 = new Thread(r2);
            t1.start();
            t2.start();

        } catch (UnknownHostException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}

4. Case Screenshot

First you see Open Receiver, then Sender

After opening Sender - see the Receiver console show that the connection was successful


Sender Sends Information

Receiver receives the information and replies

Point to the Sender console, see the reply information, and continue replying

Two-way communication and communication








The best investment is to invest in yourself

Chenling's 86th Blog on 06.22, 2020

Tags: network socket Java

Posted on Wed, 24 Jun 2020 21:53:24 -0400 by arnoldd99