Java无法获得客户端或服务器响应

时间:2015-04-03 00:12:27

标签: java connection connection-string server

我来自波兰,所以我对任何错误感到抱歉 当我停止烦人的问题时,我编写了一段时间的小型服务器和客户端连接。当我从客户端发送到服务器信息(String)时,服务器可以获取它,但不能响应它。

这是代码。

客户端

private static Socket socket;

public static void main(String args[]){
    try{
       String host = "localhost";
       int port = 25002;
       InetAddress address = InetAddress.getByName(host);
       socket = new Socket();
       socket.connect(new InetSocketAddress(host, port), 5000);

       //Send the message to the server
       System.out.println("< "+sendMessage(socket));
       //socket.shutdownOutput();
       System.out.println("> "+getMessage(socket));
   }catch (SocketTimeoutException e){
      System.out.println(e.getMessage()); // changed
   }catch (IOException e){
      System.out.println(e.getMessage()); // changed
   }catch (IllegalBlockingModeException e){
      System.out.println(e.getMessage()); // changed
   }catch(IllegalArgumentException e){
      System.out.println(e.getMessage()); // changed
   }finally{
      //Closing the socket
      try{
         socket.close();
      }catch(Exception e){
         System.out.println(e.getMessage()); // changed
      }
   }
}

public static String sendMessage(Socket client){
    try {
        String message = "test";
        PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
        writer = new PrintWriter(client.getOutputStream(), true);
        writer.print(message);
        writer.flush();
        writer.close();
        return message;
     } catch (IOException e) {
        System.out.println(e.getMessage()); // changed
        return "false";
     }
}

public static String getMessage(Socket client){
            try {
                BufferedReader socketReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                return socketReader.readLine();
            } catch (IOException e){
                System.out.println(e.getMessage()); // changed
                return "false";
            }
        }

和..服务器

public class kRcon{
        private static Socket socket;
        private static ServerSocket serverSocket;
        private static Thread u;

        private static class Server extends Thread {
                public void run() {
                        int port = 25002;
                        try {
                            serverSocket = new ServerSocket(port);
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        while(true) {
                          try {
                             socket = serverSocket.accept();
                             BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                             BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                             String str = socketReader.readLine();
                        socketReader.close();
                             System.out.println(str);
                             socketWriter.write("test");
                             socketWriter.flush();
                             socketWriter.close();
                          }
                        }catch (IOException e1) {
                            e1.printStackTrace();
                        }
               }
         public static void init(){
         try {
            u = new Server();
            u.setName("Server");
            u.start();
        } catch (IOException e) {
            System.out.println(e.getMessage()); // changed
        }
    }
}

结果
如果,我首先启动服务器看起来很好看。 因此,我使用parametr“test”启动客户端,nad输出到控制台是:

< test
Socket is closed // changed

在控制台的服务器端,我有:

"test"
Socket is closed // changed

我试图关闭输入和输出并且不起作用..我现在不知道要做什么。请帮忙:c

编辑2015-04-03
我改变了注释“已更改”的行。

适用于Google和读者 要解决问题,请不要在客户端关闭StreamReaders和StreamWriters。 感谢EJP,求助! 来自波兰的问候。

1 个答案:

答案 0 :(得分:0)

当您收到异常时,请将其打印出来。不要丢弃所有这些信息。并且也不会返回魔法Strings。在这种情况下,您应该让异常传播。如果你已经完成了所有这些,你会看到SocketException: socket closed引发异常getMessage(),,你会得到一些具体的调查内容,而不是一个完整的谜。

这是由关闭PrintWriter中的sendMessage().引起的。关闭套接字的输入或输出流关闭另一个流和套接字。