Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed EthernetInterface mbed-rto
TCPlistener.cpp
- Committer:
- KlaasGovaerts
- Date:
- 2018-04-18
- Revision:
- 15:ffd311730575
- Parent:
- 14:5f1fda6b9140
- Child:
- 18:5ee34e60a31d
File content as of revision 15: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;
}
*/
