TCP send message to Server

21 Jan 2012

Hello,

I'm working several Days on a Problem and now I'm at loss.

I would send e.g. every hour a tcp packet to a server. But it wouldn't work. Can anybody help me?

Here is my code

#include "mbed.h"
#include "EthernetNetIf.h"
#include "TCPSocket.h"

EthernetNetIf eth;
TCPSocket sock;
Ticker socketticker;

void onTCPSocketEvent(TCPSocketEvent e)
{
    switch(e)
    {
        case TCPSOCKET_CONNECTED:
            printf("Connected\n");
        break;
        
        case TCPSOCKET_ACCEPT:
            printf("Accepted\n");
        break;
        
        case TCPSOCKET_READABLE:
            printf("Readable\n");
        break;
        
        case TCPSOCKET_WRITEABLE:
            printf("Writeable\n");
        break;
        
        case TCPSOCKET_CONTIMEOUT:
            printf("Timeout\n");
        break;
        
        case TCPSOCKET_CONRST:
            printf("Reset\n");
        break;
        case TCPSOCKET_CONABRT:
            printf("Aborted\n");
        break;
        
        case TCPSOCKET_ERROR:
            printf("Error\n");
        break;
        
        case TCPSOCKET_DISCONNECTED:
            printf("Disconnected\n");
            sock.close();
        break;    
    }
}

void SendtoServer() {
    Net::poll();
    printf("Ausfuehren Unterprogramm SendtoServer\n"); 
    Host server(IpAddr(192, 168, 217, 154), 12345);
    wait(3);
    printf("\nSocket verbinden...\n");          
    TCPSocketErr bindErr = sock.connect(server);
    if(bindErr == 0) {
        printf("Socket verbunden..\n");
        }
    else {
        printf("Verbindungsfehler %i", bindErr);
        }
    wait(1);
    const char* str = "Testdata : abcabcabcabc...";
    int err = sock.send(str, strlen(str));
    if(err < 0)
    {
        printf("Fehler beim Senden der Daten!\n");
    }
    else
    {
        printf("Daten gesendet : %s\n", str);
    }
    wait(1);
    sock.close();   
}



int main() {
    socketticker.attach(&SendtoServer, 30);
    printf("Start des Main-Programmes\n");
    //Initialisierung ETH
    EthernetErr ethErr = eth.setup();
    if(ethErr)
    {
        printf("Error %d in setup.\n", ethErr);
        return -1;
    }
    printf("Setup OK\n");
    while(1) {
    }
}

thanks in advanced Klaus