TCPEchoClient-WIZwiki-W7500

Dependencies:   WIZnetInterface mbed DDNS_NoIP

main.cpp

Committer:
Ricky_Kwon
Date:
2015-07-22
Revision:
0:cdef497a8acd
Child:
1:86be81112448

File content as of revision 0:cdef497a8acd:

#include "mbed.h"
#include "EthernetInterface.h"

uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x12, 0x34, 0x45};
const char ip_addr[] = "192.168.0.123"; 
const char mask_addr[] = "255.255.255.0"; 
const char gateway_addr[] = "192.168.0.1"; 

const char* ECHO_SERVER_ADDRESS = "192.168.0.230";
const int ECHO_SERVER_PORT = 7;

int main (void) 
{
    char buf[256];
    printf("Wait a second...\r\n");
    EthernetInterface eth;
    eth.init(mac_addr, ip_addr, mask_addr, gateway_addr); //Use Static
    eth.connect();
   
     // Connect to Server
    TCPSocketConnection socket;
    
    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
        printf("Unable to connect to (%s) on port (%d)\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
        wait(1);
    }
    printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
    
    // Send message to server
    char hello[] = "Hello World";
    printf("Sending  message to Server : '%s' \r\n",hello);
    socket.send_all(hello, sizeof(hello) - 1);   
    
    while(true) {
        memset(buf, 0, sizeof(buf));
        // Receive message from server
        int n = socket.receive(buf, 256);
        buf[n] = '\0';
        printf("Received message from server: '%s' \r\n", buf);
    }
}