我如何知道客户是否收到或没有

时间:2018-03-31 13:19:23

标签: java web-services sockets serversocket java-websocket

我有这个客户端代码

class CCLI {


public static void main(String args[]) throws Exception
      {
          CCLI ccli = new CCLI() ;
      BufferedReader inFromUser =
      new BufferedReader(new InputStreamReader(System.in));

      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("localhost");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      String sentence = inFromUser.readLine();
      sentence = ccli.reversed(sentence) ;

      sendData = sentence.getBytes();
      DatagramPacket sendPacket =
      new DatagramPacket(sendData, sendData.length, IPAddress, 8888);
      clientSocket.send(sendPacket);

      DatagramPacket receivePacket =
      new DatagramPacket(receiveData, receiveData.length);


      clientSocket.receive(receivePacket);

      Thread.sleep(30);

      String modifiedSentence = new String(receivePacket.getData());



      if (modifiedSentence.contains(" ")){

      System.out.println("Lost ");
      }

      else
      {

      System.out.println("FROM SERVER:" + modifiedSentence);

      }

      }
      }

我想知道我是如何检查客户端是否接收到某些东西或者没有,来自服务器,因为我想要但是如果客户端没有收到任何东西它意味着PKT丢失,则计时器为30ms!

0 个答案:

没有答案