Jump to content
  • Advertisement
Sign in to follow this  

Winsock Async, Weird Messages

This topic is 4787 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
#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
bool wsock_class::Listen(){
	m_errorcode = bind(m_socket, (SOCKADDR*)&m_sockaddr, sizeof(SOCKADDR));
	if (m_errorcode == SOCKET_ERROR) {
		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);
			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
		m_socket	= NULL;

	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) {
			return Async(wParam,lParam); // 1916

 .......am, LPARAM lParam) { 
	int ierr=WSAGETSELECTERROR(lParam);			// equals 0
	int ievent=WSAGETSELECTEVENT(lParam);		// equals some 'random' number
#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 {
	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

	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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!