【问题标题】:out.println(); not working Java SocketServerout.println();不工作的Java SocketServer
【发布时间】:2019-01-20 15:36:51
【问题描述】:

我正在尝试通过客户端向服务器发送消息,但在服务器上看不到消息。它表明它正在正确连接。我认为问题与此有关,在 Client 类中,但我不太确定。如果有人有解决方案,请告诉我。

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String send = reader.readLine();
out.println(send);
out.flush();

这是客户端类:

package com.evolution.client;

import java.net.*;
import java.io.*;
public class Client {
   private Socket client;
   private PrintWriter out;
   private BufferedReader in;

   public static void main(String[] args) {
       Client client = new Client();
       client.startConnection("localhost", 325);
   }
   public void startConnection(String ip, int port) {
       try {
           client = new Socket(ip, port);
       out = new PrintWriter(client.getOutputStream(), true);
       in = new BufferedReader(new InputStreamReader(client.getInputStream()));
       System.out.println("Connected");
       out.println("Connection started from, " + client.getLocalAddress());
       out.flush();
       while (true) {
           String recieve = in.readLine();
           System.out.println(recieve);

           BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
           String send = reader.readLine();
           out.println(send);
           out.flush();
       }
            } catch (Exception e) {
               System.out.println(e);
        }
   }

   public void stopConnection() {
       try {
       in.close();
       out.close();
       client.close();
    }
       catch (Exception e) {

        }
}   
}

这是服务器类文件:

package com.evolution.server;

import java.io.*;
import java.net.*;

public class Server {
private int port = 325;
private ServerSocket server;
private Socket client;
private PrintWriter out;
private BufferedReader in;

public static void main(String[] args) throws IOException {
    Server server = new Server();
    System.out.println("Start Up!");
    server.start(server.port);
}

public void start(int port) throws IOException  {
    server = new ServerSocket(port);
    try {
        while (true) { //always runs unless break;
            client = server.accept();
               out = new PrintWriter(client.getOutputStream(), true);
                in = new BufferedReader(new 
InputStreamReader(client.getInputStream()));
                //out - send output 

                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    if (inputLine.equalsIgnoreCase("shutdown"))
                        stop();
                    System.out.println(inputLine);
                }
        }
    }   
    finally { //runs no matter what after try {}
        server.close();
            }
    }
public void stop() {
    try {
        in.close();
        out.close();
        client.close();
        server.close();
        System.exit(0);
    }
   catch (Exception e) {  
   }
}
}

如果您有任何可能的解决方案,请告诉我。

【问题讨论】:

  • 您的 stop() 方法将跳过第一个异常时的所有剩余步骤。您应该将每个 .close() 调用包装在其自己的 try..catch 块中,或者编写一个函数来关闭 InputStream/OutputStream/Socket。

标签: java serversocket println


【解决方案1】:

线 字符串接收 = in.readLine(); 在客户端将阻止 (BufferedReader readLine() blocks)。 由于服务器没有发送更多行,因此您的代码卡在那里。

删除此行允许您将消息从客户端发送到服务器。

【讨论】:

    猜你喜欢
    • 2011-01-31
    • 2015-12-29
    • 2011-06-02
    • 2016-04-27
    • 1970-01-01
    • 2013-07-23
    • 2016-10-14
    • 2018-12-22
    • 1970-01-01
    相关资源
    最近更新 更多