Random results with recv() on WinSock

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    
#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;
}
The error 10035 is nothing to worry about, that one basically means that the socket is busy so give it a second before you try again. 10038 on the other hand usually means that you are trying to receive data from a socket that's already been closed. After you break a connection between two points you need to reestablish it all over again with accept, you can't try to listen on the same socket again.
Right, ok, i'll try and implement an error handling into it and see if that solves it.
I did test it with a check that if WSAGetLastError was 10035, then Sleep(100) and try again, but the think just hung, constantly looping.
Topic archived. No new replies allowed.