简体   繁体   English

一个应用程序中的Server和ServerSocket:不起作用

[英]Server and ServerSocket in one Application: not working

I am trying to write a small program, that opens a server, creates a client that connects to this server and receives a message from it. 我试图编写一个小程序,打开服务器,创建连接到该服务器并从中接收消息的客户端。

This is the Code so far 到目前为止,这是《守则》

public static void main(String[] args) {
final ServerSocket serverSocket;
try {
    serverSocket = new ServerSocket(12345);
    Thread t = new Thread(){
        public void run(){
                try {
                    Socket server = serverSocket.accept();
                    PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
                    writer.write("Hello World");
                    writer.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
        Socket client = new Socket("localhost", 12345);
        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String message = reader.readLine();

        System.out.println("Received " + message);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

}

If i run program it keeps waiting in readLine() - so obviously the client does not receive the message from the server. 如果我运行程序,它将一直在readLine()中等待-因此很明显,客户端不会从服务器收到消息。 Has anyone got an idea why this isn' working? 有谁知道为什么这行不通?

Your reading thread is waiting for a newline in the data stream. 您的阅读线程正在等待数据流中的换行符。 Just change the server to use: 只需更改服务器即可使用:

writer.write("Hello World\r\n");

and you'll get the result you were expecting. 您将获得预期的结果。 Alternatively, you can just close the server socket, and then readLine will return when it reaches the end of the data stream. 或者,您可以只关闭服务器套接字,然后当readLine到达数据流的末尾时将返回它。

You should put the readline in a loop as follows: 您应该将readline放入循环中,如下所示:

public static void main(String[] args) {
    final ServerSocket serverSocket;
    try {
        serverSocket = new ServerSocket(12345);
        Thread t = new Thread() {
            public void run() {
                try {
                    Socket server = serverSocket.accept();
                    PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
                    writer.write("Hello World");
                    writer.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
        Socket client = new Socket("localhost", 12345);
        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            // Check this --------------------------------------------------->
            String message = null;
            while ((message = in.readLine()) != null) {   
                    System.out.println("Received " + message);
                    break; //This break will exit the loop when the first message is sent by the server
            }

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

}

You can read this documentation for further explanation: http://download.oracle.com/javase/tutorial/networking/sockets/ 您可以阅读该文档以获取进一步的说明: http : //download.oracle.com/javase/tutorial/networking/sockets/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM