2

I'm writing a small client application to communicate with a server. I open a socket between my client and the server and can read anything that goes out from the server with a BufferedReader. This is read in a thread. However, when I write on the socket using a BufferedReader, nothing happens ! No exception but not any server response (and it should have a server response) Here is my code:

socketWritter.write(message);
socketWritter.write("\n");
System.out.println(socketWritter.toString());
socketWritter.flush();

My socket is correctly open and mu BufferedWriter correctly initialized :

new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))

I have no idea why this doesn't work ?? Any help would be great ! Regards

rmonjo
  • 2,675
  • 5
  • 30
  • 37

3 Answers3

15

Your code is correct. I tested it with a generic server that will echo whatever the client sends and it worked fine (with no changes). It could be that the server your using is faulty. One thing I noticed was that for my server I needed to append a new line character every time I wrote to the output stream, for it to actually send the data. I'm willing to bet that's why your GUI isn't receiving anything. Here's the client thread class from my server:

class ClientThread extends Thread {

    private Socket          sock;
    private InputStream     in;
    private OutputStream    out;

    ClientThread( Socket sock ) {
        this.sock = sock;
        try {
            this.in = sock.getInputStream();
            this.out = sock.getOutputStream();
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    //Echos whatever the client sends to it
    public void run() {
        BufferedReader bufIn = new BufferedReader( new InputStreamReader( in ) );
        BufferedWriter bufOut = new BufferedWriter( new OutputStreamWriter( out ) );
        while ( true ) {
            try {
                String msg = bufIn.readLine();
                System.out.println( "Received: " + msg );
                bufOut.write( msg );
                bufOut.newLine(); //HERE!!!!!!
                bufOut.flush();
            } catch ( IOException e ) {
                e.printStackTrace();
            }

        }
    }

}
blackcompe
  • 3,180
  • 16
  • 27
  • The thing is that when using telnet, the server responds as excepted. What I write into my socket in java is the exact same thing I write into telnet command line. Thanks for your help. – rmonjo Nov 26 '11 at 01:08
  • Could this be a problem of string encoding. For example server is excepting UTF-8 and I send to it Ansi (or something like this) – rmonjo Nov 26 '11 at 01:24
  • Read again your answer and actually, my UI receive everything (keep alive packet, or info when I log to the server using telnet). But when I write on the socket (not using telnet) the server acts as if it didn't get anything :((( – rmonjo Nov 26 '11 at 02:21
  • Ok error seems to come from the server (I haven't access to it but contacted the admin). Thanks for your help !! – rmonjo Nov 26 '11 at 03:23
  • I noticed something strange, if I don't supply a new line char on my INPUT to the server, I'll still be able to parse the input server side, however when I try to write the output back to the client, i'll receive nothing. I *have* to put a new line for the input, and the output. – Nathan F. Apr 14 '18 at 00:48
1

At this point, there is no convincing evidence that your client code is not working. In particular, this statement:

System.out.println(socketWritter.toString());

won't tell you anything useful. The toString() method only on a Stream object most likely only tells you the object's class name and its identity hashcode. It certainly won't tell you what you've written to the stream.

Given that, there is no clear evidence that the flush hasn't worked. (The problem could be on the server side.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Yes sure the toString was just a desperate try ! It doesn't come from the server since using telnet, typing exactly what is sent in the message variable, the server responds – rmonjo Nov 26 '11 at 00:09
0

Use only OutputStreamWriter without BufferedWriter and use flush()

Alexmelyon
  • 1,168
  • 11
  • 18