3
I want to request the serverSocket a response (some information) every 5 seconds.
I rode my socket and I’m wearing a TimerTask to execute a method every 5 seconds, but only the first execution is successful.
Attempt a
Call the complete method and within this method the socket is created and closed at the completion of the same.
Timertask code
public void run() {
   SocketeClient() mCommand = new SocketClient();
   mCommand.runClient();
}
Socket code
public void runClient() {
   System.out.println("---------------------");
   try {
      client = new Socket("localhost", 9999);
      input = new DataInputStream(client.getInputStream());
      output = new DataOutputStream(client.getOutputStream());
      byte request[] = new byte[18];
      output.write(request, 0, 17);
      int totalBytes = input.available();
      byte response[] = new byte[totalBytes];
      boolean continuar = true;
      int bytesLer = 0;
      while (continuar && bytesLer < totalBytes) {
         try {
            response[bytesLer] = input.readByte();
            bytesRead++;
         } catch (IOException e) {
            System.out.println(e.getMessage());
            reading = false;
         }
      }
      System.out.println(response[0] + response[1]);
      input.close();
      output.close();
      socket.close();
   } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
   System.out.println("---------------------");
}
In this example, there is only return in the first execution, all other executions that occur every 5 seconds have no return. The local door is changing. Example: 55147, then 55148 and successively.
Eclipse console:
----------------------------------------------------
Cliente: Socket[addr=localhost/127.0.0.1,port=9999,localport=52206]
05,45
-----------------------------------------------------
----------------------------------------------------
Cliente: Socket[addr=localhost/127.0.0.1,port=9999,localport=52206]
Infelizmente não houve resposta do servidor.
-----------------------------------------------------
----------------------------------------------------
Cliente: Socket[addr=localhost/127.0.0.1,port=9999,localport=52206]
Infelizmente não houve resposta do servidor.
-----------------------------------------------------
Attempt two
I tried another approach, thought I’d pass an object Socket for the method, so I would not have to keep creating and closing, I would use it to receive the value every 5 seconds, but I get the following error from the second execution and I am not using "client.close()" since I intend to always use the same Socket object.
Error
java.net.SocketException: Socket is closed
    at java.net.Socket.getInputStream(Unknown Source)
    at com.iamExport.model.MessageCommands.apiGetAudienceStatus(MessageCommands.java:29)
    at com.iamExport.model.ConnectionSocket.Connectar(ConnectionSocket.java:36)
    at com.iamExport.model.ConnectionSocket.run(ConnectionSocket.java:26)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)
Remarks
- The server keeps running, everything I tried failed, the idea is to send bytes to it, and the same return me other bytes. The first approach tries to create new - sockets and the second use the same- socket. I’m trying to communicate with an interval of time.
- I tried to explain my problem and need that I have. I believe I am in the way, but something is missing.