简体   繁体   English

Java-客户端从服务器接收空白消息,但仅通过网络

[英]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. 客户端可以很好地连接到服务器,发送给客户端的第一条消息应该是“ Server:Hello,World!”,但是会出现一个空字符串。 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. 服务器使用自动刷新功能通过PrintStream发送信息。

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. 服务器每500毫秒发送一行文本,计数到每个客户端。 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. 客户应接收到消息并将其打印到屏幕上,并在下面的5,10,15和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.) (快速编辑:自从我在类上实现Runnable以来,应该提到这是在run方法内的while循环中。)

When using input.readLine(), should I be checking something before to make sure it doesn't come out as just empty strings? 使用input.readLine()时,我是否应该先检查一下内容以确保它不会只是空字符串而出现? 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! 在此先感谢您,我还要提一下我是stackoverflow的新手,如果我做错了什么,请说! :) :)

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 :) 无论如何,谢谢大家:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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