First version

Dependencies:   mbed EthernetInterface mbed-rto

Committer:
KlaasGovaerts
Date:
Wed Apr 18 08:09:56 2018 +0000
Revision:
13:a6c651a1e51c
Parent:
12:f3db7045e220
Child:
14:5f1fda6b9140
Versie 18/04 (2)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KlaasGovaerts 4:0413d42377d1 1 #include "TCPlistener.h"
KlaasGovaerts 4:0413d42377d1 2
KlaasGovaerts 4:0413d42377d1 3 TCPlistener::TCPlistener(){
KlaasGovaerts 4:0413d42377d1 4 char ip[]="192.168.0.253";
KlaasGovaerts 4:0413d42377d1 5 char mask[]="255.255.255.0";
KlaasGovaerts 4:0413d42377d1 6 char gateway[]="192.168.0.254";
KlaasGovaerts 4:0413d42377d1 7 eth.init(ip,mask,gateway);
KlaasGovaerts 4:0413d42377d1 8 eth.connect();
KlaasGovaerts 4:0413d42377d1 9 server.bind(4000);
KlaasGovaerts 12:f3db7045e220 10 server.set_blocking(false);
KlaasGovaerts 12:f3db7045e220 11 for(int i=0;i<10;i++){
KlaasGovaerts 13:a6c651a1e51c 12 connections[i]=new TCPSocketConnection();
KlaasGovaerts 12:f3db7045e220 13 }
KlaasGovaerts 4:0413d42377d1 14 }
KlaasGovaerts 12:f3db7045e220 15 void TCPlistener::listen(){
KlaasGovaerts 12:f3db7045e220 16 server.listen(10);//Accept 10 connections at a time.
KlaasGovaerts 12:f3db7045e220 17 //Source:https://os.mbed.com/handbook/Socket
KlaasGovaerts 12:f3db7045e220 18
KlaasGovaerts 12:f3db7045e220 19 bool incomingConnection=true;
KlaasGovaerts 12:f3db7045e220 20 while(incomingConnection){
KlaasGovaerts 13:a6c651a1e51c 21 TCPSocketConnection *connection=newConnection();
KlaasGovaerts 12:f3db7045e220 22 server.accept(client);
KlaasGovaerts 12:f3db7045e220 23 }
KlaasGovaerts 12:f3db7045e220 24 //client.set_blocking(false, 100);
KlaasGovaerts 12:f3db7045e220 25
KlaasGovaerts 12:f3db7045e220 26
KlaasGovaerts 12:f3db7045e220 27 printf("Connection from: %s\n", client.get_address());
KlaasGovaerts 12:f3db7045e220 28
KlaasGovaerts 12:f3db7045e220 29
KlaasGovaerts 12:f3db7045e220 30 }
KlaasGovaerts 12:f3db7045e220 31
KlaasGovaerts 4:0413d42377d1 32 void TCPlistener::receivePacket(char *contents,int size){
KlaasGovaerts 12:f3db7045e220 33 int n = server.receive(contents, size);
KlaasGovaerts 4:0413d42377d1 34 contents[n] = '\0';
KlaasGovaerts 4:0413d42377d1 35 printf("Segment ontvangen van %s:%i met inhoud \"%s\"\r\n", client.get_address(),client.get_port(),contents);
KlaasGovaerts 12:f3db7045e220 36 }
KlaasGovaerts 12:f3db7045e220 37
KlaasGovaerts 13:a6c651a1e51c 38 TCPSocketConnection * TCPlistener::newConnection(TCPSocketConnection **connectionArray,int arraySize){
KlaasGovaerts 13:a6c651a1e51c 39 bool searchLocation=true;
KlaasGovaerts 13:a6c651a1e51c 40 for(int i=0;i<arraySize&&searchLocation;i++){
KlaasGovaerts 13:a6c651a1e51c 41 if(connectionArray[i]==0){
KlaasGovaerts 13:a6c651a1e51c 42 connectionArray[i]=new TCPSocketConnection();
KlaasGovaerts 13:a6c651a1e51c 43 return connectionArray[i];
KlaasGovaerts 13:a6c651a1e51c 44 } else if(!(connectionArray[i]->is_connected())){
KlaasGovaerts 13:a6c651a1e51c 45 delete connectionArray[i];
KlaasGovaerts 13:a6c651a1e51c 46 connectionArray[i]=new TCPSocketConnection();
KlaasGovaerts 13:a6c651a1e51c 47 return connectionArray[i];
KlaasGovaerts 13:a6c651a1e51c 48 }
KlaasGovaerts 13:a6c651a1e51c 49 }
KlaasGovaerts 13:a6c651a1e51c 50 return 0;
KlaasGovaerts 13:a6c651a1e51c 51 }
KlaasGovaerts 13:a6c651a1e51c 52
KlaasGovaerts 13:a6c651a1e51c 53 /*
KlaasGovaerts 13:a6c651a1e51c 54 if(){
KlaasGovaerts 13:a6c651a1e51c 55 connections[i]=new TCPSocketConnection();
KlaasGovaerts 13:a6c651a1e51c 56
KlaasGovaerts 13:a6c651a1e51c 57 searchLocation=false;
KlaasGovaerts 13:a6c651a1e51c 58 }
KlaasGovaerts 13:a6c651a1e51c 59 */