Advertisement

Problem with using non-blocking mode!

Started by February 27, 2001 07:29 PM
0 comments, last by kalldrex 23 years, 9 months ago
LOok at my code. When i compile it and it does the recieve command i get the error: INVALID_SOCKET! any ideas?
  
#include <stdio.h>
#include <winsock2.h>
#include <iostream.h>
int main(int argc, char* argv[])
{
	cout <<"The Nonamed Game Server Version 0.1a\nBy:Matthew Shapiro\n\n";
	unsigned long ul=1;
	sockaddr_in me;
	WSADATA wsaData;
	SOCKET s;
	const int max_length=25;
	char namebuf[max_length];
	char passbuf[max_length];
	char username;
	char password;
	int error = 0;
	int nRet;
	if(error = WSAStartup(MAKEWORD(2,0), &wsaData))
	{
		switch(error)
		{
		case WSASYSNOTREADY:
			printf("WSASYSNOTREADY\n");
		case WSAVERNOTSUPPORTED:
			printf("WSAVERNOTSUPPORTED\n");
		case WSAEINPROGRESS:
			printf("WSAEINPROGRESS\n");
		case WSAEPROCLIM:
			printf("WSAEPROCLIM\n");
		case WSAEFAULT:
			printf("WSAEFAULT\n");
	};		return 1;
	}
	s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s == INVALID_SOCKET)
    {
        printf("socket() failed; %d\n", WSAGetLastError());
        return 1;
    }
	nRet = ioctlsocket(s,FIONBIO, (unsigned long *) &ul);
	if (nRet == SOCKET_ERROR)
	{
		cout <<"failed to put socket into nonblocking mode: "<<WSAGetLastError<<''\n'';
		return 1;
	}
	me.sin_family = AF_INET;
	me.sin_port = htons(5151);
	me.sin_addr.s_addr = htonl(INADDR_ANY);
	cout <<"Testing\n";
	if (bind(s, (SOCKADDR *)&me, sizeof(me)) == SOCKET_ERROR)
	{
		cout <<"bind() failed: ";WSAGetLastError();
		return 1;
}

	int err = recv(s,namebuf,25,0);
	if (err==0 || err==INVALID_SOCKET)
	{
		cout <<"Couldn''t recieve username: "<<WSAGetLastError()<<''\n'';
		return 1;
}
	username;
	err = recv(s,passbuf,25,0);
	if (err==0 || err==INVALID_SOCKET)
	{
		cout <<"Couldn''t recieve password: "<<WSAGetLastError()<<''\n'';
		return 1;
}
	cout <<"everything works right now\n";
password;
return 1; 
}
  
ALL YOUR BASE ARE BELONG TO US!!!!
with nonblocking sockets, be sure to check for a WSAEWOULDBLOCK return value.

  int main(int argc, char* argv[]){	cout <<"The Nonamed Game Server Version 0.1a\nBy:Matthew Shapiro\n\n";	unsigned long ul=1;	sockaddr_in me;	WSADATA wsaData;	SOCKET s;	const int max_length=25;	char namebuf[max_length];	char passbuf[max_length];	int error = 0;	int nRet;		if(error = WSAStartup(MAKEWORD(2,0), &wsaData)){		switch(error)			{			case WSASYSNOTREADY:			printf("WSASYSNOTREADY\n");			case WSAVERNOTSUPPORTED:				printf("WSAVERNOTSUPPORTED\n");			case WSAEINPROGRESS:						printf("WSAEINPROGRESS\n");			case WSAEPROCLIM:				printf("WSAEPROCLIM\n");			case WSAEFAULT:					printf("WSAEFAULT\n");		};			return 1;	}	s = socket(AF_INET, SOCK_DGRAM, 0);  	if (s == INVALID_SOCKET){		TCHAR szErr[MAX_PATH];		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, WSAGetLastError(), 0, szErr, MAX_PATH, NULL);		printf("socket() failed: %d\n", szErr);  		return 1; 	}	nRet = ioctlsocket(s,FIONBIO, (unsigned long *) &ul);	if (nRet == SOCKET_ERROR){			TCHAR szErr[MAX_PATH];		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, WSAGetLastError(), 0, szErr, MAX_PATH, NULL);		cout << "failed to put socket into nonblocking mode: " << szErr << ''\n'';		return 1;	}		me.sin_family = AF_INET;	me.sin_port = htons(5151);	me.sin_addr.s_addr = htonl(INADDR_ANY);	cout <<"Testing\n";	if (bind(s, (SOCKADDR *)&me, sizeof(me)) != 0){		TCHAR szErr[MAX_PATH];		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, WSAGetLastError(), 0, szErr, MAX_PATH, NULL);		cout << "bind() failed: " << szErr << ''\n'';		return 1;	}	int err = recv(s,namebuf,25,0);		if(err == 0){		cout << "Socket closed\n";		return 1;	}	if( err == SOCKET_ERROR){		DWORD dwErr = WSAGetLastError();		if(dwErr != WSAEWOULDBLOCK)		{			TCHAR szErr[MAX_PATH];			FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwErr, 0, szErr, MAX_PATH, NULL);			cout << "Couldn''t recieve username: " << szErr << ''\n'';			return 1;		}	}	err = recv(s,passbuf,25,0);	if(err == 0){		cout << "Socket closed\n";		return 1;	}	if(err == SOCKET_ERROR){		DWORD dwErr = WSAGetLastError();		if(dwErr != WSAEWOULDBLOCK)		{			TCHAR szErr[MAX_PATH];			FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwErr, 0, szErr, MAX_PATH, NULL);			cout << "Couldn''t recieve password: " << szErr << ''\n'';			return 1;		}	}		cout <<"everything works right now\n";	return 1;}  

This topic is closed to new replies.

Advertisement