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.

NDK Socket API: problem during reconnection to the TCP server

Other Parts Discussed in Thread: OMAP-L137, TMS320C6747

Dear E2E!

 

I have the next problem:

Socket-function connect() hangs for a minute while reconnecting to the server.

 

I wrote my TCP-client application on the base of the helloWord NDK example.

After callback function "NetworkIPAddr" is launched, I try to connect to a TCP-server in the separate task. (NetworkIPAddr is the callback function of the NC_NetStart(), it is called after the device IP address is obtained or released ) Priority of the task is 4 (lower as NDK tasks).

In this task following operations are performed:

1) create the socket;

2) set timeouts of the socket;

3) set number of port of the local socket;

4) connect socket to a server

code listing see pleace in the end of message.

 

The first launch of the program succeeds every time. Connect() returns zero (OK!) and then program can communicate with TCP-server with send() and recv().

But when I stop program and then restart it (for example, my device is resetted due to the power supply switching), then the program will hang in "connect" function for a minute. Changing of the socket timeouts hasn't effect.

If I will change the local port number - all right, connect() succeeds again, but only for a one call of the connect().

 It seems, that server after reset of my device has the opened part of the socket. It must be closed before my device call connect(). But how can I do this from my device using NDK API?

 

My platform:

TMS320C6747 EVM (OMAP-L137 EVM),

CCStudio3.3,

DSP/BIOS5.33.01

NDK 2.0.0

 

Thanks in advance!

Vitalii

 

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

Listing of the task that initializes and connects socket:

 

SOCKET s;

struct sockaddr_in peer;

int rc;

struct timeval timeout;

struct sockaddr_in MySockProp;  

int rc;

               

// Allocate the NDK file descriptor environment for this task

fdOpenSession( (HANDLE)TSK_self() ); // from NDK User's Guide "Developing Socket Applications with DSP/BIOS"

 

peer.sin_family = AF_INET;    

peer.sin_port   = htons( 4736 );  

peer.sin_addr.s_addr = inet_addr( "192.168.1.200" ); 

peer.sin_len    = sizeof( peer );

 

s = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( INVALID_SOCKET == s )

    printf("Error during socket() \n");

 

timeout.tv_sec = 20;

timeout.tv_usec = 0;

if( -1 == setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) )

    printf("Error when setting TIMEOUTs \n");

 

if( -1 == setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) )

                printf("Error when setting TIMEOUTs \n");

 

MySockProp.sin_family = AF_INET;      

MySockProp.sin_port   = htons( 7777 );

MySockProp.sin_addr.s_addr = htonl( INADDR_ANY );  

MySockProp.sin_len    = sizeof( MySockProp );     

if( -1 == bind( s, (struct sockaddr *)&MySockProp, sizeof(MySockProp) ) )

    printf("Error when setting portNo of the local socket \n");

 

rc = connect( s, (struct sockaddr *)&peer, sizeof(peer) ); // if it succeeds, the function returns 0

if ( rc )

    printf("Error during connect() \n");

 

if( 0 == n32ReturnValue )

    printf("Connected to TCP Server \n");

 

fdCloseSession( (HANDLE)TSK_self() ); // NDK User's Guide "Developing Socket Applications with DSP/BIOS"

TSK_exit();

  • Hi!
    I also experienced this problem with NDK 2.22.03.20 now.
    Have you found a solution in the meanwhile?
    Cheers, F.
  • Hi F. Brettschneider!

    The problem was on the server-side.
    It must close socket before device will try to reconnect.

    For example, after connection, server should ping device. If ping is unsuccessful, server must close this socket (on the server side).

    I hope, it will help!

    With best regards,
    Vitalii

  • Hi Vitalii,
    Thanks for you fast reply!
    In my case, I have no access to the software on server side. That's why my restarted device must handle the lost but still pending connection it by itself. Now I found out before connecting I must temporarily set the socket to non-blocking mode.

    //set nonblocking for connection timeout
    int sockopt = 0;
    if (setsockopt(sData, SOL_SOCKET, SO_BLOCKING, (char*) &sockopt, sizeof(int)) == -1) { /*error handling*/ }
    
    //do connect
    if (connect(sData, &sin.sa, sizeof(sin.sa)) < 0) {
        errno = fdError();
        if (errno != EWOULDBLOCK && errno != EINPROGRESS) { /*error handling*/ }
    }
    FD_ZERO(&wr);
    FD_SET( sData, &wr);
    tv.tv_sec = CONNECT_TIMEOUT;
    tv.tv_usec = 0;
    if (select(sData+1, 0, &wr, 0, &tv) < 1) { /*error handling*/ }
    
    //make blocking again
    sockopt = 1;
    if (setsockopt(sData, SOL_SOCKET, SO_BLOCKING, (char*) &sockopt, sizeof(int)) == -1){ /*error handling*/ }
    

    If connect runs into a timeout, I must send a QUIT command to the FTP server, and then login again.

    Cheers!