I have a small test program made, and it actually fully works. It connects, and you can send messages back and forth.....but for some reason, when I call connect(), It returns SOCKET_ERROR, but connects, and works fine anyways. Does anyone know why?

here is the code: see lines 160, 161

#include "deskconnect.h"

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  LPSTR lpCmdLine, int nCmdShow)
{ 
	MSG msg;
	WNDCLASSEX cmain;

	cmain.cbSize        = sizeof(WNDCLASSEX);
	cmain.style         = CS_HREDRAW | CS_VREDRAW;
	cmain.lpfnWndProc   = MainProc;
	cmain.cbClsExtra    = 0;
	cmain.cbWndExtra    = 0;
	cmain.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	cmain.hCursor       = LoadCursor(NULL, IDC_ARROW);
	cmain.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	cmain.lpszMenuName  = NULL;
	cmain.lpszClassName = L"deskConnect";
	cmain.hInstance     = hInstance;
	cmain.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	RegisterClassEx(&cmain);

	mWnd = CreateWindow(L"deskConnect", L"Desk Connect", WS_OVERLAPPEDWINDOW, 300, 300, 512, 384, NULL, NULL, hInstance, NULL);

	CreateWindow(L"button", L"Listen", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 5, 5, 80, 25, mWnd, (HMENU)BTN_LISTEN, hInstance, NULL);
	CreateWindow(L"button", L"Connect", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 85, 5, 80, 25, mWnd, (HMENU)BTN_CONNECT, hInstance, NULL);
	CreateWindow(L"button", L"Send", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 165, 5, 80, 25, mWnd, (HMENU)BTN_SEND, hInstance, NULL);
	CreateWindow(L"button", L"Screenshot", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 245, 5, 80, 25, mWnd, (HMENU)BTN_SCREENSHOT, hInstance, NULL);


	if(!mWnd) return 0;
	ShowWindow(mWnd, SW_SHOWNORMAL);
	UpdateWindow(mWnd);
	
	if(!WSAInit())
	{
		MessageBox(mWnd, L"Couldn't initialize winsock", L"Socket error", MB_OK | MB_ICONERROR);
		return 0;
	}
	
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
LRESULT CALLBACK MainProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
		case SOCKET_MESSAGE:
			switch (WSAGETSELECTEVENT(lParam)) //If so, which one is it?
            {
				case FD_ACCEPT:
					//Connection request was made
					memset(&userAddr, 0, sizeof(SOCKADDR));
					tmpSocket = accept(mSocket, (LPSOCKADDR)&userAddr, &sizeAddr);
					//CloseSocket(mSocket);
					mSocket = tmpSocket;
					break;
				case FD_CONNECT:
					//MessageBox(hWnd, L"Connection successful!", L"status", MB_OK);
					//Connection was made successfully
					break;
				case FD_READ:
					//Incoming data; get ready to receive
					memset(buffer, 0, sizeof(buffer));
					recv(mSocket, buffer, sizeof(buffer)-1, 0);
					MessageBoxA(hWnd, buffer, "status", MB_OK);
					break;
				case FD_CLOSE:
					CloseSocket(mSocket);
					//Lost the connection
					MessageBox(hWnd, L"Connection lost!", L"status", MB_OK);
					break;
            }
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
			case BTN_LISTEN:
				CloseSocket(mSocket);
				mSocket = CreateTCPSocket(hWnd);
				Listen(mSocket, 21234);
				break;
			case BTN_CONNECT:
				CloseSocket(mSocket);
				mSocket = CreateTCPSocket(hWnd);
				Connect(mSocket, "127.0.0.1", 21234);
				break;
			case BTN_SEND:
				if(mSocket) send(mSocket, "Hello World!", 12, 0);
				break;
			case BTN_SCREENSHOT:
				//BitBlt(GetWindowDC(hWnd),0,0,300,300,GetWindowDC(GetDesktopWindow()),0,0,SRCCOPY);
				GetScreenBits();
				break;
			}
			break;
		case WM_DESTROY:
			CloseSocket(mSocket);
			WSACleanup();
			PostQuitMessage(WM_QUIT);
			break;
		default:
			return DefWindowProc(hWnd, Msg, wParam, lParam);
		}
    return true;
}
bool WSAInit()
{
	WSADATA wsadata;
	
	if(WSAStartup(SCK_VERSION2, &wsadata) != 0) //0 = success
	{
		return false;
	}
	
	if(wsadata.wVersion != SCK_VERSION2)
	{
		WSACleanup();
		return false;
	}
	return true;
}
SOCKET CreateTCPSocket(HWND hWindow)
{
	if(mSocket)
	{
		EMSG("Socket already opened");
		return 0;
	}

	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(sock == INVALID_SOCKET)
	{
		EMSG("Couldn't create socket.");
		return 0;
	}

	if(WSAAsyncSelect(sock, hWindow, SOCKET_MESSAGE, FD_ACCEPT | FD_CONNECT | FD_READ | FD_CLOSE) == SOCKET_ERROR)
	{
		EMSG("Failed to select window");
		return 0;
	}
	
	return sock;
}
bool Connect(SOCKET sock, char *IPAddress, unsigned int port)
{
	SOCKADDR_IN addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.S_un.S_addr = inet_addr(IPAddress);

	int error = connect(sock, (LPSOCKADDR)&addr, sizeof(SOCKADDR));
	if(error != 0)
	{
		EMSG("Connection failed!"); // I GET ERROR HERE, BUT IT WORKS ANYWAYS ?.?
		iMSG32(WSAGetLastError()); //ALSO, THIS RETURNS 0 ?¿?
		return false;
	}

	return true;
}
bool Listen(SOCKET sock, unsigned int port)
{
	SOCKADDR_IN addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

	if(bind(sock, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
	{
		EMSG("could not bind socket");
		return false;
	}
	
	if(listen(sock, SOMAXCONN) == SOCKET_ERROR)
	{
		EMSG("could not listen");
		return false;
	}

	return true;
}
void CloseSocket(SOCKET sock)
{
	if(sock)  closesocket(sock);
}
BYTE *GetScreenBits()
{
	RECT wRect;
	HWND hDesktop = GetDesktopWindow();
	GetWindowRect(hDesktop, &wRect);
	
	BITMAPINFO bmi;
	memset(&bmi, 0, sizeof(BITMAPINFO));
	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth = wRect.right;
	bmi.bmiHeader.biHeight = wRect.bottom;
	bmi.bmiHeader.biPlanes = 1;
	bmi.bmiHeader.biBitCount = 8;
	bmi.bmiHeader.biCompression = BI_RGB;
	
	BYTE *pixels;

	HDC hdcScreen = GetWindowDC(hDesktop);
	HDC hDC = CreateCompatibleDC(hdcScreen);
	HBITMAP bmSource = CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS, (void**)&pixels, NULL, NULL);
	SelectObject(hDC, bmSource);
	BitBlt(hDC, 0, 0, wRect.right, wRect.bottom, hdcScreen, 0, 0, SRCCOPY);
	BitBlt(GetWindowDC(mWnd), 0, 0, 400, 400, hDC, 0, 0, SRCCOPY);
	DeleteObject(bmSource);
	DeleteDC(hDC);
	ReleaseDC(hDesktop, hdcScreen);
	return pixels;
}

thx =)

solved. Problem was that my MessageBox function was clearing the error, so that it couldn't be retrieved by WSAGetLastError, which needed to be called immediately after the error, which was a non-fatal error 10035, i.e., "hang on a sec" lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.