Advertisement

Winsock Async, Weird Messages

Started by November 05, 2005 11:23 AM
0 comments, last by Gng 19 years, 3 months ago
Basicly the problem is that when i tell windows to send all FD_ACCEPT to a certain window, windows starts sending 'random' numbers, but FD_ACCEPT is never encountered. What could the problem be? Code below.. ---------------------------------------------- Window Proc must include: case SM_CONNECT: break; Usage: wsock_class x; x.SetWindow(hwnd); // Pass in your window HWND for the message handler. x.Listen(); Problem: SM_CONNECT is encountered, but WSAGETSELECTEVENT(lParam) returns an invalid result. Usually numbers in the range of 1900-1999, 3100-3199, 3900-3999 The numbers are not random, they change on reboot, but may return to their original state after a few reboots. A reboot just seem to 'change' the numbers, to a predefined set.. Iv also encountered messages when the socket has been written to, even though the socket has not yet been accept()'ed the High Word of lParam always contain 0. .cpp

#ifndef _NETWORK__CLASS_CPP_
#define _NETWORK__CLASS_CPP_
#include "network class.h"

bool wsock_class::SetWindow(HWND hWindow) {
	char m_buf[50];
	m_errorcode = WSAAsyncSelect(m_socket, hWindow, SM_CONNECT, FD_ACCEPT);
	if (m_errorcode == SOCKET_ERROR) {		//if (WSAAsyncSelect(m_socket, hWindow, SM_ASYNC, d_socketFlags) == SOCKET_ERROR) {
		m_errorcode = WSAGetLastError();
		if (m_errorcode == WSAEINVAL) // Probably lacking hwnd
			sprintf_s(m_buf,50,"Invalid Argument supplied.");
		else  // unknown
			sprintf_s(m_buf,50,"Error code: %d", m_errorcode);

		MessageBox(NULL,m_buf,"WSAAyncSelect has failed!", MB_OK | MB_ICONEXCLAMATION);
			return false;
	}
	return true;
}
wsock_class::~wsock_class() { Cleanup(); }

wsock_class::wsock_class() {
	m_port = DEF_PORT;						// default port
	m_socket = NULL;						// no socket
	Initiate();
}
bool wsock_class::Listen(){
	m_errorcode = bind(m_socket, (SOCKADDR*)&m_sockaddr, sizeof(SOCKADDR));
	if (m_errorcode == SOCKET_ERROR) {
		closesocket(m_socket);
		char buf[128];
		if (m_errorcode == WSAEADDRINUSE)
			sprintf_s(buf,128,"Cannot bind to ip 'ANY' port '%u'.\nError: IP:PORT already in use!",m_port);
		else
			sprintf_s(buf,128,"Cannot bind to ip 'ANY' port '%u'.\nError: %e",m_port,WSAGetLastError());

		MessageBox(NULL,buf,"Bind() has failed!", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}
	m_errorcode = listen(m_socket,MAX_SOCKETS);
	if(m_errorcode==SOCKET_ERROR) {}
	return true;
}
bool wsock_class::Cleanup() {
	if (m_socket != NULL) {
		// shutdown(m_socket, FD_SEND);
		// read
		closesocket(m_socket);
		m_socket	= NULL;
	}

	WSACleanup();
	return true;
}
bool wsock_class::Initiate() {
	m_errorcode = WSAStartup(MAKEWORD(2,0),&m_wsa);
	if (m_errorcode == SOCKET_ERROR) {
		MessageBox(NULL,"Cannot initialize winsock 2.0","WSAStartup has failed!", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	m_sockaddr.sin_port			= htons(m_port);		// Port
	m_sockaddr.sin_family		= AF_INET;				// Internet Protocol
	m_sockaddr.sin_addr.s_addr	= INADDR_ANY;			// No IP on bind()
	m_socket	= socket(AF_INET,SOCK_STREAM,0);		// Listen type TCP/IP
	return true;
}

/*LRESULT CALLBACK wsock_class::WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
	switch (Msg) {
		case SM_CONNECT:	// FD_ACCEPT
			return Async(wParam,lParam); // 1916
			break;
.........

 .......am, LPARAM lParam) { 
	int ierr=WSAGETSELECTERROR(lParam);			// equals 0
	int ievent=WSAGETSELECTEVENT(lParam);		// equals some 'random' number
.......}
		*/
#endif
.h

#ifndef _NETWORK__CLASS_H_
#define _NETWORK__CLASS_H_
#include <winsock2.h>						// winsock2.h
#include <windows.h>						// windows.h
#define SM_CONNECT WM_USER+2				// WndProc Identifier
#define MAX_SOCKETS 5						// Max users/sockets
#define DEF_PORT 300						// Default port for default constructor

class wsock_class { //: public cGUI {
public:
	wsock_class();							// constructor
	virtual ~wsock_class();					// deconstructor

	bool SetWindow(HWND hWindow);
	bool Initiate();						// initiate winsock/etc
	bool Cleanup();							// clean up the mess
	bool Listen();							// listen to port 'x' on all ip's

protected:
	SOCKET m_socket;						// listen socket
	SOCKADDR_IN m_sockaddr;					// listen socket's sockaddr
	WSADATA m_wsa;							// for Wsastartup

	int m_errorcode;						// whatever error code encountered
	int m_port;								// port to bind to
};
#endif
Not winsock related.

(LOWORD(lParam) & FD_ACCEPT) works fine.

This topic is closed to new replies.

Advertisement