Java ServerSocket不会从客户端发送或接收消息

时间:2018-02-06 00:20:12

标签: java sockets server client-server serversocket

我有两个类 Client.java Server.java ,每个类都包含main方法。这个想法非常简单,其中的代码非常相似。我试图实现的目标是打开一个服务器,使用ServerSocket侦听特定端口,然后打开客户端并连接到服务器使用Socket以便我最终可以将消息从客户端发送到服务器并反向直到我关闭连接。

到目前为止,我尝试了最简单的示例,在端口 4444上打开了服务器,然后在相同的 PC上打开了我已经打开的一个客户端连接到地址 127.0.0.1(localhost),在端口 4444上。一切运行顺畅,每次成功建立连接,除了它赢得了' t发送任何消息,从服务器到客户端或客户端到服务器。

最后一个问题是,如何才能在服务器和客户端之间交换消息?我应该修改什么?

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 个答案:

答案 0 :(得分:2)

你正在读行,但你不是在行。您应该使用out.write()而不是out.print()out.println()