Select is not blocking. - VxWorks

This is a discussion on Select is not blocking. - VxWorks ; Hi, I have created two tasks. one creates a socket and sends only one message. The other one first wait on select to read the data and if the data is availble, it will read it. What happend is select ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Select is not blocking.

  1. Select is not blocking.

    Hi,

    I have created two tasks. one creates a socket and sends only one
    message. The other one first wait on select to read the data and if
    the data is availble, it will read it.

    What happend is select is returned correctly for the first time when
    the socket has the data and read it. Afterthat also select is
    returting 1 eventhough my socket has nothing to read. Here is my code.

    #define MAX_MESSAGE_LENGTH 1024

    typedef enum {
    BOOL_FALSE = 0,
    BOOL_TRUE = 1
    } Boolean_t;


    void nar_sender_task( void )
    {

    int mbxSock;
    int noOfMsg ;
    char *msg;
    int msglen;


    printf ("\n Entered into the sender task");

    msg = (char *)malloc( MAX_MESSAGE_LENGTH );

    if (NULL == msg)
    {
    printf("\n Memory allocation failed");
    exit(1);
    }

    memset(msg, 0xAA , MAX_MESSAGE_LENGTH - 1);
    msg[MAX_MESSAGE_LENGTH - 1] = '\0';

    msglen = strlen ( msg ) + 1;

    printf("\n msglen : %d", msglen);


    // create socket and init parameters
    mbxSock = socket(AF_INET, SOCK_STREAM,0);
    if ( -1 == mbxSock )
    {
    printf("\n Socket creation failed");
    exit(1);
    }

    printf("\n Socket creation successful : sender");
    int reuse = 1;
    setsockopt(mbxSock, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse,
    sizeof(int));

    // connect socket to receiver task
    struct sockaddr_in addrPtr;

    addrPtr.sin_family = AF_INET;
    addrPtr.sin_port = 1299;

    // use loopback device
    addrPtr.sin_addr.s_addr = inet_addr("127.0.0.1");

    while (0 != connect(mbxSock,
    (struct sockaddr *) &addrPtr,
    sizeof(addrPtr)))
    {
    printf("\n Error during socket connection, still trying...");

    taskDelay(1000);
    }

    int flag = 1;
    setsockopt(mbxSock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag,
    sizeof(int));

    int bufferSize = 16384;
    setsockopt(mbxSock,SOL_SOCKET,SO_SNDBUF,(char
    *)&bufferSize,sizeof(int));

    noOfMsg = 0;
    do
    {
    int tmpsent;

    // Call send to send the message block to receiver.
    //
    printf ( "\n Calling send
    --------------------------------------------------");


    tmpsent = send(mbxSock, (char *)msg, msglen, 0);

    if( -1 == tmpsent )
    {
    // send failed !
    // assume it is because there is no more free mbufs
    // so, we wait a bit, and try again !

    printf("send to other thread failed !");

    taskDelay(250);
    continue;
    }
    noOfMsg++;
    }while (1 != noOfMsg );

    free ( msg );

    shutdown(mbxSock,2);
    close(mbxSock);

    printf("\n -----------------------------------------");
    printf("\n Existing from nar_sender_task");
    printf("\n -----------------------------------------");
    }


    void nar_receiver_task ( void )
    {

    struct fd_set fdMasks;

    int maxSockId = -1;
    int listenSock;
    int mailboxSock = -1;

    int count = 0;

    FD_ZERO(&fdMasks);

    listenSock = socket( AF_INET, SOCK_STREAM,0 );

    if ( -1 == listenSock )
    {
    printf("\n Listen socket creation failed");
    exit(1);
    }

    printf ("\n Listen socket created");

    int reuse = 1;
    setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR,(char *)
    &reuse,sizeof(int));


    sockaddr_in add;

    add.sin_family = AF_INET;
    add.sin_port = 1299;
    add.sin_addr.s_addr = INADDR_ANY;

    // Bind address to the socket.
    //
    if ( -1 == bind(listenSock, (struct sockaddr *)&add, sizeof(struct
    sockaddr_in)) )
    {
    printf("\n Socket bind failed with error no : %d",errno);
    shutdown(listenSock,2);
    close(listenSock);

    exit(1);
    }

    printf ("\n Bind Successful");

    if ( -1 == listen(listenSock, 5) )
    {
    printf("\n Socket listen failed with error no : %d",errno);
    shutdown(listenSock,2);
    close(listenSock);

    exit(1);
    }

    printf("\n Listen successful");

    struct sockaddr clientAddress;
    int addrlg; // socket address structure length.

    printf("\n Calling accept .... Naresh");

    addrlg = sizeof(clientAddress);
    int tmp_sock_id = accept(listenSock,
    ( sockaddr *) &clientAddress, &addrlg);

    if (-1 == tmp_sock_id)
    {

    printf("\n Socket accept failed with error no : %d",errno);
    shutdown(listenSock,2);
    close(listenSock);

    exit(1);
    }


    printf ("\n tmp_sock_id : %d", tmp_sock_id);

    unsigned long remoteIp = ((struct sockaddr_in *)
    &clientAddress)->sin_addr.s_addr;

    // socket ID is valid
    // check if it is the local socket from the other task
    if( remoteIp == inet_addr("127.0.0.1") )
    {
    mailboxSock = tmp_sock_id;
    /* int blocking = 1;
    ioctl(mailboxSock,FIONBIO,(int)&blocking); */

    FD_SET(mailboxSock, &fdMasks);

    // and update maxSockId
    if ( mailboxSock > maxSockId )
    maxSockId = mailboxSock;

    printf ("\n mailboxSock : %d", mailboxSock);
    printf("\n maxSockId : %d", maxSockId);
    }

    int select_code;

    while(1)
    {

    printf ("\n Calling select");
    // call select
    printf("\n maxSockId : %d", maxSockId);

    FD_ZERO(&fdMasks);
    FD_SET(mailboxSock, &fdMasks);

    select_code = select( maxSockId + 1,
    &fdMasks, NULL, NULL, NULL);

    if( -1 == select_code )
    {
    printf("\n Select is failed ............");
    exit(1);
    }

    if ( errno == EINTR )
    {
    printf("\n Select returned EINTR");
    continue;
    }

    // measure of 'empty' select()
    if ( 0 == select_code )
    {
    printf ("empty select with errno = %d", errno);
    continue;
    }


    printf ("\n Printing select return code : %d", select_code);

    if( (-1 != mailboxSock ) && (FD_ISSET(mailboxSock, &fdMasks)) )
    {
    int rcvBytes;
    char bufferToRec[MAX_MESSAGE_LENGTH];

    rcvBytes = recv(mailboxSock,(char
    *)bufferToRec,MAX_MESSAGE_LENGTH,0);

    if (rcvBytes == -1)
    {
    printf("\n Socket Recv failed with error no :
    %d",errno);
    shutdown(listenSock,2);
    close(listenSock);
    exit(1);
    }

    if ( 0 != rcvBytes )
    {
    printf(" \n Message : %d", count++);
    printf("\n
    ------------------------------------------");
    printf("\n ( --- No.of bytes Received : %d --- )",
    rcvBytes );
    printf( "\n Read message : %s", bufferToRec);
    }
    else
    {
    printf ("\n recv retured %d as return value with error
    no : %d", rcvBytes, errno);
    }
    }
    }

    }

    Can some body help me why the select is not blocking eventhough my
    socket has nothing to read.


  2. Re: Select is not blocking.

    naresh.ganapathineedi@gmail.com wrote:
    > Hi,
    >
    > I have created two tasks. one creates a socket and sends only one
    > message. The other one first wait on select to read the data and if
    > the data is availble, it will read it.
    >
    > What happend is select is returned correctly for the first time when
    > the socket has the data and read it. Afterthat also select is
    > returting 1 eventhough my socket has nothing to read. Here is my code.


    Select() is returning because the sender closed the socket. Your code
    needs to look at the case where the fd is readable but the read returns
    0 bytes as the case where the receiver will close its fd and remove it
    from the select list.

    Mark

    >
    > #define MAX_MESSAGE_LENGTH 1024
    >
    > typedef enum {
    > BOOL_FALSE = 0,
    > BOOL_TRUE = 1
    > } Boolean_t;
    >
    >
    > void nar_sender_task( void )
    > {
    >
    > int mbxSock;
    > int noOfMsg ;
    > char *msg;
    > int msglen;
    >
    >
    > printf ("\n Entered into the sender task");
    >
    > msg = (char *)malloc( MAX_MESSAGE_LENGTH );
    >
    > if (NULL == msg)
    > {
    > printf("\n Memory allocation failed");
    > exit(1);
    > }
    >
    > memset(msg, 0xAA , MAX_MESSAGE_LENGTH - 1);
    > msg[MAX_MESSAGE_LENGTH - 1] = '\0';
    >
    > msglen = strlen ( msg ) + 1;
    >
    > printf("\n msglen : %d", msglen);
    >
    >
    > // create socket and init parameters
    > mbxSock = socket(AF_INET, SOCK_STREAM,0);
    > if ( -1 == mbxSock )
    > {
    > printf("\n Socket creation failed");
    > exit(1);
    > }
    >
    > printf("\n Socket creation successful : sender");
    > int reuse = 1;
    > setsockopt(mbxSock, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse,
    > sizeof(int));
    >
    > // connect socket to receiver task
    > struct sockaddr_in addrPtr;
    >
    > addrPtr.sin_family = AF_INET;
    > addrPtr.sin_port = 1299;
    >
    > // use loopback device
    > addrPtr.sin_addr.s_addr = inet_addr("127.0.0.1");
    >
    > while (0 != connect(mbxSock,
    > (struct sockaddr *) &addrPtr,
    > sizeof(addrPtr)))
    > {
    > printf("\n Error during socket connection, still trying...");
    >
    > taskDelay(1000);
    > }
    >
    > int flag = 1;
    > setsockopt(mbxSock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag,
    > sizeof(int));
    >
    > int bufferSize = 16384;
    > setsockopt(mbxSock,SOL_SOCKET,SO_SNDBUF,(char
    > *)&bufferSize,sizeof(int));
    >
    > noOfMsg = 0;
    > do
    > {
    > int tmpsent;
    >
    > // Call send to send the message block to receiver.
    > //
    > printf ( "\n Calling send
    > --------------------------------------------------");
    >
    >
    > tmpsent = send(mbxSock, (char *)msg, msglen, 0);
    >
    > if( -1 == tmpsent )
    > {
    > // send failed !
    > // assume it is because there is no more free mbufs
    > // so, we wait a bit, and try again !
    >
    > printf("send to other thread failed !");
    >
    > taskDelay(250);
    > continue;
    > }
    > noOfMsg++;
    > }while (1 != noOfMsg );
    >
    > free ( msg );
    >
    > shutdown(mbxSock,2);
    > close(mbxSock);
    >
    > printf("\n -----------------------------------------");
    > printf("\n Existing from nar_sender_task");
    > printf("\n -----------------------------------------");
    > }
    >
    >
    > void nar_receiver_task ( void )
    > {
    >
    > struct fd_set fdMasks;
    >
    > int maxSockId = -1;
    > int listenSock;
    > int mailboxSock = -1;
    >
    > int count = 0;
    >
    > FD_ZERO(&fdMasks);
    >
    > listenSock = socket( AF_INET, SOCK_STREAM,0 );
    >
    > if ( -1 == listenSock )
    > {
    > printf("\n Listen socket creation failed");
    > exit(1);
    > }
    >
    > printf ("\n Listen socket created");
    >
    > int reuse = 1;
    > setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR,(char *)
    > &reuse,sizeof(int));
    >
    >
    > sockaddr_in add;
    >
    > add.sin_family = AF_INET;
    > add.sin_port = 1299;
    > add.sin_addr.s_addr = INADDR_ANY;
    >
    > // Bind address to the socket.
    > //
    > if ( -1 == bind(listenSock, (struct sockaddr *)&add, sizeof(struct
    > sockaddr_in)) )
    > {
    > printf("\n Socket bind failed with error no : %d",errno);
    > shutdown(listenSock,2);
    > close(listenSock);
    >
    > exit(1);
    > }
    >
    > printf ("\n Bind Successful");
    >
    > if ( -1 == listen(listenSock, 5) )
    > {
    > printf("\n Socket listen failed with error no : %d",errno);
    > shutdown(listenSock,2);
    > close(listenSock);
    >
    > exit(1);
    > }
    >
    > printf("\n Listen successful");
    >
    > struct sockaddr clientAddress;
    > int addrlg; // socket address structure length.
    >
    > printf("\n Calling accept .... Naresh");
    >
    > addrlg = sizeof(clientAddress);
    > int tmp_sock_id = accept(listenSock,
    > ( sockaddr *) &clientAddress, &addrlg);
    >
    > if (-1 == tmp_sock_id)
    > {
    >
    > printf("\n Socket accept failed with error no : %d",errno);
    > shutdown(listenSock,2);
    > close(listenSock);
    >
    > exit(1);
    > }
    >
    >
    > printf ("\n tmp_sock_id : %d", tmp_sock_id);
    >
    > unsigned long remoteIp = ((struct sockaddr_in *)
    > &clientAddress)->sin_addr.s_addr;
    >
    > // socket ID is valid
    > // check if it is the local socket from the other task
    > if( remoteIp == inet_addr("127.0.0.1") )
    > {
    > mailboxSock = tmp_sock_id;
    > /* int blocking = 1;
    > ioctl(mailboxSock,FIONBIO,(int)&blocking); */
    >
    > FD_SET(mailboxSock, &fdMasks);
    >
    > // and update maxSockId
    > if ( mailboxSock > maxSockId )
    > maxSockId = mailboxSock;
    >
    > printf ("\n mailboxSock : %d", mailboxSock);
    > printf("\n maxSockId : %d", maxSockId);
    > }
    >
    > int select_code;
    >
    > while(1)
    > {
    >
    > printf ("\n Calling select");
    > // call select
    > printf("\n maxSockId : %d", maxSockId);
    >
    > FD_ZERO(&fdMasks);
    > FD_SET(mailboxSock, &fdMasks);
    >
    > select_code = select( maxSockId + 1,
    > &fdMasks, NULL, NULL, NULL);
    >
    > if( -1 == select_code )
    > {
    > printf("\n Select is failed ............");
    > exit(1);
    > }
    >
    > if ( errno == EINTR )
    > {
    > printf("\n Select returned EINTR");
    > continue;
    > }
    >
    > // measure of 'empty' select()
    > if ( 0 == select_code )
    > {
    > printf ("empty select with errno = %d", errno);
    > continue;
    > }
    >
    >
    > printf ("\n Printing select return code : %d", select_code);
    >
    > if( (-1 != mailboxSock ) && (FD_ISSET(mailboxSock, &fdMasks)) )
    > {
    > int rcvBytes;
    > char bufferToRec[MAX_MESSAGE_LENGTH];
    >
    > rcvBytes = recv(mailboxSock,(char
    > *)bufferToRec,MAX_MESSAGE_LENGTH,0);
    >
    > if (rcvBytes == -1)
    > {
    > printf("\n Socket Recv failed with error no :
    > %d",errno);
    > shutdown(listenSock,2);
    > close(listenSock);
    > exit(1);
    > }
    >
    > if ( 0 != rcvBytes )
    > {
    > printf(" \n Message : %d", count++);
    > printf("\n
    > ------------------------------------------");
    > printf("\n ( --- No.of bytes Received : %d --- )",
    > rcvBytes );
    > printf( "\n Read message : %s", bufferToRec);
    > }
    > else
    > {
    > printf ("\n recv retured %d as return value with error
    > no : %d", rcvBytes, errno);
    > }
    > }
    > }
    >
    > }
    >
    > Can some body help me why the select is not blocking eventhough my
    > socket has nothing to read.
    >


  3. Re: Select is not blocking.

    Thank u Mark.
    It is working now.


+ Reply to Thread