dbncourt
dbncourt

Reputation: 580

Why is the message never delivered?

For some reason, i never get the message back from the server to the client :/ whats happening? how can i know or solve this?

I send the request from the client, the server gets that request, process it and generate a response that is send. But the client never reads it.

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

The server is running fine!

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

Upvotes: 0

Views: 49

Answers (1)

Braj
Braj

Reputation: 46881

I suggest you to use PrintWriter instead of BufferedWriter. There is no need to call flush after each line and simply use println() method along with auto-flush feature to add a new line as well.

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output.

These methods use the platform's own notion of line separator rather than the newline character.

There is no need to append \n in the message itself.

Sample code: (Do the changes in both server and client side classes)

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

Upvotes: 2

Related Questions