First version
Dependencies: mbed EthernetInterface mbed-rto
TCPlistener.cpp@12:f3db7045e220, 2018-04-18 (annotated)
- Committer:
- KlaasGovaerts
- Date:
- Wed Apr 18 07:41:38 2018 +0000
- Revision:
- 12:f3db7045e220
- Parent:
- 4:0413d42377d1
- Child:
- 13:a6c651a1e51c
Versie 18/04
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KlaasGovaerts | 4:0413d42377d1 | 1 | #include "TCPlistener.h" |
KlaasGovaerts | 4:0413d42377d1 | 2 | |
KlaasGovaerts | 4:0413d42377d1 | 3 | TCPlistener::TCPlistener(){ |
KlaasGovaerts | 4:0413d42377d1 | 4 | char ip[]="192.168.0.253"; |
KlaasGovaerts | 4:0413d42377d1 | 5 | char mask[]="255.255.255.0"; |
KlaasGovaerts | 4:0413d42377d1 | 6 | char gateway[]="192.168.0.254"; |
KlaasGovaerts | 4:0413d42377d1 | 7 | eth.init(ip,mask,gateway); |
KlaasGovaerts | 4:0413d42377d1 | 8 | eth.connect(); |
KlaasGovaerts | 4:0413d42377d1 | 9 | server.bind(4000); |
KlaasGovaerts | 12:f3db7045e220 | 10 | server.set_blocking(false); |
KlaasGovaerts | 12:f3db7045e220 | 11 | for(int i=0;i<10;i++){ |
KlaasGovaerts | 12:f3db7045e220 | 12 | connections[i]=0; |
KlaasGovaerts | 12:f3db7045e220 | 13 | } |
KlaasGovaerts | 4:0413d42377d1 | 14 | } |
KlaasGovaerts | 12:f3db7045e220 | 15 | void TCPlistener::listen(){ |
KlaasGovaerts | 12:f3db7045e220 | 16 | server.listen(10);//Accept 10 connections at a time. |
KlaasGovaerts | 12:f3db7045e220 | 17 | //Source:https://os.mbed.com/handbook/Socket |
KlaasGovaerts | 12:f3db7045e220 | 18 | |
KlaasGovaerts | 12:f3db7045e220 | 19 | bool searchLocation=true; |
KlaasGovaerts | 12:f3db7045e220 | 20 | for(int i=0;i<10&&searchLocation;i++){ |
KlaasGovaerts | 12:f3db7045e220 | 21 | if(connections[i]!=0){ |
KlaasGovaerts | 12:f3db7045e220 | 22 | connections[i]=new TCPSocketConnection(); |
KlaasGovaerts | 12:f3db7045e220 | 23 | |
KlaasGovaerts | 12:f3db7045e220 | 24 | searchLocation=false; |
KlaasGovaerts | 12:f3db7045e220 | 25 | } |
KlaasGovaerts | 12:f3db7045e220 | 26 | } |
KlaasGovaerts | 12:f3db7045e220 | 27 | |
KlaasGovaerts | 12:f3db7045e220 | 28 | bool incomingConnection=true; |
KlaasGovaerts | 12:f3db7045e220 | 29 | while(incomingConnection){ |
KlaasGovaerts | 12:f3db7045e220 | 30 | server.accept(client); |
KlaasGovaerts | 12:f3db7045e220 | 31 | } |
KlaasGovaerts | 12:f3db7045e220 | 32 | //client.set_blocking(false, 100); |
KlaasGovaerts | 12:f3db7045e220 | 33 | |
KlaasGovaerts | 12:f3db7045e220 | 34 | |
KlaasGovaerts | 12:f3db7045e220 | 35 | printf("Connection from: %s\n", client.get_address()); |
KlaasGovaerts | 12:f3db7045e220 | 36 | |
KlaasGovaerts | 12:f3db7045e220 | 37 | |
KlaasGovaerts | 12:f3db7045e220 | 38 | } |
KlaasGovaerts | 12:f3db7045e220 | 39 | |
KlaasGovaerts | 4:0413d42377d1 | 40 | void TCPlistener::receivePacket(char *contents,int size){ |
KlaasGovaerts | 12:f3db7045e220 | 41 | int n = server.receive(contents, size); |
KlaasGovaerts | 4:0413d42377d1 | 42 | contents[n] = '\0'; |
KlaasGovaerts | 4:0413d42377d1 | 43 | printf("Segment ontvangen van %s:%i met inhoud \"%s\"\r\n", client.get_address(),client.get_port(),contents); |
KlaasGovaerts | 12:f3db7045e220 | 44 | } |
KlaasGovaerts | 12:f3db7045e220 | 45 | |
KlaasGovaerts | 12:f3db7045e220 | 46 | void TCPlistener::firstEmptyLocation(){ |
KlaasGovaerts | 12:f3db7045e220 | 47 | |
KlaasGovaerts | 4:0413d42377d1 | 48 | } |