Click here to Skip to main content
15,905,322 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: simple console input question Pin
rajdawg30-Oct-03 1:08
rajdawg30-Oct-03 1:08 
GeneralLayered Service Provider (LSP) Pin
catmust29-Oct-03 13:55
catmust29-Oct-03 13:55 
GeneralRe: Layered Service Provider (LSP) Pin
Alexander M.,30-Oct-03 2:49
Alexander M.,30-Oct-03 2:49 
GeneralRe: Layered Service Provider (LSP) Pin
catmust30-Oct-03 8:55
catmust30-Oct-03 8:55 
GeneralLoading dll issue during debuging Pin
catmust29-Oct-03 13:48
catmust29-Oct-03 13:48 
Generalmemory allocation issues Pin
Steven M Hunt29-Oct-03 13:21
Steven M Hunt29-Oct-03 13:21 
GeneralRe: memory allocation issues Pin
Christian Graus29-Oct-03 15:52
protectorChristian Graus29-Oct-03 15:52 
Generalsockets and threads Pin
michael_cowan29-Oct-03 13:14
michael_cowan29-Oct-03 13:14 
I am writing an application that open multiple sockets to a server. A number of threads (about 10) are each given a pointer to functions previously resolved from a DLL that handle the sockets.

My code works fine for 1-4 threads, but any more than that and WSAEnumNetworkEvents() returns a socket error. My code is in a DLL and have two global variables SOCKET* pSocket and WSAEVENT* hEvent.
I know that multiple threads cannot safely access the same value, so I made them pointers and each time a thread calls this function the new operator allocates memory to the value. Is this ok?

The code I use for socket connection is show below:
WORD wVersionRequested = MAKEWORD(2,0);
WSADATA wsaData;
int nRet;

nRet = WSAStartup(wVersionRequested, &wsaData);
if(nRet) {
    //AfxMessageBox("WSAStartup()");
    WSACleanup();
    return -1;
}

if(wsaData.wVersion != wVersionRequested) {
    //AfxMessageBox("WinSock version not supported");
    WSACleanup();
    return -1;
}

LPHOSTENT lpHostEntry;

lpHostEntry = gethostbyname(lpServerName);

if(lpHostEntry == NULL) {
    //AfxMessageBox("gethostbyname()");
    WSACleanup();
    return -1;
}

SOCKADDR_IN sa;
sa.sin_family = AF_INET;
sa.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
sa.sin_port = htons(nPort);

pSocket = new SOCKET;
*pSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(*pSocket == INVALID_SOCKET) {
    //AfxMessageBox("socket()");
    WSACleanup();
    return -1;
}

hEvent = new WSAEVENT;
*hEvent = WSACreateEvent();
if(*hEvent == WSA_INVALID_EVENT) {
    AfxMessageBox("WSACreateEvent()");
    closesocket(*pSocket);
    WSACleanup();
    return -1;
}

nRet = WSAEventSelect(*pSocket, *hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
if(nRet == SOCKET_ERROR) {
    AfxMessageBox("EventSelect()");
    closesocket(*pSocket);
    WSACloseEvent(*hEvent);
    WSACleanup();
    return -1;
}

nRet = connect(*pSocket, (LPSOCKADDR)&sa, sizeof(SOCKADDR_IN));
if(nRet == SOCKET_ERROR) {
    nRet = WSAGetLastError();
    if(nRet == WSAEWOULDBLOCK) {
        //AfxMessageBox("Connect would block");
    }
    else {
        //AfxMessageBox("connect()");
        closesocket(*pSocket);
        WSACloseEvent(*hEvent);
        WSACleanup();
        return -1;
    }
}

WSANETWORKEVENTS events;
while(1) {
    DWORD dwRet;
    dwRet = WSAWaitForMultipleEvents(1,
        hEvent,
        FALSE,
        5000,
        FALSE);
    if (dwRet == WSA_WAIT_TIMEOUT) {
        AfxMessageBox("Wait timed out1");
        closesocket(*pSocket);
        WSACloseEvent(*hEvent);
        return -1;
    }

    nRet = WSAEnumNetworkEvents(*pSocket, *hEvent, &events);

    if (nRet == SOCKET_ERROR) {
        AfxMessageBox("WSAEnumNetworkEvents()");
        closesocket(*pSocket);
        WSACloseEvent(*hEvent);
        return -1;
    }

    if (events.lNetworkEvents & FD_CLOSE) {
        AfxMessageBox("closing...");
        closesocket(*pSocket);
        WSACloseEvent(*hEvent);
    }

    if (events.lNetworkEvents & FD_CONNECT) {
        if(events.iErrorCode[FD_CONNECT_BIT]!=0)
            return -1;

        //AfxMessageBox("connected");
        return 0;
    }
}
return 0;


There are 10 types of people in the world
Those who understand binary, and those who don't

GeneralWINS Pin
Jair29-Oct-03 13:06
Jair29-Oct-03 13:06 
GeneralRe: WINS Pin
Jair1-Nov-03 3:29
Jair1-Nov-03 3:29 
QuestionXP GetSysColor()??? Pin
alex.barylski29-Oct-03 12:17
alex.barylski29-Oct-03 12:17 
AnswerRe: XP GetSysColor()??? Pin
michael_cowan29-Oct-03 13:00
michael_cowan29-Oct-03 13:00 
GeneralRe: XP GetSysColor()??? Pin
alex.barylski29-Oct-03 15:17
alex.barylski29-Oct-03 15:17 
GeneralCalling a dialog from a dll question Pin
Steve Messer29-Oct-03 11:53
Steve Messer29-Oct-03 11:53 
GeneralRe: Calling a dialog from a dll question Pin
igor196029-Oct-03 12:49
igor196029-Oct-03 12:49 
GeneralRe: Calling a dialog from a dll question Pin
Steve Messer29-Oct-03 14:35
Steve Messer29-Oct-03 14:35 
GeneralProblem with control arrays Pin
Stephan Poirier29-Oct-03 11:49
Stephan Poirier29-Oct-03 11:49 
GeneralRe: Problem with control arrays Pin
alex.barylski29-Oct-03 12:04
alex.barylski29-Oct-03 12:04 
GeneralRe: Problem with control arrays Pin
Joaquín M López Muñoz29-Oct-03 12:05
Joaquín M López Muñoz29-Oct-03 12:05 
GeneralRe: Problem with control arrays Pin
Stephan Poirier29-Oct-03 12:43
Stephan Poirier29-Oct-03 12:43 
GeneralRe: Problem with control arrays Pin
Stephan Poirier29-Oct-03 13:01
Stephan Poirier29-Oct-03 13:01 
GeneralATL 7 collection class Pin
Urban Olars29-Oct-03 10:45
Urban Olars29-Oct-03 10:45 
QuestionCan someone clarify some Windows' terms for me Pin
mwhannan29-Oct-03 10:38
mwhannan29-Oct-03 10:38 
AnswerRe: Can someone clarify some Windows' terms for me Pin
alex.barylski29-Oct-03 11:07
alex.barylski29-Oct-03 11:07 
GeneralRe: Can someone clarify some Windows' terms for me Pin
Blake Coverett29-Oct-03 11:12
Blake Coverett29-Oct-03 11:12 

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.