![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
demo versie 16/05
Dependencies: EthernetInterface mbed-rto mbed
Fork of ProjectVLC by
TCPlistener.cpp@16:ffd311730575, 2018-04-18 (annotated)
- Committer:
- KlaasGovaerts
- Date:
- Wed Apr 18 09:17:14 2018 +0000
- Revision:
- 16:ffd311730575
- Parent:
- 15:5f1fda6b9140
- Child:
- 19:5ee34e60a31d
Versie 18/04 (4)
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 | 5:0413d42377d1 | 3 | TCPlistener::TCPlistener(){ |
KlaasGovaerts | 5:0413d42377d1 | 4 | char ip[]="192.168.0.253"; |
KlaasGovaerts | 5:0413d42377d1 | 5 | char mask[]="255.255.255.0"; |
KlaasGovaerts | 5:0413d42377d1 | 6 | char gateway[]="192.168.0.254"; |
KlaasGovaerts | 5:0413d42377d1 | 7 | eth.init(ip,mask,gateway); |
KlaasGovaerts | 5:0413d42377d1 | 8 | eth.connect(); |
KlaasGovaerts | 5:0413d42377d1 | 9 | server.bind(4000); |
KlaasGovaerts | 16:ffd311730575 | 10 | server.listen(); |
KlaasGovaerts | 13:f3db7045e220 | 11 | server.set_blocking(false); |
KlaasGovaerts | 13:f3db7045e220 | 12 | for(int i=0;i<10;i++){ |
KlaasGovaerts | 15:5f1fda6b9140 | 13 | connections[i]=0; |
KlaasGovaerts | 13:f3db7045e220 | 14 | } |
KlaasGovaerts | 15:5f1fda6b9140 | 15 | arraySize=10; |
KlaasGovaerts | 5:0413d42377d1 | 16 | } |
KlaasGovaerts | 13:f3db7045e220 | 17 | void TCPlistener::listen(){ |
KlaasGovaerts | 15:5f1fda6b9140 | 18 | //server.listen(10);//Accept 10 connections at a time. |
KlaasGovaerts | 13:f3db7045e220 | 19 | //Source:https://os.mbed.com/handbook/Socket |
KlaasGovaerts | 13:f3db7045e220 | 20 | bool incomingConnection=true; |
KlaasGovaerts | 13:f3db7045e220 | 21 | while(incomingConnection){ |
KlaasGovaerts | 16:ffd311730575 | 22 | TCPSocketConnection* connection=newConnection(); |
KlaasGovaerts | 16:ffd311730575 | 23 | if(connection!=0){ |
KlaasGovaerts | 16:ffd311730575 | 24 | if(server.accept(*connection)==-1) |
KlaasGovaerts | 16:ffd311730575 | 25 | incomingConnection=false; |
KlaasGovaerts | 16:ffd311730575 | 26 | else |
KlaasGovaerts | 16:ffd311730575 | 27 | connection->set_blocking(false); |
KlaasGovaerts | 16:ffd311730575 | 28 | printf("Connectie van: %s\n", connection->get_address()); |
KlaasGovaerts | 16:ffd311730575 | 29 | } |
KlaasGovaerts | 13:f3db7045e220 | 30 | } |
KlaasGovaerts | 13:f3db7045e220 | 31 | } |
KlaasGovaerts | 13:f3db7045e220 | 32 | |
KlaasGovaerts | 16:ffd311730575 | 33 | bool TCPlistener::receiveSegment(char *contents,int size){ |
KlaasGovaerts | 16:ffd311730575 | 34 | for(int i=0;i<arraySize;i++){ |
KlaasGovaerts | 16:ffd311730575 | 35 | int n = connections[i]->receive(contents, size); |
KlaasGovaerts | 16:ffd311730575 | 36 | if(n!=-1){ |
KlaasGovaerts | 16:ffd311730575 | 37 | contents[n] = '\0'; |
KlaasGovaerts | 16:ffd311730575 | 38 | printf("Segment ontvangen van %s:%i met inhoud \"%s\"\r\n", connections[i]->get_address(),connections[i]->get_port(),contents); |
KlaasGovaerts | 16:ffd311730575 | 39 | return true; |
KlaasGovaerts | 16:ffd311730575 | 40 | } |
KlaasGovaerts | 16:ffd311730575 | 41 | } |
KlaasGovaerts | 16:ffd311730575 | 42 | return false; |
KlaasGovaerts | 13:f3db7045e220 | 43 | } |
KlaasGovaerts | 13:f3db7045e220 | 44 | |
KlaasGovaerts | 16:ffd311730575 | 45 | TCPSocketConnection* TCPlistener::newConnection(){ |
KlaasGovaerts | 14:a6c651a1e51c | 46 | bool searchLocation=true; |
KlaasGovaerts | 14:a6c651a1e51c | 47 | for(int i=0;i<arraySize&&searchLocation;i++){ |
KlaasGovaerts | 15:5f1fda6b9140 | 48 | if(connections[i]==0){ |
KlaasGovaerts | 15:5f1fda6b9140 | 49 | connections[i]=new TCPSocketConnection(); |
KlaasGovaerts | 15:5f1fda6b9140 | 50 | return connections[i]; |
KlaasGovaerts | 15:5f1fda6b9140 | 51 | } else if(!(connections[i]->is_connected())){ |
KlaasGovaerts | 15:5f1fda6b9140 | 52 | delete connections[i]; |
KlaasGovaerts | 15:5f1fda6b9140 | 53 | connections[i]=new TCPSocketConnection(); |
KlaasGovaerts | 15:5f1fda6b9140 | 54 | return connections[i]; |
KlaasGovaerts | 14:a6c651a1e51c | 55 | } |
KlaasGovaerts | 14:a6c651a1e51c | 56 | } |
KlaasGovaerts | 14:a6c651a1e51c | 57 | return 0; |
KlaasGovaerts | 14:a6c651a1e51c | 58 | } |
KlaasGovaerts | 14:a6c651a1e51c | 59 | |
KlaasGovaerts | 14:a6c651a1e51c | 60 | /* |
KlaasGovaerts | 14:a6c651a1e51c | 61 | if(){ |
KlaasGovaerts | 14:a6c651a1e51c | 62 | connections[i]=new TCPSocketConnection(); |
KlaasGovaerts | 14:a6c651a1e51c | 63 | |
KlaasGovaerts | 14:a6c651a1e51c | 64 | searchLocation=false; |
KlaasGovaerts | 14:a6c651a1e51c | 65 | } |
KlaasGovaerts | 14:a6c651a1e51c | 66 | */ |