This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Error running NDK example HelloWorld

Hi,

I have tried out the NDK example HelloWorld sucessfully using the client program helloworld.exe file in C:\TI\ndk_2_20_04_26\packages\ti\ndk\winapps.

But when I tried to rebuild it using the source file included in the same directory, I got some compiler errors. So I fixed them and run it, and it sends data correctly to the DSP but failed to receive data from the DSP.

I was wondering if the source file was out of date, because it contains compiler errors. I am listing the codes below, hoping some one can help me find out why it failed to receive data.

Basically I have just commented out free( pBuf );, and added the WS2_32.lib.

The problem I have confronted is that at the client side recv( s, pBuf, TESTSIZE, 0 ) returns -1, while the server side has sendt data out correctly.

 

----------------------------------------------------------------------------------------------------

#include <windows.h>

#include <stdio.h>

#include <time.h>

#include <winsock.h>

#pragma comment(lib, "WS2_32")

 

#define TESTSIZE                15

 

int main( int argc, char *argv[] )

{

    WORD                wVersionRequested;

    WSADATA             wsaData;

    SOCKET              s;

    struct sockaddr_in  sin;

    unsigned char       *pBuf = "Hello World!\n";

    int                 tmp1,tmp2,tmp3,tmp4;

    int                 idle;

    fd_set              readfds;

    struct timeval      timeout;

    time_t              ts,tn;

 

 

    wVersionRequested = MAKEWORD(1, 1);

    tmp1 = WSAStartup(wVersionRequested, &wsaData);

    if (tmp1 != 0)

    {

        printf("\r\nUnable to initialize WinSock for host info");

        exit(0);

    }

 

    if( (argc != 2) ||

        sscanf(argv[1],"%03d.%03d.%03d.%03d",&tmp1,&tmp2,&tmp3,&tmp4)!=4 ||

        (tmp1 < 0 || tmp1 > 255 || tmp2 < 0 || tmp2 > 255 ||

         tmp3 < 0 || tmp3 > 255 || tmp4 < 0 || tmp4 > 255) )

    {

        printf("\nUsage: HELLOWORLD <x.x.x.x>\n");

        goto leave;

    }

 

    tmp1 |= tmp2 << 8;

    tmp1 |= tmp3 << 16;

    tmp1 |= tmp4 << 24;

 

    printf("\nTesting DSP UDP echo server at %d.%d.%d.%d\n",

                tmp1&255, (tmp1>>8)&255, (tmp1>>16)&255, (tmp1>>24)&255 );

 

    s = socket( AF_INET, SOCK_DGRAM, 0 );

    if( s < 0 )

    {

        printf("failed socket (%d)\n",WSAGetLastError());

        goto leave;

    }

 

    sin.sin_family      = AF_INET;

    sin.sin_addr.s_addr = 0;

    sin.sin_port        = htons(1234);

 

    if ( bind( s, &sin, sizeof(sin) ) < 0 )

    {

        printf("failed bind (%d)\n",WSAGetLastError());

        goto leave;

    }

 

    idle     = 0;

 

    timeout.tv_sec = 0;

    timeout.tv_usec = 0;

 

    sin.sin_addr.s_addr = tmp1;

    sin.sin_port        = htons(7);

 

    ts = time(0);

 

        // Send the string to the DSP

        if( sendto( s, pBuf, strlen(pBuf), 0, &sin, sizeof(sin) ) < 0 )

        {

                printf("send failed (%d)\n",WSAGetLastError());

                goto leave;

        }

 

        // Now try and receive a packet

        idle = 0;

        do

        {

                FD_ZERO( &readfds );

                FD_SET( s, &readfds );

 

                if( !select( 0, &readfds, 0, 0, &timeout ) )

                {

                        if( ++idle == 50000 )

                        {

                                printf("Receive timeout\n");

                                goto leave;

                        }

                }

                else

                        break;

        } while(1);

 

        tmp1 = recv( s, pBuf, TESTSIZE, 0 );

 

        // Print the data echoed back

        if (tmp1 > 0)

                printf("\nReceived back %d bytes.\nData Echoed back: %s\n",tmp1, pBuf);

 

    tn = time(0) - ts;

    printf("Exiting test. Time = %d seconds",tn);

leave:

    //if( pBuf )

    //    free( pBuf );

    if( s >= 0 )

        closesocket( s );

 

    WSACleanup();

}