TCP communication
Diff: TCP_COMM.cpp
- Revision:
- 1:174bf74eba86
- Parent:
- 0:6e800fc6935f
--- a/TCP_COMM.cpp Sun Apr 03 15:44:37 2016 +0000 +++ b/TCP_COMM.cpp Mon Apr 04 22:04:28 2016 +0000 @@ -1,17 +1,17 @@ #include "TCP_COMM.h" - TCP_Communication::TCP_Communication() - :threadSocket(&TCP_Communication::threadReceiverStarter, this, osPriorityHigh,1024) + :threadSocket(&TCP_Communication::threadServerStarter, this, osPriorityNormal,1024) { + wait(5); threadSocket.signal_set(0x01); } TCP_Communication::TCP_Communication(string _addr, int _port) - :threadSocket(&TCP_Communication::threadReceiverStarter, this, osPriorityHigh,1024) + :threadSocket(&TCP_Communication::threadReceiverStarter, this, osPriorityNormal,1024) { address = _addr.c_str(); port = _port; - + initSocket(); threadSocket.signal_set(0x02); } @@ -31,10 +31,13 @@ void TCP_Communication::threadServerWorker() { + printf("Attente du signal\n"); threadSocket.signal_wait(0x01); + printf("Init Server\n"); TCPSocketServer server; server.bind(SERVER_PORT); + printf("Server listen\n"); server.listen(); while(1) @@ -67,23 +70,27 @@ printf("Receiver Worker thread \n"); char message[256]; int messageLength; + printf("Wait to receive data :\n"); messageLength = socket.receive(message, 256); if(messageLength <= 0) { - printf("This thread yield \n"); - Thread::yield(); + printf("No message \n"); + Thread::wait(500); continue; } message[messageLength] = '\0'; // String end type - printf("Received message from server: '%s' %i \n", message, messageLength); + printf("Received message from server: '%s' %i \n", message, messageLength); + Thread::wait(500); } - closeSocketConnection(); } + void TCP_Communication::initSocket() { - while (socket.connect(address, port) < 0) { + int compteur = 0; + while (!socket.connect(address, port) && compteur < 0) { printf("Unable to connect to (%s) on port (%d)\n", address, port); + ++compteur; wait(1); } printf("Connected to Server at %s\n",address); @@ -95,6 +102,7 @@ socket.send_all(data, strlen(data)); } + void TCP_Communication::closeSocketConnection() { socket.close();