简体   繁体   English

侦听服务器发送尝试的正确方法

[英]the proper way to listen for server sending attempt

what is the proper way to determine that the server is sending data at the moment for example 例如,确定当前服务器正在发送数据的正确方法是什么?
Pseudo-code 伪代码

while(true){
  //Do something
  if(ServerIsSendingrightnow){
    //Get The Data
    //Calling some method to handling the server's data
  }
  //Do something else
}

does the method available() of InputStream class do the job ? InputStream类的available()方法能完成这项工作吗?
Code: 码:

 while(true){
   //Do something
   InputStream IStreamsock = Socket1.getInputStream();
   if(IStreamsock.available()){ //the server is sending data right now !
     //Get The Data
     //Calling some method to handling the server's data}
     //Do something else
}

In C# we have MemoryStream class that serve as Dynamic byte array 在C#中,我们有MemoryStream类用作动态字节数组
is there any java equivalent for MemoryStream 是否有任何Java等价于MemoryStream
can I do something like this in java: Pseudo-code 我可以在Java中做类似的事情吗?伪代码

 while(DataIsAvailableInSocketInputStreamBuffer){
   MemoryStreamEquivalent.WriteByte(IStreamsock.ReadByte())}

I'm Sorry, but I'm new comer in java 我很抱歉,但是我是Java的新手

Nope, the usage of available is not very useful, because it does not work as you would expect it. 不,可用的用法不是很有用,因为它无法正常工作。 Just use in.read(). 只需使用in.read()。 It will wait until something is sended by the server. 它将等待,直到服务器发送了一些东西。 So if you use it in a thread, it just waits until something can be recieved. 因此,如果您在线程中使用它,它只会等到可以接收到某些内容为止。

Edit: It recieves just one Byte, so eg a BufferedReader (to read Strings) is a better solution, or maybe an ObjectInputReader (for Objects, obviously). 编辑:它仅接收一个字节,因此例如BufferedReader(读取字符串)是更好的解决方案,或者ObjectInputReader(显然是对象)。 And of course the while(true) is needed :) 当然需要while(true):)

Example: 例:

Socket s = new Socket(...); // connect to server
BufferedReader br = new BufferedReader(s.getInputStream()); // creating a bufferedReader around the inputstream. If you're dealing with binary data, you shouldn't create a (Buffered)Reader
while (String line = br.readLine()) {
  //do something here
}

So here is an answer how you could do this: (I wrote a complete example run() method of the client thread 因此,这是您如何执行此操作的答案:(我编写了客户端线程的完整示例run()方法

@Override
public void run() {
   while(client.isConnected()) {  //client.isConnected should be a method of your client class
      Object inputData = in.read(); //you should use a proper Object type here, if you
                                    //use InputStreamReader, it would be Byte and if you
                                    //use BufferedReader it would be String
      doCrazyStuff(inputData);      //just an example of manipulating data, do your own stuff here
   }
}

Here an example with BufferedReader (I will not change the encoding or something, because I think this is just a training application) 这是BufferedReader的示例(我不会更改编码或其他内容,因为我认为这只是一个培训应用程序)

public void run() {
   while(client.isConnected()) {  //client.isConnected should be a method of your client class
      while(!in.ready()) { }        //here you CAN use the method ready, that is boolean

      String inputData = in.readLine();
      doCrazyStuff(inputData);      //just an example of manipulating data, do your own stuff here
   }
}

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

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