为什么消息从未发送?

时间:2014-05-29 14:02:45

标签: java sockets client-server

出于某种原因,我从来没有收到从服务器回到客户端的消息:/什么事情发生了?我怎么知道或解决这个问题?

我从客户端发送请求,服务器获取该请求,处理它并生成发送的响应。但客户从不读它。

public class Client {

private static Socket socket;

public static void main(String args[]) {
    try {
        String host = "localhost";
        int port = 13579;
        System.out.println("Conecting to : " + host + ":" + port);
        InetAddress address = InetAddress.getByName(host);
        socket = new Socket(address, port);

        //Send the message to the server
        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);

        String sendMessage = "103700635105281047295162150000001418    99900001000000717999000NovoTransactionsBusiness 717          VE000000000054300052810472900000000000099900001                  1803\n";

        bw.write(sendMessage);
        bw.flush();
        System.out.println("Message sent to the server : " + sendMessage);

        //Get the return message from the server
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
   //But i never get the message back
        String message = br.readLine();

        System.out.println("Message received from the server : " + message);
    } catch (Exception exception) {
        exception.printStackTrace();
    } finally {
        //Closing the socket
        try {
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 }
}

服务器运行正常!

public class Server {

private static Socket socket;

public static void main(String[] args) {
    try {
        int port = 13579;
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Servidor Iniciado escuchando al puerto " + port);
        while (true) {
            socket = serverSocket.accept();
            String strRequest = new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine();
            System.out.println("Request Received: " + strRequest);
            String returnMessage;
            try {
                returnMessage = new NovoTrans().init(strRequest).toString();
            } catch (Exception e) {
                returnMessage = "Error: " + e.getMessage() + "\n";
            }
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(returnMessage);
            System.out.println("Sending Message: " + returnMessage);
            bw.flush();
        }
    } catch (Exception e) {
    } finally {
        try {
            socket.close();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}
}

1 个答案:

答案 0 :(得分:2)

我建议您使用PrintWriter代替BufferedWriter。在每一行之后无需调用flush,只需使用println()方法以及自动刷新功能来添加新行。

  

与PrintStream类不同,如果启用了自动刷新,则仅在调用printlnprintfformat方法之一时才会执行,而不是每当换行符时碰巧是输出。

     

这些方法使用平台自己的行分隔符概念而不是换行符。

无需在邮件本身附加\n

示例代码:(执行服务器端和客户端类的更改)

// here true means auto flush when `println()` method is called
PrintWriter bw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
bw.println(returnMessage);