3

I have two Java applications, where an Android client connects to a server on a computer and sends a message using BufferedWriter over websockets.

The client:

    try {
        toast("Sending...");
        Socket sock = new Socket(ip, PORT);
        OutputStream os = sock.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        bw.flush();
        bw.write("Hello Server!");
        toast("Connected!");
    } catch (UnknownHostException e) {
        toast(e.getMessage());
    } catch (IOException e) {
        toast(e.getMessage());
    }

The server:

public static void main(String[] args) {
    ServerSocket server;
    ConnectionThread ct;
    Socket s;
    ExecutorService es = Executors.newSingleThreadExecutor();
    try {
        System.out.println("Starting server...");
        server = new ServerSocket(1337);
        s = server.accept();
        ct = new ConnectionThread(s);
        es.execute(ct);
    } catch (IOException ex) {
        ex.printStackTrace();
    }       
}

The ConnectionThread class:

public class ConnectionThread implements Runnable {
    private Socket sock;
    private InputStream is;
    private BufferedReader br;
    private boolean online;
    public ConnectionThread(Socket s) {
        System.out.println("Creating connection thread.");
        this.sock = s;
        online = true;
    }
    @Override
    public void run() {
        String input = "";
        try {
            System.out.println("Starting to read...");
            is = sock.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));

            while (online) {
                input = br.readLine();
                if(input != null){
                    System.out.print("Received message: ");
                    System.out.println(input);
                }

            }

            br.close();
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

When I run the server, and then the client, the client will show the "Connected!" toast, and the server's output will be:

Starting server...
Creating connection thread.
Starting to read...

So, it seems like the connection is actually being made, but the message does not arrive. Does anybody know why this could be happening?

Neko
  • 3,550
  • 7
  • 28
  • 34

3 Answers3

4

Your server is expecting a complete line terminated by a newline. Try:

bw.write("Hello Server!");
bw.newLine();
Guido Simone
  • 7,912
  • 2
  • 19
  • 21
  • Oh my... That's it indeed. I knew it was going to be something tiny yet obvious that I had overlooked. Thanks! – Neko Oct 16 '12 at 14:57
0

Do it like this...

String s = new String();

while ((br.readLine())!=null) {

                s = s+br.readLine();

                    System.out.print("Received message: ");
                    System.out.println(input);
                }

            }

And

     bw.println("Hello Server");
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

I notice that you don't send an endline on your client, so the BufferedReader.readline() will never return, because it cannot match the \n-character. Try it again with

        bw.write("Hello Server!\n");

on the client side.

charno
  • 474
  • 6
  • 14