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:
- 13:a6c651a1e51c
- Parent:
- 12:f3db7045e220
- Child:
- 14:5f1fda6b9140
File content as of revision 13:a6c651a1e51c:
#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.set_blocking(false);
for(int i=0;i<10;i++){
connections[i]=new TCPSocketConnection();
}
}
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();
server.accept(client);
}
//client.set_blocking(false, 100);
printf("Connection from: %s\n", client.get_address());
}
void TCPlistener::receivePacket(char *contents,int size){
int n = server.receive(contents, size);
contents[n] = '\0';
printf("Segment ontvangen van %s:%i met inhoud \"%s\"\r\n", client.get_address(),client.get_port(),contents);
}
TCPSocketConnection * TCPlistener::newConnection(TCPSocketConnection **connectionArray,int arraySize){
bool searchLocation=true;
for(int i=0;i<arraySize&&searchLocation;i++){
if(connectionArray[i]==0){
connectionArray[i]=new TCPSocketConnection();
return connectionArray[i];
} else if(!(connectionArray[i]->is_connected())){
delete connectionArray[i];
connectionArray[i]=new TCPSocketConnection();
return connectionArray[i];
}
}
return 0;
}
/*
if(){
connections[i]=new TCPSocketConnection();
searchLocation=false;
}
*/
