Java ServerSocket won't send or receive messages from client

萝らか妹 提交于 2020-12-15 06:42:12

问题


I have these two classes Client.java and Server.java, each containing a main method. The idea is very simple and the code in them is very similar. The objective I'm trying to achieve is opening a server that listens on a specific port using ServerSocket, then open a client and connect to the server using Socket so that I can finally send messages from client to server and reverse until I close the connection.

So far, I tried the easiest example, opened server on port 4444, then on the same PC I've opened a client to connect to the address 127.0.0.1 (localhost), on port 4444. Everything runs smooth, the connection is established successfully every time, excepting it won't send any message, either from server to client or client to server.

The final question is, what should I do to be able to exchange messages between server and client? What should I modify?

Client.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

    private static Socket client;
    private static BufferedReader in, console;
    private static PrintWriter out;

    private static String serverAddress;
    private static int port;

    public static void main(String[] args) {

        try {
            console = new BufferedReader(new InputStreamReader(System.in));

            System.out.print("Connect to server address (example 192.168.10.11): ");
            serverAddress = console.readLine();

            System.out.print("Port: ");
            port = Integer.parseInt(console.readLine());

            System.out.println("\nTrying to connect...");
            client = new Socket(serverAddress, port);

            System.out.println("Connected to " + client.getRemoteSocketAddress());
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(client.getOutputStream());

            out.write("Connected!!!!!!");

            Thread input = new Thread() {

                public void run() {
                    String tmp;

                    try {
                        while((tmp = in.readLine()) != null) {
                            System.out.println("Server: " + tmp);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };

            Thread output = new Thread() {

                public void run() {
                    String tmp;

                    try {
                        while((tmp = console.readLine()) != null) {
                            out.print(tmp);
                        }
                    } catch(IOException e) {
                        e.printStackTrace();
                    }
                }
            };


            input.start();
            output.start();

            while(true) {
                if(!input.isAlive() && !output.isAlive()) {
                    client.close();

                    in.close();
                    console.close();
                    out.close();

                    break;
                }
            }

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

    }

}

Server.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    private static ServerSocket serverSocket;
    private static Socket server;
    private static BufferedReader in, console;
    private static PrintWriter out;

    private static int port;

    public static void main(String[] args) {


        try {
            console = new BufferedReader(new InputStreamReader(System.in));

            System.out.print("Open server on port (example 1234): ");
            port = Integer.parseInt(console.readLine());

            System.out.println("Server open");
            serverSocket = new ServerSocket(port);

            System.out.println("Waiting for a client to connect...\n");
            server = serverSocket.accept();

            System.out.println("Client " + server.getRemoteSocketAddress() + " connected!");
            in = new BufferedReader(new InputStreamReader(server.getInputStream()));
            out = new PrintWriter(server.getOutputStream(), true);

            Thread input = new Thread() {

                public void run() {
                    String tmp;

                    try {
                        while((tmp = in.readLine()) != null) {
                            System.out.println("Client: " + tmp);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };

            Thread output = new Thread() {

                public void run() {
                    String tmp;

                    try {
                        while((tmp = console.readLine()) != null) {
                            out.print(tmp);
                        }
                    } catch(IOException e) {
                        e.printStackTrace();
                    }
                }
            };

            input.start();
            output.start();

            while(true) {
                if(!input.isAlive() && !output.isAlive()) {
                    serverSocket.close();
                    server.close();

                    in.close();
                    console.close();
                    out.close();

                    break;
                }
            }

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

    }

}

回答1:


You are reading lines but you aren't wring lines. Instead of out.write() and out.print() you should use out.println().



来源:https://stackoverflow.com/questions/48633611/java-serversocket-wont-send-or-receive-messages-from-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!