WORKS

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "test_env.h"
00003 #include "EthernetInterface.h"
00004 
00005 struct s_ip_address {
00006     int ip_1;
00007     int ip_2;
00008     int ip_3;
00009     int ip_4;
00010 };
00011 
00012 int main() {
00013     MBED_HOSTTEST_TIMEOUT(20);
00014     MBED_HOSTTEST_SELECT(tcpecho_client_auto);
00015     MBED_HOSTTEST_DESCRIPTION(TCP echo client);
00016     MBED_HOSTTEST_START("NET_4");
00017 
00018     char buffer[256] = {0};
00019     char out_buffer[] = "Hello World\n";
00020     char out_success[] = "{{success}}\n{{end}}\n";
00021     char out_failure[] = "{{failure}}\n{{end}}\n";
00022     s_ip_address ip_addr = {0, 0, 0, 0};
00023     int port = 0;
00024 
00025     printf("TCPCllient waiting for server IP and port..." NL);
00026     scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port);
00027     printf("Address received:%d.%d.%d.%d:%d" NL, ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port);
00028 
00029     EthernetInterface eth;
00030     eth.init(); //Use DHCP
00031     eth.connect();
00032 
00033     printf("TCPClient IP Address is %s" NL, eth.getIPAddress());
00034     sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4);
00035 
00036     TCPSocketConnection socket;
00037     while (socket.connect(buffer, port) < 0) {
00038         printf("TCPCllient unable to connect to %s:%d" NL, buffer, port);
00039         wait(1);
00040     }
00041 
00042     socket.send_all(out_buffer, sizeof(out_buffer) - 1);
00043 
00044     int n = socket.receive(buffer, sizeof(buffer));
00045     if (n > 0)
00046     {
00047         buffer[n] = '\0';
00048         printf("%s", buffer);
00049         if (strncmp(out_buffer, buffer, sizeof(out_buffer) - 1) == 0) {
00050             socket.send_all(out_success, sizeof(out_success) - 1);
00051         }
00052     }
00053 
00054     socket.send_all(out_failure, sizeof(out_failure) - 1);
00055 
00056     socket.close();
00057     eth.disconnect();
00058     return 0;
00059 }