AnsweredAssumed Answered

Connection TCP timeout

Question asked by Diego Derganz on Jun 17, 2013
Latest reply on Oct 24, 2013 by Martin Latal

Enviroment: M52259 demo board with MQX3.6 on CW 7.2

 

I have connect the M52259 board to my PC and a UMTS router thru the ethernet port.

 

I have to make a TCP connection to a server thru the UMTS router.

 

I can make the connection to my PC and from my PC to the server (thru the UMTS router using telnet), but I can't connect the board directly to the remote server (connection timeout).

 

Can be a problem with packet latency or else to estabilish the connection?

 

Thanks,

Diego Derganz

 

Lists attatched below:

void TCPClient(void)

{

    sockaddr_in    addr;

    _ip_address    ipaddr;

    uint_32        error;

    uint_32        option;

   

    char        buffer[TCPCLIENT_RECV_BUF_SIZE];

 

//    sockaddr    server_socket_addr;

   

    uint_32     socket_handle;

   

//    _ip_digital digit;

 

    uint_32     opt_val;

    uint_32     opt_len;

 

    ipaddr = Server_addr;

 

    printf("\n[TCPClient]: Launching TCPClient...");

 

    opt_val = 3 * 60 * 1000;

    opt_len = sizeof(uint_32);

 

    // set remote host(TCPServer) socket address

//    server_socket_addr.sin_family      = AF_INET;

//    server_socket_addr.sin_port        = port;

//    server_socket_addr.sin_addr.s_addr = ip;

 

    // loop to establish connection

    do {

        // Create a stream socket for TCPClient.

        socket_handle = socket(AF_INET, SOCK_STREAM, 0);

        if (socket_handle == RTCS_SOCKET_ERROR) {

            printf("\n[TCPClient]: Failed to create socket.");

            _task_block();

        }

 

        option = 10000;    // 10 sec  

        error = setsockopt(socket_handle, SOL_TCP, OPT_RETRANSMISSION_TIMEOUT, &option, sizeof(option));

//        Reduce buffer size of socket to save memory

        option = 512;  

        error = setsockopt(socket_handle, SOL_TCP, OPT_TBSIZE, &option, sizeof(option));

        option = 512;  

        error = setsockopt(socket_handle, SOL_TCP, OPT_RBSIZE, &option, sizeof(option));

 

        option = 1000;

        error = setsockopt(socket_handle, SOL_TCP, OPT_TIMEWAIT_TIMEOUT, &option, sizeof(option));

 

        // set 3mins for connection Timeout

        option = 3 * 60 * 1000;

        error = setsockopt(socket_handle, SOL_TCP, OPT_CONNECT_TIMEOUT, &option, sizeof(option));

        if (error != RTCS_OK) {

            printf("\n[TCPClient]: Failed to setsockopt for OPT_CONNECT_TIMEOUT, CODE = 0x%x.", error);

            _task_block();

        }

 

        // try to connect TCPServer

        addr.sin_port        = 2600;

        addr.sin_addr.s_addr = ipaddr;

        error = connect(socket_handle, &addr, sizeof(addr));

        if (error != RTCS_OK) {

            if (error == RTCSERR_TCP_TIMED_OUT) {

                printf("\n[TCPClient]: Connection timeout.");

            } else if (error == RTCSERR_TCP_CONN_RESET) {

                printf("\n[TCPClient]: Connection reset by peer."); // When connect to PC but TCPServer shuntdown, will alert this error

            }

            shutdown(socket_handle, FLAG_ABORT_CONNECTION);

            printf("\n[TCPClient]: Retry to connect TCPServer 5 seconds later...");

            _time_delay(5 * 1000);

        }

    } while (error != RTCS_OK);

 

//    // display connection message

//    ip_to_digital(ip, &digit);

//    printf("\n[TCPClient]: Connected to TCPServer, IP = %d.%d.%d.%d, Port = %d.",

//           digit.high, digit.middlehigh, digit.middlelow, digit.low, port);

    printf("\n[TCPClient]: Connected.");

 

     // clean buffer to ensure '\0' filled for recv

    memset(buffer, 0, sizeof(buffer));

   

    while (TRUE) {

        // Send msg to TCPServer

        if (send(socket_handle,

                 "I'm TCPClient. Please reply to me.", strlen("I'm TCPClient. Please reply to me") + 1,

                 0) == RTCS_ERROR) {

            printf("\n[TCPClient]: Failed to send, CODE = 0x%x.", RTCS_geterror(socket_handle));

            _task_block();

        }

 

        // wait to receive msg from TCPServer

        if (recv(socket_handle, buffer, sizeof(buffer), 0) == RTCS_ERROR) {

            error = RTCS_geterror(socket_handle);

 

            // Close TCPClient if TCPServer close first.

            if (error == RTCSERR_TCP_CONN_CLOSING) {

                shutdown(socket_handle, FLAG_ABORT_CONNECTION);

                printf("\n[TCPClient]: Closed.");

                break;

            // Close TCPClient if TCPServer abort.

            } else if (error == RTCSERR_TCP_CONN_RESET) {

                shutdown(socket_handle, FLAG_ABORT_CONNECTION);

                printf("\n[TCPClient]: Connection reset by peer.");

                printf("\n[TCPClient]: Closed.");

                break;

            } else {

                printf("\n[TCPClient]: Failed to recv, CODE = 0x%x.", error);

                _task_block();

            }

        } else {

            printf("\n[TCPClient]: Received \"%s\".", buffer);

           

            // clean buffer to ensure '\0' filled for next recv.

            memset(buffer, 0, sizeof(buffer));

        }

    }

}

Outcomes