Click here to Skip to main content
15,921,716 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalset thumbnail view for Cfiledialog Pin
vancouver77729-Oct-03 16:30
vancouver77729-Oct-03 16:30 
QuestionHow can a CAB get parameter from html ? Pin
suninwater29-Oct-03 16:21
suninwater29-Oct-03 16:21 
GeneralRe: Class Constructor Help Pin
Andrew Walker29-Oct-03 16:49
Andrew Walker29-Oct-03 16:49 
GeneralChanging CListBox sort at runtime Pin
Daniel132429-Oct-03 15:39
Daniel132429-Oct-03 15:39 
GeneralRe: Changing CListBox sort at runtime Pin
vcplusplus29-Oct-03 15:45
vcplusplus29-Oct-03 15:45 
GeneralRe: Changing CListBox sort at runtime Pin
Daniel132429-Oct-03 16:02
Daniel132429-Oct-03 16:02 
GeneralRe: Changing CListBox sort at runtime Pin
vcplusplus29-Oct-03 16:11
vcplusplus29-Oct-03 16:11 
Generalarray of Strings Pin
convert_sg29-Oct-03 15:14
convert_sg29-Oct-03 15:14 
GeneralRe: array of Strings Pin
Navin29-Oct-03 15:30
Navin29-Oct-03 15:30 
GeneralRe: array of Strings Pin
convert_sg29-Oct-03 15:48
convert_sg29-Oct-03 15:48 
GeneralRe: array of Strings Pin
vcplusplus29-Oct-03 15:39
vcplusplus29-Oct-03 15:39 
GeneralRe: array of Strings Pin
Christian Graus29-Oct-03 15:48
protectorChristian Graus29-Oct-03 15:48 
GeneralChange font size for CComboBox and CEdit Pin
srev29-Oct-03 14:14
srev29-Oct-03 14:14 
GeneralRe: Change font size for CComboBox and CEdit Pin
Yonatan26-Nov-03 2:04
Yonatan26-Nov-03 2:04 
Generalsimple console input question Pin
rajdawg29-Oct-03 14:09
rajdawg29-Oct-03 14:09 
GeneralRe: simple console input question Pin
Christian Graus29-Oct-03 15:23
protectorChristian Graus29-Oct-03 15:23 
GeneralRe: simple console input question Pin
Christian Graus29-Oct-03 15:47
protectorChristian Graus29-Oct-03 15:47 
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

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.