Java ServerSocket 不会从客户端发送或接收消息 [英] Java ServerSocket won't send or receive messages from client

查看:54
本文介绍了Java ServerSocket 不会从客户端发送或接收消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

到目前为止,我尝试了最简单的示例,在端口 4444 上打开服务器,然后在相同 PC 上我打开了一个客户端连接到地址 127.0.0.1 (localhost),端口 4444.一切运行顺利,每次都成功建立连接,除了它不会发送任何消息,无论是从服务器到客户端,还是从客户端到服务器.

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.

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

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();
        }

    }

}

推荐答案

你在读台词,但不是在拧台词.您应该使用 out.println() 代替 out.write()out.print().

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

这篇关于Java ServerSocket 不会从客户端发送或接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆