demo versie 16/05
Dependencies: EthernetInterface mbed-rto mbed
Fork of ProjectVLC by
TCPlistener.cpp@41:b39239c02c95, 2018-05-02 (annotated)
- Committer:
- KlaasGovaerts
- Date:
- Wed May 02 11:22:09 2018 +0000
- Revision:
- 41:b39239c02c95
- Parent:
- 37:4589f962697d
Meerdere threads (werkt niet)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KlaasGovaerts | 5:0413d42377d1 | 1 | #include "TCPlistener.h" |
KlaasGovaerts | 5:0413d42377d1 | 2 | |
KlaasGovaerts | 35:efdbfccf2678 | 3 | /** |
KlaasGovaerts | 35:efdbfccf2678 | 4 | * Initialise with ip address of 192.168.0.253, a mask of 255.255.255.0 and a gateway of 192.168.0.254. |
KlaasGovaerts | 35:efdbfccf2678 | 5 | * Starts a TCP server which will listen on port 4000. |
KlaasGovaerts | 35:efdbfccf2678 | 6 | */ |
KlaasGovaerts | 5:0413d42377d1 | 7 | TCPlistener::TCPlistener(){ |
KlaasGovaerts | 31:915f6cb7ffa5 | 8 | char ip[]="192.168.0.253"; |
KlaasGovaerts | 31:915f6cb7ffa5 | 9 | char mask[]="255.255.255.0"; |
KlaasGovaerts | 31:915f6cb7ffa5 | 10 | char gateway[]="192.168.0.254"; |
KlaasGovaerts | 5:0413d42377d1 | 11 | eth.init(ip,mask,gateway); |
KlaasGovaerts | 5:0413d42377d1 | 12 | eth.connect(); |
KlaasGovaerts | 5:0413d42377d1 | 13 | server.bind(4000); |
KlaasGovaerts | 16:ffd311730575 | 14 | server.listen(); |
KlaasGovaerts | 37:4589f962697d | 15 | server.set_blocking(true); |
KlaasGovaerts | 36:aa6c6c177be2 | 16 | //arraySize=10; |
KlaasGovaerts | 5:0413d42377d1 | 17 | } |
KlaasGovaerts | 13:f3db7045e220 | 18 | |
KlaasGovaerts | 35:efdbfccf2678 | 19 | /** |
KlaasGovaerts | 41:b39239c02c95 | 20 | * Receives all packets sent to 192.168.0.253:4000. The receive is non blocking. |
KlaasGovaerts | 35:efdbfccf2678 | 21 | * @param contents The location where the segment contents will be written, formatted as null terminated character array. |
KlaasGovaerts | 35:efdbfccf2678 | 22 | * @param size The max size of the "contents" array. |
KlaasGovaerts | 35:efdbfccf2678 | 23 | * @return True if a segment was received, false if no segment was received. |
KlaasGovaerts | 35:efdbfccf2678 | 24 | */ |
KlaasGovaerts | 16:ffd311730575 | 25 | bool TCPlistener::receiveSegment(char *contents,int size){ |
KlaasGovaerts | 20:4997b02d6a88 | 26 | printf("Maak een connectie.\r\n"); |
KlaasGovaerts | 20:4997b02d6a88 | 27 | TCPSocketConnection client; |
KlaasGovaerts | 20:4997b02d6a88 | 28 | server.accept(client); |
albireo987 | 32:537005b4a065 | 29 | int n=client.receive(contents,size); |
KlaasGovaerts | 37:4589f962697d | 30 | if(n!=-1){ |
KlaasGovaerts | 37:4589f962697d | 31 | contents[n] = '\0'; |
KlaasGovaerts | 37:4589f962697d | 32 | printf("Segment ontvangen van %s:%i met inhoud \"%s\"\r\n", client.get_address(),client.get_port(),contents); |
KlaasGovaerts | 37:4589f962697d | 33 | return true; |
KlaasGovaerts | 37:4589f962697d | 34 | } |
KlaasGovaerts | 16:ffd311730575 | 35 | return false; |
KlaasGovaerts | 19:5ee34e60a31d | 36 | } |