Click here to Skip to main content
15,891,788 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Clarification Pin
Nawal K Gupta31-Aug-06 19:37
Nawal K Gupta31-Aug-06 19:37 
AnswerRe: Threading Interview Question Pin
Zac Howland31-Aug-06 4:49
Zac Howland31-Aug-06 4:49 
AnswerRe: Threading Interview Question Pin
Cedric Moonen31-Aug-06 4:53
Cedric Moonen31-Aug-06 4:53 
GeneralRe: Threading Interview Question Pin
toxcct31-Aug-06 5:29
toxcct31-Aug-06 5:29 
AnswerRe: Clarification Pin
Nawal K Gupta31-Aug-06 19:40
Nawal K Gupta31-Aug-06 19:40 
QuestionGenerating GUID Pin
<color>Aljechin 31-Aug-06 4:30
<color>Aljechin 31-Aug-06 4:30 
AnswerRe: Generating GUID Pin
ovidiucucu31-Aug-06 4:52
ovidiucucu31-Aug-06 4:52 
QuestionProblem with select() with multiple sockets [modified] Pin
DevendraC31-Aug-06 4:07
DevendraC31-Aug-06 4:07 
From one of my client applications, I open non-blocking TCP sockets to multiple servers and then immediately call select() on all these sockets with a time out value of 30 seconds. The select() always returns before timeout value and its value is always less than the available servers.

For example I open 3 non blocking TCP connections for connecting to 3 up and running servers on same machine. Then I call select() on these 3 sockets. The return value of select() is 2 rather than 3. Even the logs of 3 servers shows connection attempts.

Following is code snippets:
SOCKET fds[MAX_SOCKETS];
struct sockaddr_in ads [MAX_SOCKETS];
for (int nNumConnections = 0; nNumConnections < 3; nNumConnections++)
{
    ads[nNumConnections].sin_addr.s_addr = inet_addr(lpszIpAddr);
    ads[nNumConnections].sin_family = AF_INET;
    ads[nNumConnections].sin_port = htons((unsigned short) (nPort[nNumConnections]) );
}


fd_set writeFDS,exceptFDS;
FD_ZERO (&writeFDS);
FD_ZERO (&exceptFDS);
int nMaxFd = 0;

int nSocketCount = 0;
for (int i = 0; i < 3; i++)
{
    fds[i] = socket( AF_INET, SOCK_STREAM , IPPROTO_TCP );
    // Set the socket in non-blocking mode
    // flag for blocking mode
    unsigned long setBlocking = 1;
    if ((ioctlsocket (fds[i], FIONBIO, &setBlocking)) != 0)
    {
        return -1;
    }
    int nResult = connect(fds[i], (SOCKCONST sockaddr *) &ads[i], sizeof (ads[i]));
    if (nResult == 0)
    {
        if (fds[i] >= nMaxFd) nMaxFd = fds[i] + 1;
        FD_SET (fds[i], &writeFDS);
        FD_SET (fds[i], &exceptFDS);
        nSocketCount ++
    }
    else if (nResult == SOCKET_ERROR)
    {
        if(WSAGetLastError() == WSAEWOULDBLOCK)
        {
            if (fds[i] >= nMaxFd) nMaxFd = fds[i] + 1;
            FD_SET (fds[i], &writeFDS);
            FD_SET (fds[i], &exceptFDS);
            nSocketCount++;
        }

    }
}

cout << "Socket count in the FD_SET :" << nSocketCount;

if (nSocketCount > 0)
{
    struct timeval tv;
    tv.tv_sec = 60;
    tv.tv_usec = 0;

    nSelectStatus = select (nMaxFd, NULL, &writeFDS, &exceptFDS, &tv);

    cout <<"Return value of select :" << nSelectStatus;

    if (nSelectStatus <=0 || nSelectStatus == SOCKET_ERROR)
    {
        // timeout or other error
    }
    else
    {
        for (int i = 0; i < 3; i++)
        {
            if (FD_ISSET (fds[nIndex], &exceptFDS))
            {
                // exception associated with the socket fds[nIndex]
            }
            else if (FD_ISSET (fds[nIndex], &writeFDS))
            {
                // socket fds[nIndex] is ready to send the data
            }
        }
    }
}//if (nMaxFd > 0)

return 0;
}


Output:
Socket count in the FD_SET :3
Return value of select :2 (sometime it comes 1 or 3)

I am not very clear the functionality of select() in case of multiple sockets.
Does it wait for all sockets to be ready OR return as soon as get some of the sockets running ?



-- modified at 0:06 Friday 1st September, 2006
AnswerRe: Problem with select() with multiple sockets Pin
toxcct31-Aug-06 4:17
toxcct31-Aug-06 4:17 
QuestionCAnimateCtrl crashes when going out of scope Pin
almc31-Aug-06 3:46
almc31-Aug-06 3:46 
AnswerRe: CAnimateCtrl crashes when going out of scope Pin
Cedric Moonen31-Aug-06 3:54
Cedric Moonen31-Aug-06 3:54 
GeneralRe: CAnimateCtrl crashes when going out of scope Pin
almc31-Aug-06 3:59
almc31-Aug-06 3:59 
GeneralRe: CAnimateCtrl crashes when going out of scope Pin
David Crow31-Aug-06 7:34
David Crow31-Aug-06 7:34 
GeneralRe: CAnimateCtrl crashes when going out of scope Pin
almc31-Aug-06 23:05
almc31-Aug-06 23:05 
QuestionBackGround Bitmap Pin
radhika2831-Aug-06 3:45
radhika2831-Aug-06 3:45 
AnswerRe: BackGround Bitmap Pin
_AnsHUMAN_ 31-Aug-06 4:03
_AnsHUMAN_ 31-Aug-06 4:03 
AnswerRe: BackGround Bitmap Pin
Hamid_RT31-Aug-06 7:31
Hamid_RT31-Aug-06 7:31 
QuestionQuestion for Foreign Langauge Speakers Pin
Joel Holdsworth31-Aug-06 3:36
Joel Holdsworth31-Aug-06 3:36 
AnswerRe: Question for Foreign Langauge Speakers Pin
toxcct31-Aug-06 3:44
toxcct31-Aug-06 3:44 
AnswerRe: Question for Foreign Langauge Speakers Pin
Rage31-Aug-06 3:45
professionalRage31-Aug-06 3:45 
GeneralRe: Question for Foreign Langauge Speakers Pin
toxcct31-Aug-06 4:09
toxcct31-Aug-06 4:09 
Questionshow text on next line in an edit box Pin
Kiran Pinjala31-Aug-06 2:44
Kiran Pinjala31-Aug-06 2:44 
AnswerRe: show text on next line in an edit box Pin
_AnsHUMAN_ 31-Aug-06 2:51
_AnsHUMAN_ 31-Aug-06 2:51 
QuestionMouse button down Pin
majco33331-Aug-06 2:14
majco33331-Aug-06 2:14 
AnswerRe: Mouse button down Pin
toxcct31-Aug-06 2:26
toxcct31-Aug-06 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.