Problem with TCPSocketConnection

24 Mar 2014

Hello Community,

i have a problem with an NXP LPC1768 an the TCPSocketConnection. Here is my code:

EthernetInterface eth;
TCPSocketConnection sock;
#ifdef DEBUG
Serial serial(USBTX, USBRX);
#endif

void InitEthernetConnection()
{
    #ifdef DEBUG
    serial.printf("Init LAN\r\n");
    #endif
    eth.init(); //INIT with DHCP
    eth.connect();  //CONNECT
    
    #ifdef DEBUG
    serial.printf("IP Adresse: %sr\n\r\n", eth.getIPAddress());
    #endif
}

void TransmitDataoverTCP(char *Host, string Daten, string Username, string Passwort)
{
    sock.connect(Host, 80);
    
    //Datenpaket schnüren
    string DATEN = "Username=" + Username + "&Passwort=" + Passwort + "&" + Daten;
    
    string POST = "POST /H2M/h2m.php HTTP/1.1\r\n";
    string HOST = "Host: ";
    HOST.append(Host);
    HOST.append("\r\n");
    string CONTENTTYPE = "Content-Type: application/x-www-form-urlencoded\r\n";
    string CONTENTLENGTH = "Content-Length: ";
    string Length;
    stringstream convert;
    convert << DATEN.length();
    Length = convert.str();
    CONTENTLENGTH.append(Length);
    CONTENTLENGTH.append("\r\n\r\n");
    
    string datenpaket = POST + HOST + CONTENTTYPE + CONTENTLENGTH + DATEN + "\r\n";
    
    //Umwandeln in char*
    const char *DataBuf = datenpaket.c_str();
    char* DataPaket = new char[datenpaket.length()];
    strcpy(DataPaket,DataBuf);
    DataPaket[datenpaket.size()] = '\0';
    
    #ifdef DEBUG
    serial.printf("%s",DataPaket);
    #endif    
    
    #ifdef DEBUG
    serial.printf("Sende Paket.\r\n");
    #endif
    
    sock.send_all(DataPaket, sizeof(DataPaket)-1);
    
    #ifdef DEBUG
    serial.printf("Paket gesendet.\r\n");
    #endif
    
    char buffer[300];
    int ret;
    while (true) {
        ret = sock.receive(buffer, sizeof(buffer)-1);
        if (ret <= 0)
            break;
        buffer[ret] = '\0';
        serial.printf("Received %d chars from server:\n%s\n", ret, buffer);
    }
    
    sock.close();
    
    #ifdef DEBUG
    serial.printf("Socket geschlossen.\r\n");
    #endif
    
   //eth.disconnect();
}

This is the call of the function above:

char Host[]="192.168.2.20";
TransmitDataoverTCP(Host, "Variable1=Test1&Variable2=Test2&Variable3=Test3", "USER", "PW");

My Problem is when i use this instead of the first Code:

    sock.connect(Host, 80);
    char http_cmd[] = "POST /H2M/h2m.php HTTP/1.1\r\nHost: 192.168.2.20\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 75\r\n\r\nUsername=USER&Passwort=PW&Variable1=Test1&Variable2=Test2&Variable3=Test3\r\n";
    sock.send_all(http_cmd, sizeof(http_cmd)-1);

    char buffer[300];
    int ret;
    while (true) {
        ret = sock.receive(buffer, sizeof(buffer)-1);
        if (ret <= 0)
            break;
        buffer[ret] = '\0';
        serial.printf("Received %d chars from server:\n%s\n", ret, buffer);
    }
    
    sock.close();

it works an will send the Data. Also i get the response of the Server over the serial interface. I hope you can help me.

Nice wishes S3B1

25 Mar 2014

Hello,

problem is solved. Instead of char* DataPaket = new char[datenpaket.length()]; i have to write char DataPaket[datenpaket.lengt()]. When it will work fine.

Nice Wishes S3B1