TCP communication

Dependents:   Ethernet

Files at this revision

API Documentation at this revision

Comitter:
13075593
Date:
Mon Apr 04 22:04:28 2016 +0000
Parent:
0:6e800fc6935f
Commit message:
Communication TCP/IP

Changed in this revision

TCP_COMM.cpp Show annotated file Show diff for this revision Revisions of this file
TCP_COMM.h Show annotated file Show diff for this revision Revisions of this file
--- 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();
--- a/TCP_COMM.h	Sun Apr 03 15:44:37 2016 +0000
+++ b/TCP_COMM.h	Mon Apr 04 22:04:28 2016 +0000
@@ -1,6 +1,6 @@
 #ifndef TCP_COMM_H
 #define TCP_COMM_H
-
+#include "rtos.h"
 #include "mbed.h"
 #include "ConfigEthernet.h"
 #include <string>
@@ -10,7 +10,7 @@
 class TCP_Communication
 {
     public :
-        TCP_Communication(); // Établir un Thread Server pour recevoir des connexions sur MBED
+        TCP_Communication();
         TCP_Communication(string adrr, int port); // Établir une connexion Socket à l'adresse et le port en paramètre 
         void initSocket();
         void sendDataSocket(char *data);
@@ -28,6 +28,7 @@
         TCPSocketConnection socket;
         
         Thread threadSocket;
+        
         char *url;
         const char *address;
         int port;