Socket with Timertask

3

I want to ask for serverSocket a response (some information) every 5 seconds.

I set up my socket and I'm using a TimerTask to execute a method every 5 seconds, but only the first execution is successful.

Attempt one

Call the complete method and within this method the socket is created and closed at the end of it.

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 on first run, all other runs that occur every 5 seconds have no return. The local port is changing. Example: 55147, then 55148 and so on.

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 to use another approach, I thought I would pass a Socket object to the method, so I would not have to create and close it, I would use the same to get the value every 5 seconds, but I get the following error from the second and I'm not using "client.close ()" since I want 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)

Observations

  • The server keeps running, everything I tried failed, the idea is to send bytes to it, and it returns me other bytes. The first approach tries to create new socket s and the second one uses the same socket . I'm trying to communicate with a time interval.

  • I tried to explain my problem and need that I have. I believe I'm on my way, but something is missing.

  • asked by anonymous 10.04.2014 / 22:43

    1 answer

    1

    Although you did not have your server code available, the description of your problem seems to imply that it is not ready to receive connections in sequence. That is, whenever your client closes the first socket, ServerSocket is closed and from the second connection there is no-one waiting for new connections.

    If this is the case, a simple solution is to do something like this:

    ServerSocket serveridor = new ServerSocket(porta);
    while(true) {
        Socket cliente = servidor.accept();
        //iniciar_uma_thread_pra_cuidar_deste(cliente)
    }
    
        
    11.04.2014 / 02:00