TCP communication

Dependents:   Ethernet

Committer:
13075593
Date:
Mon Apr 04 22:04:28 2016 +0000
Revision:
1:174bf74eba86
Parent:
0:6e800fc6935f
Communication TCP/IP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
13075593 0:6e800fc6935f 1 #include "TCP_COMM.h"
13075593 0:6e800fc6935f 2 TCP_Communication::TCP_Communication()
13075593 1:174bf74eba86 3 :threadSocket(&TCP_Communication::threadServerStarter, this, osPriorityNormal,1024)
13075593 0:6e800fc6935f 4 {
13075593 1:174bf74eba86 5 wait(5);
13075593 0:6e800fc6935f 6 threadSocket.signal_set(0x01);
13075593 0:6e800fc6935f 7 }
13075593 0:6e800fc6935f 8
13075593 0:6e800fc6935f 9 TCP_Communication::TCP_Communication(string _addr, int _port)
13075593 1:174bf74eba86 10 :threadSocket(&TCP_Communication::threadReceiverStarter, this, osPriorityNormal,1024)
13075593 0:6e800fc6935f 11 {
13075593 0:6e800fc6935f 12 address = _addr.c_str();
13075593 0:6e800fc6935f 13 port = _port;
13075593 1:174bf74eba86 14 initSocket();
13075593 0:6e800fc6935f 15 threadSocket.signal_set(0x02);
13075593 0:6e800fc6935f 16 }
13075593 0:6e800fc6935f 17
13075593 0:6e800fc6935f 18 void TCP_Communication::threadServerStarter(const void *args)
13075593 0:6e800fc6935f 19 {
13075593 0:6e800fc6935f 20 printf("Thread Server Starter \n");
13075593 0:6e800fc6935f 21 TCP_Communication *instance = (TCP_Communication*)args;
13075593 0:6e800fc6935f 22 instance->threadServerWorker();
13075593 0:6e800fc6935f 23 }
13075593 0:6e800fc6935f 24
13075593 0:6e800fc6935f 25 void TCP_Communication::threadReceiverStarter(void const *args)
13075593 0:6e800fc6935f 26 {
13075593 0:6e800fc6935f 27 printf("Thread Receiver Starter \n");
13075593 0:6e800fc6935f 28 TCP_Communication *instance = (TCP_Communication*)args;
13075593 0:6e800fc6935f 29 instance->threadReceiverWorker();
13075593 0:6e800fc6935f 30 }
13075593 0:6e800fc6935f 31
13075593 0:6e800fc6935f 32 void TCP_Communication::threadServerWorker()
13075593 0:6e800fc6935f 33 {
13075593 1:174bf74eba86 34 printf("Attente du signal\n");
13075593 0:6e800fc6935f 35 threadSocket.signal_wait(0x01);
13075593 0:6e800fc6935f 36
13075593 1:174bf74eba86 37 printf("Init Server\n");
13075593 0:6e800fc6935f 38 TCPSocketServer server;
13075593 0:6e800fc6935f 39 server.bind(SERVER_PORT);
13075593 1:174bf74eba86 40 printf("Server listen\n");
13075593 0:6e800fc6935f 41 server.listen();
13075593 0:6e800fc6935f 42
13075593 0:6e800fc6935f 43 while(1)
13075593 0:6e800fc6935f 44 {
13075593 0:6e800fc6935f 45 printf("Wait for new connection...\n");
13075593 0:6e800fc6935f 46 TCPSocketConnection client;
13075593 0:6e800fc6935f 47 server.accept(client);
13075593 0:6e800fc6935f 48 client.set_blocking(false, 1500); // Timeout after (1.5)s
13075593 0:6e800fc6935f 49
13075593 0:6e800fc6935f 50 printf("Connection from: %s\n", client.get_address());
13075593 0:6e800fc6935f 51 char dataBuffer[256];
13075593 0:6e800fc6935f 52 while (true) {
13075593 0:6e800fc6935f 53 int lengthData = client.receive(dataBuffer, sizeof(dataBuffer));
13075593 0:6e800fc6935f 54 if (lengthData <= 0) break;
13075593 0:6e800fc6935f 55
13075593 0:6e800fc6935f 56 // print received message to terminal
13075593 0:6e800fc6935f 57 dataBuffer[lengthData] = '\0';
13075593 0:6e800fc6935f 58 printf("Received message from Client :'%s'\n",dataBuffer);
13075593 0:6e800fc6935f 59 }
13075593 0:6e800fc6935f 60 client.close();
13075593 0:6e800fc6935f 61 }
13075593 0:6e800fc6935f 62 }
13075593 0:6e800fc6935f 63
13075593 0:6e800fc6935f 64 void TCP_Communication::threadReceiverWorker()
13075593 0:6e800fc6935f 65 {
13075593 0:6e800fc6935f 66 threadSocket.signal_wait(0x02);
13075593 0:6e800fc6935f 67
13075593 0:6e800fc6935f 68 while(1)
13075593 0:6e800fc6935f 69 {
13075593 0:6e800fc6935f 70 printf("Receiver Worker thread \n");
13075593 0:6e800fc6935f 71 char message[256];
13075593 0:6e800fc6935f 72 int messageLength;
13075593 1:174bf74eba86 73 printf("Wait to receive data :\n");
13075593 0:6e800fc6935f 74 messageLength = socket.receive(message, 256);
13075593 0:6e800fc6935f 75 if(messageLength <= 0)
13075593 0:6e800fc6935f 76 {
13075593 1:174bf74eba86 77 printf("No message \n");
13075593 1:174bf74eba86 78 Thread::wait(500);
13075593 0:6e800fc6935f 79 continue;
13075593 0:6e800fc6935f 80 }
13075593 0:6e800fc6935f 81 message[messageLength] = '\0'; // String end type
13075593 1:174bf74eba86 82 printf("Received message from server: '%s' %i \n", message, messageLength);
13075593 1:174bf74eba86 83 Thread::wait(500);
13075593 0:6e800fc6935f 84 }
13075593 0:6e800fc6935f 85 }
13075593 0:6e800fc6935f 86
13075593 1:174bf74eba86 87
13075593 0:6e800fc6935f 88 void TCP_Communication::initSocket()
13075593 0:6e800fc6935f 89 {
13075593 1:174bf74eba86 90 int compteur = 0;
13075593 1:174bf74eba86 91 while (!socket.connect(address, port) && compteur < 0) {
13075593 0:6e800fc6935f 92 printf("Unable to connect to (%s) on port (%d)\n", address, port);
13075593 1:174bf74eba86 93 ++compteur;
13075593 0:6e800fc6935f 94 wait(1);
13075593 0:6e800fc6935f 95 }
13075593 0:6e800fc6935f 96 printf("Connected to Server at %s\n",address);
13075593 0:6e800fc6935f 97 }
13075593 0:6e800fc6935f 98
13075593 0:6e800fc6935f 99 void TCP_Communication::sendDataSocket(char *data)
13075593 0:6e800fc6935f 100 {
13075593 0:6e800fc6935f 101 printf("Sending message data to Server : '%s' length %i \n",data, strlen(data));
13075593 0:6e800fc6935f 102 socket.send_all(data, strlen(data));
13075593 0:6e800fc6935f 103 }
13075593 0:6e800fc6935f 104
13075593 1:174bf74eba86 105
13075593 0:6e800fc6935f 106 void TCP_Communication::closeSocketConnection()
13075593 0:6e800fc6935f 107 {
13075593 0:6e800fc6935f 108 socket.close();
13075593 0:6e800fc6935f 109 }