简体   繁体   中英

Having trouble sending String from client socket to server socket, and vice versa

I am trying to create an application that will count the amount of times a button has been clicked. This client would connect to a server, and when the user clicks the button, it should increment the counter on the server. The server should then send back the current amount of clicks to the client. But that's where I'm having a bit of problems.

This is the relevant client-sided code.

public void actionPerformed(ActionEvent arg0) {
            try {
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);
            String target = "";
            bw.write("increment" + "\n");
            bw.flush();

            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String id = br.readLine();
            System.out.println("test: " + id);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

The client stops at:

String id = br.readLine();

I just want to get the output from the server.

This is the relevant server-sided code.

public void run() {
        try {
            while (true) {
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is); //Create the input Streams
                BufferedReader br = new BufferedReader(isr);
                String input = br.readLine();
                System.out.println("got input");
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);
                System.out.println("wrote to out");
                if(input.equals("increment" + "\n")) {
                    totalBets++;
                    System.out.println("inif");
                    bw.write(totalBets);    
                    System.out.println("wrote");
                    bw.flush();
                    System.out.println("flushed");
                    System.out.println("Total Bets: " + totalBets);
                }
            }
        } catch (IOException e) {
            log("Error handling client# " + clientNumber + ": " + e);
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                log("Couldn't close a socket, what's going on?");
            }
            log("Connection with client# " + clientNumber + " closed");
        }
    }

I found that it also stops here:

String input = br.readLine();

I'm just trying to get the bw.write("imcrement") from the client, so the server can increment the counter, and send back the total clicks.

Any help?

Thank you.

You need to set TCP_NODELAY on the client's socket. The default is for data to be buffered until it will fill an entire packet. When the buffer is full, a packet is sent. However, for this protocol you want the data to be sent immediately so that the server can respond.

I frequently use wireshark when testing and debugging my networking code. It will show exactly what packets are sent and received. (note, however, that on Windows you can not capture from the loopback interface; this is a limitation of Windows and does not apply to other systems)

You are writing the totalBets value using BufferedWriter.write(int) .

This interprets the value as a single character . So, for example, if totalBets is 65, it writes the character 'A' !

Moreover, it does not add a newline. So the client reads that 'A' but tries to read more characters as it is trying to read a whole line. Remember that you have to write lines to read lines.

Thus, you should replace the part that writes totalBets with:

bw.write(String.valueOf(totalBets));
bw.newLine();

Also remember, as I pointed in a comment, that you have to write a line with a \\n (or preferably BufferedWriter.newLine() ), but when you read the line on the other side, the line separator is stripped away, so you should compare the string you expect without a \\n .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM