DeeperShade 4 Newbie Poster

Yes, me again, here to prove i'm an idiot. :)

Ok, all previous problems solved (wrapping win32, and getting winsock to actually do ANYTHING)
My problem now is that recv is very random. Sometimes it'll recieve data, sometimes it'll return -1. WSAGetLastError (still not entirely certain I should be using that for this but still), switches between 10038 and 10035 error codes.

Google's not being particularly helpful so I was wondering if someone could take a look at tell me what i'm doing wrong?

winClude.cpp:

#define _WINSOCKAPI_ 
#include <WinSock2.h>
#include <Windows.h>
#include <string>
#include <stdint.h>
#include <fstream>
#include "winClude.h"
using namespace std;

winBase::winBase( HINSTANCE hinst, int showCommand, string title )
{
    hwnd = NULL;
    hinstance = hinst;

    setWinTitle(title);
    className = title;
}

winBase::~winBase(){}

bool winBase::create()
{
    WNDCLASSEX wcx; 

    wcx.cbSize = sizeof(WNDCLASSEX);                            
    wcx.style = CS_HREDRAW | CS_VREDRAW;                         
    wcx.lpfnWndProc = winBase::messageRouter;                    
    wcx.cbClsExtra = 0;                                         
    wcx.cbWndExtra = 0;                                         
    wcx.hInstance = hinstance;                                  
    wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);                
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);                  
    wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);    
    wcx.lpszMenuName = NULL;                                    
    wcx.lpszClassName = className.c_str();                       
    wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);              

    if(!RegisterClassEx(&wcx))
        return false;

    int32_t winLeft;
    int32_t winTop;
    int32_t screenWidth = 800;
    int32_t screenHeight = 600;

    winLeft = (GetSystemMetrics(SM_CXSCREEN) / 2) - (screenWidth / 2);
    winTop = (GetSystemMetrics(SM_CYSCREEN) / 2) - (screenHeight / 2);

    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, className.c_str(), windowTitle.c_str(), WS_OVERLAPPEDWINDOW,
        winLeft, winTop, screenWidth, screenHeight, 
        NULL, NULL, hinstance, (void *)this);
    if(hwnd == NULL)
        return 0;

    ShowWindow(hwnd, ShowCommand);
    UpdateWindow(hwnd);

    return (hwnd != NULL);
}


LRESULT CALLBACK winBase::messageRouter(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    winBase * Wnd;
    if (msg == WM_NCCREATE)
    {
        SetWindowLong(hwnd, GWL_USERDATA, (long)((LPCREATESTRUCT(lparam))->lpCreateParams));
    }

    Wnd = GetObjectFromWin(hwnd);

    if (Wnd)
        return Wnd->messageHandler(hwnd, msg, wparam, lparam);
    else
        return DefWindowProc(hwnd, msg, wparam, lparam);
}

window::window( HINSTANCE hinst, int showCommand, string title ) : winBase( hinst, showCommand, title )
{
    bool success = false;
    ShowCommand = showCommand;
    success = create();

    active = true;
}

window::~window(){}

LRESULT CALLBACK window::messageHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    string output;

    switch (msg)
    {
    case WM_CREATE:
        break;

    case WM_KEYDOWN:
    {
        switch(wparam)
        {
            case VK_ESCAPE:
            {
                active = false;
                ::PostQuitMessage(0);
                return 0;
            }
            case VK_F1:
            {
                SOCKET ls;
                string input;

                input = "GET /index.php HTTP/1.1\r\n";
                input += "Host: www.yunaco.com\r\n";
                input += "Connection: close\r\n";
                input += "\r\n";

                wsa.Startup();
                wsa.Socket(ls);
                wsa.AsyncSelect(ls, hwnd);
                wsa.Connect(ls, "216.239.138.52", 80);
                wsa.SendMsg(ls, input, lparam);
                //Sleep(100);
                wsa.Receive(ls, output);
                wsa.KillSwitch(ls);
            }
        }
        break;
    }
    case WM_LBUTTONDOWN:
        break;

    case WM_LBUTTONUP:
        break;

    case WM_RBUTTONDOWN:
        break;

    case WM_RBUTTONUP:
        break;

    case WM_MOUSEMOVE:
        break;

    case WM_PAINT:
        break;

    case WM_DESTROY:
        active = false;
        ::PostQuitMessage(0);
        return 0;
    default:
        return DefWindowProc(hwnd, msg, wparam, lparam);
    }
    return 0;
}

bool window::msgPump()
{
    static MSG Msg;

    if (PeekMessage(&Msg, hwnd, 0, 0, PM_REMOVE))
    {
        if (Msg.message == WM_QUIT)
            return false;

        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    return true;
}


int window::run()
{
    while(active)
    {
        if(!msgPump())
            active = false;
    }

    return 0;
}

winSockClass.h:

#ifndef _WINSOCKCLASS_H_
#define _WINSOCKCLASS_H_
#define _WINSOCKAPI_ 

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

class winSockClass
{
public:
    winSockClass();
    ~winSockClass();
    bool AsyncSelect( SOCKET &localSocket, HWND lHwnd );
    bool Startup();
    bool Socket( SOCKET &localSocket );
    bool Connect( SOCKET &localSocket, std::string URL, int lPort );
    bool SendMsg( SOCKET &localSocket, std::string message, LPARAM lParam );
    bool Receive( SOCKET &localSocket, std::string buffer );
    SOCKET Accept( SOCKET &localSocket );
    bool shutdown( SOCKET &localSocket );
    bool KillSwitch( SOCKET &localSocket );

    std::string lServer;
    int lPort;

private:
    SOCKADDR_IN sockAddr;
    WSADATA wsaDat;
    struct hostent *host;
};

#endif _WINSOCKCLASS_H_

winSockClass.cpp:

#define _WINSOCKAPI_ 
#include "winSockClass.h"
#include <WinSock2.h>
#include <vector>
#include <fstream>
#include <string>

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

winSockClass::winSockClass()
{

}

winSockClass::~winSockClass() {}

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

    if ( LOBYTE( wsaa.wVersion ) != 2 || HIBYTE( wsaa.wVersion ) != 2 ) 
    {
        WSACleanup();
        return false;
    }

    return true;
}

bool winSockClass::Socket( SOCKET &localSocket )
{
    localSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    if( localSocket == INVALID_SOCKET )
    {
        return false;
    }

    return true;
}

bool winSockClass::Connect( SOCKET &localSocket, std::string URL, int lPort )
{
    if( ( host = ( hostent * )gethostbyname( URL.c_str() ) ) == NULL )
    {
        return false;
    }

    sockAddr.sin_port=htons( lPort );
    sockAddr.sin_family=AF_INET;        
    sockAddr.sin_addr.s_addr=*( ( unsigned long* )host->h_addr );

    connect( localSocket, (SOCKADDR*) &sockAddr, sizeof( sockAddr ) );

    return true;
}

bool winSockClass::AsyncSelect( SOCKET &localSocket, HWND lHwnd )
{
    int nResult = WSAAsyncSelect( localSocket, lHwnd, 104, ( FD_CLOSE | FD_READ ) );
    if ( nResult )
    {
        return false;
    }

    return true;
}

bool winSockClass::SendMsg( SOCKET &localSocket, std::string message, LPARAM lParam )
{
    int a = send( localSocket, message.c_str(), message.size(), 0 );

    return true;
}

bool winSockClass::shutdown( SOCKET &localSocket )
{
    shutdown( localSocket );
    return true;
}

bool winSockClass::KillSwitch( SOCKET &localSocket )
{
    if( localSocket != NULL )
    {
        closesocket( localSocket );
        return true;
    }
    else return false;

    return true;
}

bool winSockClass::Receive( SOCKET &localSocket, std::string buffer )
{
    std::ofstream out ("c:\\test.txt", std::ios::out | std::ios::ate);
    std::string recvstore;
    int lResult;
    char rec[1024 * 4];

    do
    {
        lResult = recv( localSocket, rec, strlen(rec), 0 );

        if(lResult > 0)
        {
            recvstore.append(rec);
            recvstore.resize(lResult);
        }
        else if(lResult == 0)
            int a = 1;
        else 
        {
            out << WSAGetLastError();
        }

    } while (lResult > 0);

    out << recvstore;
    out.close();

    return true;
}

SOCKET winSockClass::Accept( SOCKET &localSocket )
{
    SOCKET ls;
    ls = accept( localSocket, ( LPSOCKADDR )( &sockAddr ), ( int * )sizeof( sockaddr ) );

    return ls;
}