Click here to Skip to main content
15,911,531 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Converting TCHAR to LPSTR to be used in CreateProcess Pin
Rajesh R Subramanian28-Jan-10 1:04
professionalRajesh R Subramanian28-Jan-10 1:04 
GeneralRe: Converting TCHAR to LPSTR to be used in CreateProcess Pin
lazy_panther28-Jan-10 1:30
lazy_panther28-Jan-10 1:30 
GeneralRe: Converting TCHAR to LPSTR to be used in CreateProcess Pin
Rajesh R Subramanian28-Jan-10 2:05
professionalRajesh R Subramanian28-Jan-10 2:05 
AnswerRe: Converting TCHAR to LPSTR to be used in CreateProcess Pin
KingsGambit28-Jan-10 1:06
KingsGambit28-Jan-10 1:06 
AnswerRe: Converting TCHAR to LPSTR to be used in CreateProcess Pin
Richard MacCutchan28-Jan-10 1:47
mveRichard MacCutchan28-Jan-10 1:47 
QuestionAbout select() in multithread Pin
luderjane28-Jan-10 0:10
luderjane28-Jan-10 0:10 
QuestionRe: About select() in multithread Pin
Moak28-Jan-10 8:00
Moak28-Jan-10 8:00 
AnswerRe: About select() in multithread Pin
luderjane1-Feb-10 20:16
luderjane1-Feb-10 20:16 
I wrote a simple testbed to exclude other factor in my program as follows(builds in VC2005, must depends on Ws2_32.lib):

#include <stdio.h>
#include <tchar.h>
#include <Winsock2.h>

typedef struct ServerInfo {
    char sIP[32];
    int iPort;
    DWORD dwTimeout;
    BOOL bPrintable;
} ServerInfo, *pServerInfo;

DWORD WINAPI OpenThreadProc(void* lpParam);
void BlockingOpen(pServerInfo pServer);
void NonBlockingOpen(pServerInfo pServer);

int _tmain(int argc, _TCHAR* argv[])
{
    int iNumOfURServer = 0;
    char sIP[32];
    int iPort = 80;
    DWORD dwTimeout = 5000;
    
    printf("Number of unreachable server: ");
    scanf("%d", &iNumOfURServer);
    printf("Reachable server IP: ");
    scanf("%s", sIP);
    printf("Reachable server port: ");
    scanf("%d", &iPort);
    printf("Timeout(in millisecond, 0 indicates blocking mode): ");
    scanf("%u", &dwTimeout);

    WSADATA wsaData;
    WORD version = MAKEWORD(2,2);
    WSAStartup(version, &wsaData);

    pServerInfo ServerArray = new ServerInfo[iNumOfURServer+1];
    char sURIP[32];

    for (int i = 0; i < iNumOfURServer; i++)
    {
        sprintf(sURIP, "192.168.1.1%02d", i);
        strcpy_s(ServerArray[i].sIP, sURIP);
        ServerArray[i].iPort = 3456;
        ServerArray[i].dwTimeout = dwTimeout;
        ServerArray[i].bPrintable = FALSE;

        CreateThread(NULL, 0, OpenThreadProc, &ServerArray[i], 0, NULL);
    }

    strcpy_s(ServerArray[iNumOfURServer].sIP, sIP);
    ServerArray[iNumOfURServer].iPort = iPort;
    ServerArray[iNumOfURServer].dwTimeout = dwTimeout;
    ServerArray[iNumOfURServer].bPrintable = TRUE;
    CreateThread(NULL, 0, OpenThreadProc, &ServerArray[iNumOfURServer], 0, NULL);

    scanf("%d", &iPort);

    return 0;
}

DWORD WINAPI OpenThreadProc(void* lpParam)
{
    pServerInfo pServer = (pServerInfo)lpParam;
    
    while (true)
    {
        if (pServer->dwTimeout == 0)
        {
            BlockingOpen(pServer);
        } 
        else
        {
            NonBlockingOpen(pServer);
        }
        
        Sleep(1000);
    }

    return 0;
}

void BlockingOpen(pServerInfo pServer)
{
    SOCKET theSocket;
    int nRet;

    //
    // Store information about the server
    //
    LPHOSTENT lpHostEntry;

    lpHostEntry = gethostbyname(pServer->sIP);		// Specifying server by its name
    if (lpHostEntry == NULL) {
       return;
    }

    //
    // Create the socket
    //
    theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (theSocket == INVALID_SOCKET) {
        return;
    }

    //
    // Use SOCKADDR_IN to fill in address information
    //
    SOCKADDR_IN saServer;

    saServer.sin_family = AF_INET;
    saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
    // ^ Address of the server being inserted into the address field
    saServer.sin_port = htons(pServer->iPort);

    //
    // Connect to the server
    //
    DWORD dwStartTime = GetTickCount();
    nRet = connect(theSocket,
        (LPSOCKADDR)&saServer,		// Server address
        sizeof(struct sockaddr));	// Length of address structure
    if (pServer->bPrintable)
    {
        if (nRet == SOCKET_ERROR) 
        {
            printf("%s:%d blocking open failed takes %d ms\n", pServer->sIP, pServer->iPort, 
                      GetTickCount()-dwStartTime);
        }
        else
        {
            printf("%s:%d blocking open successful takes %d ms\n", pServer->sIP, pServer->iPort,  
                      GetTickCount()-dwStartTime);
        }
    }

    closesocket(theSocket);
}

void NonBlockingOpen(pServerInfo pServer)
{
    SOCKET theSocket;
    int nRet;
    //
    // Store information about the server
    //
    LPHOSTENT lpHostEntry;

    lpHostEntry = gethostbyname(pServer->sIP);		// Specifying server by its name
    if (lpHostEntry == NULL) {
        return;
    }

    //
    // Create the socket
    //
    theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (theSocket == INVALID_SOCKET) {
        return;
    }

    // Set socket to non-blocking mode
    //
    unsigned long ul = 1;
    nRet = ioctlsocket(theSocket, FIONBIO, (unsigned long*)&ul);
    if (nRet == SOCKET_ERROR) {
        closesocket(theSocket);
        return;
    }

    // Use SOCKADDR_IN to fill in address information
    //
    SOCKADDR_IN saServer;

    saServer.sin_family = AF_INET;
    saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
    // ^ Address of the server being inserted into the address field
    saServer.sin_port = htons(pServer->iPort);

    // Connect to the server
    //
    nRet = connect(theSocket,
        (LPSOCKADDR)&saServer,		// Server address
        sizeof(struct sockaddr));	// Length of address structure

    if (nRet == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) {
        closesocket(theSocket);
        return;
    }

    struct timeval timeout;
    fd_set r;

    FD_ZERO(&r);
    FD_SET(theSocket, &r);
    timeout.tv_sec = pServer->dwTimeout / 1000;
    timeout.tv_usec = (pServer->dwTimeout % 1000) * 1000;
    DWORD dwStartTime = GetTickCount();
    nRet = select(0, 0, &r, 0, &timeout);
    if (pServer->bPrintable)
    {
        if ( nRet <= 0 ) 
        {
            printf("%s:%d non-blocking open failed takes %d ms\n", pServer->sIP, pServer->iPort, 
                      GetTickCount()-dwStartTime);
        }
        else
        {
            printf("%s:%d non-blocking open successful takes %d ms\n", pServer->sIP, 
                      pServer->iPort, GetTickCount()-dwStartTime);
        }
    }

    closesocket(theSocket);
}


This testbed simply creates multiple thread which opens a socket repeatedly.

There is only one reachable server and the rest are unreachable.

When I use non-blocking mode(timeout = 5000) and 2 unreachable servers, the open procedure takes 0 ms at most of time.

If I gradually increase the number of unreachable server and keep the same timeout, the open procedure takes more and more time.

In the end(about 16 unreachable servers), the open procedure failed every time(except first 5 times).

Even in blocking mode(timeout = 0), the opening time consuming will increase with the number of unreachable server.

There seems to be a constraint on the socket connection in multithread.

But I cannot google any official document about it...
QuestionPainting in Win32, MFC and WPF ? Pin
Sameerkumar Namdeo27-Jan-10 23:19
Sameerkumar Namdeo27-Jan-10 23:19 
AnswerRe: Painting in Win32, MFC and WPF ? Pin
KingsGambit28-Jan-10 0:06
KingsGambit28-Jan-10 0:06 
QuestionCDocTemplate labelling a file yesAlreadyOpen after a document close Pin
maycockt27-Jan-10 23:02
maycockt27-Jan-10 23:02 
AnswerRe: CDocTemplate labelling a file yesAlreadyOpen after a document close Pin
Richard MacCutchan28-Jan-10 1:51
mveRichard MacCutchan28-Jan-10 1:51 
AnswerRe: CDocTemplate labelling a file yesAlreadyOpen after a document close Pin
krmed28-Jan-10 2:47
krmed28-Jan-10 2:47 
QuestionProblem in loading custom menu Pin
Anu_Bala27-Jan-10 21:31
Anu_Bala27-Jan-10 21:31 
AnswerRe: Problem in loading custom menu Pin
KingsGambit28-Jan-10 0:09
KingsGambit28-Jan-10 0:09 
AnswerRe: Problem in loading custom menu Pin
krmed28-Jan-10 2:32
krmed28-Jan-10 2:32 
QuestionDeselecting Tree Node Item in MFC Pin
RS.Ratheesh27-Jan-10 19:53
RS.Ratheesh27-Jan-10 19:53 
QuestionRe: Deselecting Tree Node Item in MFC Pin
David Crow28-Jan-10 3:42
David Crow28-Jan-10 3:42 
QuestionHow to use library files. Pin
Paulraj G27-Jan-10 19:52
Paulraj G27-Jan-10 19:52 
AnswerRe: How to use library files. Pin
CPallini27-Jan-10 21:04
mveCPallini27-Jan-10 21:04 
JokeRe: How to use library files. PinPopular
Nelek27-Jan-10 21:12
protectorNelek27-Jan-10 21:12 
GeneralRe: How to use library files. Pin
CPallini27-Jan-10 21:32
mveCPallini27-Jan-10 21:32 
JokeRe: How to use library files. Pin
Cedric Moonen27-Jan-10 21:47
Cedric Moonen27-Jan-10 21:47 
GeneralRe: How to use library files. Pin
Paulraj G27-Jan-10 21:50
Paulraj G27-Jan-10 21:50 
QuestionRe: How to use library files. Pin
CPallini27-Jan-10 22:14
mveCPallini27-Jan-10 22:14 

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.