Question:
I want to request a response from serverSocket
(some information) every 5 seconds.
I TimerTask
my socket
and am 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 a return on the first execution, all other executions 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 about passing a Socket
object to the method, so I wouldn't 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'm not using it "client.close()" since I intend to always use the same Socket object.
Mistake
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)
Comments
-
The server is running, everything I tried failed, the idea is to send bytes to it, and it returns other bytes to me. The first approach tries to create new
socket
second uses the samesocket
. I am trying to communicate with an interval of time. -
I tried to explain my problem and need I have. I believe I'm on the way, but something's missing.
Answer:
Even though you haven't made your server code available, your problem description seems to imply that it's not ready to receive streaming connections. In other words, whenever your client closes the first Socket, the ServerSocket is closed and from the second connection onwards there is "nobody" 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)
}