Quantcast
Channel: MSP low-power microcontroller forum - Recent Threads
Viewing all articles
Browse latest Browse all 22262

RTOS/MSP432E401Y: Socket TCP/IP Connection and disconnection

$
0
0

Part Number:MSP432E401Y

Tool/software: TI-RTOS

Hi, 

    I'm working on an application that requires to connect with multiples server as client TCP/IP. I'm able to connect with the first server, but I cannot close the connection and to open a new one in the same socket. 

My code here:

void tcpHandler_Client(uint32_t arg0, uint32_t arg1)
/*******************************************************************************
*   @fn         tcpHandler
*
*   @brief      Creates new Task to handle new TCP connections.
*
*   @param      arg0: PORT SERVER
*
*   @return     none
*/
{
    void *thread = NULL;
    int                status;
    int                client;
    struct sockaddr_in ServerAddr;
    struct sockaddr_in falseAddr;
    int                optval;
    int                bytesSent;
    int                optlen = sizeof(optval);

    /* TCP Socket */
    client = socket(AF_INET, SOCK_STREAM, 0);
    if (client == -1) {
        /* socket failed */
        /* TO DO */
        goto shutdown;
    }


    memset(&ServerAddr, 0, sizeof(ServerAddr));
    ServerAddr.sin_family = AF_INET;
    ServerAddr.sin_addr.s_addr = htonl(arg1);  //Address in compact mode (uin32_t)
    ServerAddr.sin_port = htons(arg0);

    memset(&falseAddr, 0, sizeof(falseAddr));
    falseAddr.sin_family = AF_INET;
    falseAddr.sin_addr.s_addr = NULL;
    falseAddr.sin_port = NULL;


    optval = TRUE;
    status = setsockopt(client, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
    if (status == -1) {
        /* setsockopt failed */
        /* TO DO */
        goto shutdown;
    }

    while(TRUE){

        if (connect(client, (struct sockaddr *)&ServerAddr, sizeof(ServerAddr)) == EAI_BADFLAGS ){
            /* connection failed */
            /* check the error */
            switch(fdError()){
            case EADDRINUSE:
                /* The specified address is already in use. */
                TaskSleep(TCP_CLIENT_CONN);
                break;
            case EADDRNOTAVAIL:
                /* The specified address is not available from the local machine. */
                TaskSleep(TCP_CLIENT_CONN+1);
                goto shutdown;
            case EALREADY:
                /* A connection request is already pending on this socket. */
                TaskSleep(TCP_CLIENT_CONN+2);
                break;
            case EBADF:
                /* The file descriptor (socket) is invalid. */
                TaskSleep(TCP_CLIENT_CONN+3);
                goto shutdown;
            case ECONNREFUSED:
                /* The attempt to connect was forcefully rejected. */
                TaskSleep(TCP_CLIENT_CONN+4);
                break;
            case EHOSTUNREACH:
                /* The host is not reachable. */
                TaskSleep(TCP_CLIENT_CONN+5);
                break;
            case EINPROGRESS:
                /* The request was accepted and is pending (non-blocking sockets). */
                TaskSleep(TCP_CLIENT_CONN+6);
                break;
            case EINVAL:
                /* Name arguments are invalid. */
                TaskSleep(TCP_CLIENT_CONN+7);
                goto shutdown;
            case EISCONN:
                /* The socket is already connected. */
                TaskSleep(TCP_CLIENT_CONN+8);
                break;
            case ENOTSOCK:
                /* The file descriptor does not reference a socket. */
                TaskSleep(TCP_CLIENT_CONN+9);
                goto shutdown;
            case ETIMEDOUT:
                /* Connection establishment timed out without establishing a connection. */
                TaskSleep(TCP_CLIENT_CONN+10);
                break;
            default:
                TaskSleep(TCP_CLIENT_CONN+11);
                break;

            }

        }else{

            bytesSent = send(client, "Test", sizeof("Test"), 0);
            if (bytesSent < 0 || bytesSent != sizeof("Test")) {
                /* fatal error */
                //break;
            }

            TaskSleep(TCP_CLIENT_CONN);
close(client);

TaskSleep(TCP_CLIENT_CONN); } } /* accept failed */ shutdown: if (client != -1) { close(client); } }

After the first connection I receive the error 

EISCONN  because the socket is still connected, the close function seems does not work properly. 


Any advice?


Viewing all articles
Browse latest Browse all 22262

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>