简体   繁体   中英

Java - Client recieving blank messages from Server, but only over network

I was unsure whether this issue is to do with the network, but I'm trying to get a simple server to send messages to multiple clients. It works fine when testing on my own computer, but over the network to a different computer has issues.

The client connects to the server fine, the first message sent to the client SHOULD be 'Server : Hello, World!', but a blank string comes through. Then the second string is always read as it should be, and all the rest usually come out as blank strings. The server sends info via a PrintStream, using autoflush.

Below is the part of the Client program which reads input. I believe if anything, there is an issue here, but I'm not sure what since I'm a little new to sockets and networking things. The server sends a line of text each 500 milliseconds, counting up to each client. I've tried changing this number to something higher but the client still doesn't receive the correct messages. The client should receive the messages and print them to the screen, replying with the replies seen below at 5,10,15 and 20.

    try {
        socket.setSoTimeout(10);
        String line;
        while((line = input.readLine())!=null){
            if (!line.equals("")){
                // Replies
                if (output != null) {
                    if (line.endsWith(" 20")){
                        output.println("Reached 20!");
                    }else if (line.endsWith(" 15")){
                        output.println("Reached 15!");
                    }else if (line.endsWith(" 10")){
                        output.println("Reached 10!");
                    }else if (line.endsWith(" 5")){
                        output.println("Reached 5!");
                    }
                }

                messages.add(line);
                if (messages.size() >= 8){
                    messages.remove(0);
                }
            }else{
                messages.add("EMPTY STRING!!");
                if (messages.size() >= 8){
                    messages.remove(0);
                }
            }
        }
    } catch (SocketTimeoutException e){
        // Timed out
    } catch (Exception e) {
        System.err.println("Connection lost");
        break;
    }

(Quick Edit: Should probably mention this is in a while loop inside the run method since I implemented Runnable on my class.)

When using input.readLine(), should I be checking something before to make sure it doesn't come out as just empty strings? Is it because of the timeout I have put on it?

Thanks in advance, and I should also mention I'm new to stackoverflow, If I'm doing anything wrong please say! :)

Okay, Well. Turns out it was to do with the timeout being too small. I put it up to a whole second and now it prints out fine. Thanks anyway people :)

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