How do I send a string to a thread to create a chat program with Java socket programming?

Asked 2 weeks ago, Updated 2 weeks ago, 3 views

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner;

public class Server { private ServerSocket serverSocket = null; private Socket socket = null; private BufferedReader in = null; private BufferedWriter out = null; private Scanner sc = new Scanner(System.in);

public Server() {

    try {
        System.out.println ("waiting connection");
        serverSocket = new ServerSocket(9999);
        socket = serverSocket.accept();
        System.out.println ("connected");

        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        // The string entered by the server
        String serverMessage = null;
        while (true) {
            System.out.print ("Server: ");
            serverMessage = sc.nextLine();
            out.write(serverMessage + "\n");
            out.flush();
        }

    } } catch (Exception e) {
        e.printStackTrace();
    }
    GetMessage th = new GetMessage();
    th.start();
}

// String entered by Client
class GetMessage extends Thread {
    @Override
    public void run() {
        String clientMessage = null;
        while (true) {
            try {
                if (clientMessage == null) {
                    return;
                }
                clientMessage = in.readLine();
                System.out.println ("client: " + clientMessage);
            } } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

public static void main(String[] args) {
    new Server();
}

}

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Scanner;

public class Client { private Socket socket = null; private BufferedReader in = null; private BufferedWriter out = null; private Scanner sc = new Scanner(System.in);

public Client() {
    GetMessage th = new GetMessage();
    th.start();
    try {
        socket = new Socket("localhost", 9999);
        System.out.println ("connected");

        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        // String entered by Client
        String clientMessage;
        while (true) {
            System.out.print ("Client: ");
            clientMessage = sc.nextLine();
            out.write(clientMessage + "\n");
            out.flush();
        }

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

// The string entered by the server
class GetMessage extends Thread {
    @Override
    public void run() {
        String serverMessage = null;
        while (true) {
            try {
                if (serverMessage == null) {
                    return;
                }
                serverMessage = in.readLine();
                System.out.println ("server: " + serverMessage);
            } } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public static void main(String[] args) {
    new Client();
}

}

If you send a text on the server side, the text is not visible on the client side. What's the problem?

java socket thread server

2022-09-20 11:30

1 Answers



public Server() {
    GetMessage th = new GetMessage();
    th.start();



    try {
        System.out.println ("waiting connection");
        serverSocket = new ServerSocket(9999);
        socket = serverSocket.accept();
        System.out.println ("connected");

        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        // The string entered by the server
        String serverMessage = null;
        while (true) {
            System.out.print ("Server: ");
            serverMessage = sc.nextLine();
            out.write(serverMessage + "\n");
            out.flush();
        }

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

}

Try to do the thread execution part below so that it runs before the try! It's while(true), so I don't think the thread will run if it's below.

GetMessage th = new GetMessage();
    th.start();


2022-09-20 11:30

If you have any answers or tips


© 2022 pinfo. All rights reserved.