TCP communication
TCP_COMM.cpp@0:6e800fc6935f, 2016-04-03 (annotated)
- Committer:
- 13075593
- Date:
- Sun Apr 03 15:44:37 2016 +0000
- Revision:
- 0:6e800fc6935f
- Child:
- 1:174bf74eba86
TCP communication
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
13075593 | 0:6e800fc6935f | 1 | #include "TCP_COMM.h" |
13075593 | 0:6e800fc6935f | 2 | |
13075593 | 0:6e800fc6935f | 3 | TCP_Communication::TCP_Communication() |
13075593 | 0:6e800fc6935f | 4 | :threadSocket(&TCP_Communication::threadReceiverStarter, this, osPriorityHigh,1024) |
13075593 | 0:6e800fc6935f | 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 | 0:6e800fc6935f | 10 | :threadSocket(&TCP_Communication::threadReceiverStarter, this, osPriorityHigh,1024) |
13075593 | 0:6e800fc6935f | 11 | { |
13075593 | 0:6e800fc6935f | 12 | address = _addr.c_str(); |
13075593 | 0:6e800fc6935f | 13 | port = _port; |
13075593 | 0:6e800fc6935f | 14 | |
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 | 0:6e800fc6935f | 34 | threadSocket.signal_wait(0x01); |
13075593 | 0:6e800fc6935f | 35 | |
13075593 | 0:6e800fc6935f | 36 | TCPSocketServer server; |
13075593 | 0:6e800fc6935f | 37 | server.bind(SERVER_PORT); |
13075593 | 0:6e800fc6935f | 38 | server.listen(); |
13075593 | 0:6e800fc6935f | 39 | |
13075593 | 0:6e800fc6935f | 40 | while(1) |
13075593 | 0:6e800fc6935f | 41 | { |
13075593 | 0:6e800fc6935f | 42 | printf("Wait for new connection...\n"); |
13075593 | 0:6e800fc6935f | 43 | TCPSocketConnection client; |
13075593 | 0:6e800fc6935f | 44 | server.accept(client); |
13075593 | 0:6e800fc6935f | 45 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
13075593 | 0:6e800fc6935f | 46 | |
13075593 | 0:6e800fc6935f | 47 | printf("Connection from: %s\n", client.get_address()); |
13075593 | 0:6e800fc6935f | 48 | char dataBuffer[256]; |
13075593 | 0:6e800fc6935f | 49 | while (true) { |
13075593 | 0:6e800fc6935f | 50 | int lengthData = client.receive(dataBuffer, sizeof(dataBuffer)); |
13075593 | 0:6e800fc6935f | 51 | if (lengthData <= 0) break; |
13075593 | 0:6e800fc6935f | 52 | |
13075593 | 0:6e800fc6935f | 53 | // print received message to terminal |
13075593 | 0:6e800fc6935f | 54 | dataBuffer[lengthData] = '\0'; |
13075593 | 0:6e800fc6935f | 55 | printf("Received message from Client :'%s'\n",dataBuffer); |
13075593 | 0:6e800fc6935f | 56 | } |
13075593 | 0:6e800fc6935f | 57 | client.close(); |
13075593 | 0:6e800fc6935f | 58 | } |
13075593 | 0:6e800fc6935f | 59 | } |
13075593 | 0:6e800fc6935f | 60 | |
13075593 | 0:6e800fc6935f | 61 | void TCP_Communication::threadReceiverWorker() |
13075593 | 0:6e800fc6935f | 62 | { |
13075593 | 0:6e800fc6935f | 63 | threadSocket.signal_wait(0x02); |
13075593 | 0:6e800fc6935f | 64 | |
13075593 | 0:6e800fc6935f | 65 | while(1) |
13075593 | 0:6e800fc6935f | 66 | { |
13075593 | 0:6e800fc6935f | 67 | printf("Receiver Worker thread \n"); |
13075593 | 0:6e800fc6935f | 68 | char message[256]; |
13075593 | 0:6e800fc6935f | 69 | int messageLength; |
13075593 | 0:6e800fc6935f | 70 | messageLength = socket.receive(message, 256); |
13075593 | 0:6e800fc6935f | 71 | if(messageLength <= 0) |
13075593 | 0:6e800fc6935f | 72 | { |
13075593 | 0:6e800fc6935f | 73 | printf("This thread yield \n"); |
13075593 | 0:6e800fc6935f | 74 | Thread::yield(); |
13075593 | 0:6e800fc6935f | 75 | continue; |
13075593 | 0:6e800fc6935f | 76 | } |
13075593 | 0:6e800fc6935f | 77 | message[messageLength] = '\0'; // String end type |
13075593 | 0:6e800fc6935f | 78 | printf("Received message from server: '%s' %i \n", message, messageLength); |
13075593 | 0:6e800fc6935f | 79 | } |
13075593 | 0:6e800fc6935f | 80 | closeSocketConnection(); |
13075593 | 0:6e800fc6935f | 81 | } |
13075593 | 0:6e800fc6935f | 82 | |
13075593 | 0:6e800fc6935f | 83 | void TCP_Communication::initSocket() |
13075593 | 0:6e800fc6935f | 84 | { |
13075593 | 0:6e800fc6935f | 85 | while (socket.connect(address, port) < 0) { |
13075593 | 0:6e800fc6935f | 86 | printf("Unable to connect to (%s) on port (%d)\n", address, port); |
13075593 | 0:6e800fc6935f | 87 | wait(1); |
13075593 | 0:6e800fc6935f | 88 | } |
13075593 | 0:6e800fc6935f | 89 | printf("Connected to Server at %s\n",address); |
13075593 | 0:6e800fc6935f | 90 | } |
13075593 | 0:6e800fc6935f | 91 | |
13075593 | 0:6e800fc6935f | 92 | void TCP_Communication::sendDataSocket(char *data) |
13075593 | 0:6e800fc6935f | 93 | { |
13075593 | 0:6e800fc6935f | 94 | printf("Sending message data to Server : '%s' length %i \n",data, strlen(data)); |
13075593 | 0:6e800fc6935f | 95 | socket.send_all(data, strlen(data)); |
13075593 | 0:6e800fc6935f | 96 | } |
13075593 | 0:6e800fc6935f | 97 | |
13075593 | 0:6e800fc6935f | 98 | void TCP_Communication::closeSocketConnection() |
13075593 | 0:6e800fc6935f | 99 | { |
13075593 | 0:6e800fc6935f | 100 | socket.close(); |
13075593 | 0:6e800fc6935f | 101 | } |