![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
demo versie 16/05
Dependencies: EthernetInterface mbed-rto mbed
Fork of ProjectVLC by
TCPlistener.cpp
- Committer:
- KlaasGovaerts
- Date:
- 2018-04-18
- Revision:
- 16:ffd311730575
- Parent:
- 15:5f1fda6b9140
- Child:
- 19:5ee34e60a31d
File content as of revision 16:ffd311730575:
#include "TCPlistener.h" TCPlistener::TCPlistener(){ char ip[]="192.168.0.253"; char mask[]="255.255.255.0"; char gateway[]="192.168.0.254"; eth.init(ip,mask,gateway); eth.connect(); server.bind(4000); server.listen(); server.set_blocking(false); for(int i=0;i<10;i++){ connections[i]=0; } arraySize=10; } void TCPlistener::listen(){ //server.listen(10);//Accept 10 connections at a time. //Source:https://os.mbed.com/handbook/Socket bool incomingConnection=true; while(incomingConnection){ TCPSocketConnection* connection=newConnection(); if(connection!=0){ if(server.accept(*connection)==-1) incomingConnection=false; else connection->set_blocking(false); printf("Connectie van: %s\n", connection->get_address()); } } } bool TCPlistener::receiveSegment(char *contents,int size){ for(int i=0;i<arraySize;i++){ int n = connections[i]->receive(contents, size); if(n!=-1){ contents[n] = '\0'; printf("Segment ontvangen van %s:%i met inhoud \"%s\"\r\n", connections[i]->get_address(),connections[i]->get_port(),contents); return true; } } return false; } TCPSocketConnection* TCPlistener::newConnection(){ bool searchLocation=true; for(int i=0;i<arraySize&&searchLocation;i++){ if(connections[i]==0){ connections[i]=new TCPSocketConnection(); return connections[i]; } else if(!(connections[i]->is_connected())){ delete connections[i]; connections[i]=new TCPSocketConnection(); return connections[i]; } } return 0; } /* if(){ connections[i]=new TCPSocketConnection(); searchLocation=false; } */