I cannot for the life of me see where i'm going wrong.
I've got a winsock class, the SOCKET is defined in the Header, but when I try to use it in a function I get a first exception crash. It's ground me to a halt (btw, for anyone who saw the other thread, I solved my win32 wrapper problem).

Code:

//Header://

#ifndef _WINSOCKCLASS_H_
#define _WINSOCKCLASS_H_
#define _WINSOCKAPI_ 

#include <WinSock2.h>
#include <String>
#include <Windows.h>

class winSockClass
{
public:
    winSockClass();
    ~winSockClass();
    bool AsyncSelect( HWND lHwnd );
    bool Startup();
    bool wsConnect(std::string URL, int lPort);
    bool SendMsg(std::string message, LPARAM lParam);
    bool Shutdown();

    std::string lServer;
    int lPort;
private:
    SOCKET lSocket;
    SOCKADDR_IN sockAddr;
    WSADATA wsaDat;
    struct hostent *host;
};

#endif _WINSOCKCLASS_H_


// CPP: //
#define _WINSOCKAPI_ 
#include "winSockClass.h"
#include <WinSock2.h>
#include <vector>

#pragma comment(lib, "ws2_32.lib")

winSockClass::winSockClass()
{
    lSocket = NULL;
}

winSockClass::~winSockClass() {}

bool winSockClass::Startup()
{
    WSADATA wsaa;
    int nResult = WSAStartup(MAKEWORD(2,2), &wsaa);
    if(nResult != 0)
    {

        return false;
    }

    lSocket = socket( AF_INET, SOCK_STREAM, 0 );  // <<--- crash here.
    if( lSocket == INVALID_SOCKET )
    {
        return false;
    }

    return true;
}

How about verifying that the Winsock DLL does in fact support 2.2 as indicated below?

int nResult = WSAStartup(MAKEWORD(2,2), &wsaa);
if(nResult != 0)
{
return false;
}

/* Confirm that the WinSock DLL supports 2.2.*/

if (LOBYTE(wsaa.wVersion) != 2 || HIBYTE(wsaa.wVersion) != 2) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        printf("Could not find a usable version of Winsock.dll\n");
        WSACleanup();
        return 1;
    }
    else
        printf("The Winsock 2.2 dll was found okay\n");

lSocket = socket( AF_INET, SOCK_STREAM, 0 ); // <<--- crash here.

Just implemented that, but still no difference.
if I define a SOCKET sock; just above the socket() line and use that instead of lSocket, it's fine, but then I couldn't use sock again in any other function.

I just don't understand why it's having trouble with lSocket. It's defined properly in the class...

This article has been dead for over six months. Start a new discussion instead.