Skip to main content
gamedev.net
🔒 Locked

INVALID_SOCKET

Started by Tac-Tics Dec 29, 2002 at 2:43 AM 6 replies 450+ views
Original Post
Tac-Tics
Tac-Tics
Hi. I just picked up a book on networking and I''m trying to get *something* to go across my NIC. However, upon closer inspection, I''m getting an INVALID_SOCKET with all my socket(...) calls:
  
int SetupServer()
{
	LPWSADATA wsaData = NULL;
		
	SOCKET listenSock;
	SOCKET connectionSock;

	SOCKADDR_IN serverAddr;
	SOCKADDR_IN clientAddr;

	int port = 5150;
	
	char data[32];	

	// INIT WINSOCK

	if (! WSAStartup(MAKEWORD(2, 2), wsaData) )
	{
		printf("Couldn\''t init WinSock\n");
		return -1;
	}

	printf("WinSock init\''ed\n");

	// INIT LISTEN SOCKET

	listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (listenSock == WSAEWOULDBLOCK)
	{
		printf("This isn''t blocking....!\n");
		return -1;
	}
	else
	if (listenSock == INVALID_SOCKET)
	{
		printf("Ah crums. listenSock is broke T.t \n");
		return -1;
	}
.
.
.
.

  
Any clues as to what I''m doing wrong? Help == Thnx "You TK''ed my chicken!"
Zern
Zern
Granted I am no Winsock guru however ... I see a flaw in your code ...


// INIT WINSOCK
if (! WSAStartup(MAKEWORD(2, 2), wsaData) )
{
printf("Couldn\'t init WinSock\n");
return -1;
}


WSAStartup returns 0 if it is successful. !0 = True ... so you should do an equality test ... if( WSAStartup(..) != 0 ) { //code error return blah blah }


next if you do get past that stage ... and listenSock is still equal to INVALID_SOCKET ... use WSAGetLastError() to output the error ...

char szTemp[50];
sprintf( szTemp, "Error: %d", WSAGetLastError() );
printf( szTemp );


if you don't know what the error code means ... post what is outputted and I'll respond with what it means.


~

[edited by - Zern on December 29, 2002 3:53:05 AM]
Tac-Tics
Tac-Tics
Yeah, I just figured that out too. However, I got a "WSA not initialised" error. I''m still not sure where I''m going wrong (besides that flaw I had).

"You TK''ed my chicken!"
siaspete
siaspete
Try not passing a LPWSADATA into WSAStartup, but an address of a WSADATA structure.

WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
// error
}

Edit. You should check MSDN for this kind of thing, it even has an example.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions


[edited by - siaspete on December 29, 2002 4:19:36 AM]
Tac-Tics
Tac-Tics
>> Try not passing a LPWSADATA into WSAStartup, but an address of a WSADATA structure.

Shouldn''t the address of (ie: &) a WSADATA struct be the same as a LPWSADATA (ie: WSADATA*) anyway? MSDN even says it takes an LPWSADATA as it''s 2nd param.

I changed it exactly as you said and it still gives me the same error:


  
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WinSock couldn\''t init\n");
return -1;
}


>> Edit. You should check MSDN for this kind of thing, it even has an example.

You don''t think I have every resource I own in front of me right now? I have multiple windows open to MSDN Library and both books on own on WinSock open right now and I still can''t figure this out =-( I''m sure it''s something really obvious too.

"You TK''ed my chicken!"
Zern
Zern
>>Shouldn''t the address of (ie: &) a WSADATA struct be the same as a LPWSADATA (ie: WSADATA*) anyway? MSDN even says it takes an LPWSADATA as it''s 2nd param.

Correct...


What I''m oddly thinking now is either you have a serious problem or simply you don''t have winsockv2.2 installed on your machine ( Why not? shrug ;P )


Try this out to be sure it is not just v2.2 that is causing you this issue ...



if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
{
printf("WinSock couldn\''t init\n");
return -1;
}


.. if that raises some issues with you also ... Then you have a bigger problem ><

If you would like instant responces you are more than welcome to IM me on AIM: ib etear ~
Tac-Tics
Tac-Tics
Hrmm I think the GameDev server error when I tried to post my last replay about an hour ago. Heh, I figured out my problem. When I first created my project, I changed the settings to output the *.exe file by a different name. However, my anti-virus launched it''s weekly reign of terror and it froze my computer (I hate my anti-virus soft). When I restarted, my settings had not been saved and so when I changed things in my code, it appeared they were not working (the first time, I think it *was* because of the LPWSADATA vs &WSADATA issue). But all is good now and all I have to worry about is the fact I don''t know much of anything else when it comes to this stuff >_< Heh, I guess that''s why I got a book on it. Thanks for everyone''s help. Ato de ne.

"You TK''ed my chicken!"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.