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.
Revision 1:8c10d8f4e3aa, committed 2020-05-09
- Comitter:
- JohnnyK
- Date:
- Sat May 09 09:16:53 2020 +0000
- Parent:
- 0:633c10a3bb5d
- Commit message:
- Correction of buffers and rework to Network Interface instead of Ethernet Interface.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 633c10a3bb5d -r 8c10d8f4e3aa main.cpp
--- a/main.cpp Fri May 08 18:47:52 2020 +0000
+++ b/main.cpp Sat May 09 09:16:53 2020 +0000
@@ -1,5 +1,7 @@
#include "mbed.h" //MbedOS 5.15
-#include "EthernetInterface.h"
+#include "NetworkInterface.h"
+
+#define BUFFSIZE 50
#define ROUTER // commented = Static IP uncomented = address assigned by router
#ifndef ROUTER
@@ -8,73 +10,79 @@
#define MASK "255.255.255.0"
#endif
#define PORT 20
-
+
DigitalOut led1(LED1);
-
-EthernetInterface eth;
+
+NetworkInterface *net = NetworkInterface::get_default_instance();
TCPSocket server;
TCPSocket* client;
SocketAddress clientAddress;
+
+char *in_buffer;
+char *out_buffer;
int main (void){
printf("TCP Echo starting...\n");
- int eth_stat;
+ int net_stat;
#ifndef ROUTER
- eth.disconnect();
- eth_stat = eth.set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY);
- printf("set IP status: %i\n",eth_stat);
+ net->disconnect();
+ net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY);
+ printf("set IP status: %i\n",net_stat);
#endif
- eth_stat = eth.connect();
- printf("connect status: %i\n",eth_stat);
-
+ net_stat = net->connect();
+ printf("connect status: %i\n",net_stat);
+
SocketAddress ip;
- eth.get_ip_address(&ip);
+ net->get_ip_address(&ip);
const char *p_ip = ip.get_ip_address();
printf("IP address: %s and Port: %d\n", p_ip ? p_ip : "None" , PORT );
SocketAddress mask;
- eth.get_netmask(&mask);
+ net->get_netmask(&mask);
const char *p_mask = mask.get_ip_address();
printf("Netmask: %s\n", p_mask ? p_mask : "None");
SocketAddress gateway;
- eth.get_gateway(&gateway);
+ net->get_gateway(&gateway);
const char *p_gateway = gateway.get_ip_address();
printf("Gateway: %s\n", p_gateway ? p_gateway : "None");
if(ip){
- server.open(ð);
+ server.open(net);
server.bind(PORT);
server.listen(1);
while (1) {
printf("Server bound and listening\n");
client = server.accept();
-
+
client->getpeername(&clientAddress);
const char *p_clientAddress = clientAddress.get_ip_address();
printf("Client connected from IP address: %s\n", p_clientAddress ? p_clientAddress : "None");
bool b = true;
while (b) {
- char in_buffer[100];
- int n = client->recv(in_buffer, sizeof(in_buffer));
+ in_buffer = new char[BUFFSIZE];
+ int n = client->recv(in_buffer, BUFFSIZE);
if (n == 0) {
printf("Client disconnected\n");
b = false;
}else{
+ in_buffer[n] = '\0';
printf("Received message from Client :'%s'\n", in_buffer);
- char out_buffer[100];
+ out_buffer = new char[BUFFSIZE];
n = sprintf(out_buffer,"Echo - %s", in_buffer);
printf("Sending echo to client\n");
client->send(out_buffer, n);
led1 =! led1;
+ delete []out_buffer;
}
led1 =! led1;
+ delete []in_buffer;
}
client->close();
}
}else{
printf("No IP\n");
- eth.disconnect();
+ net->disconnect();
printf("Program end\n");
}
}
\ No newline at end of file