🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Winsock does not like me =(

Started by
10 comments, last by kalldrex 23 years, 4 months ago
okay well i think the problem is my computer doesn''t like winscok to actully work! here''s the code i''m working with (not mine) #include #include #include #define RPSS_NUMOFUSERS 0x03 #define RPSS_STARTGAME 0x04 #define RPSS_SCISSOR 0x05 #define RPSS_ROCK 0x06 #define RPSS_PAPER 0x07 #define RPSS_QUIT 0x08 void main ( void ) { SOCKET s[3]; sockaddr_in me; sockaddr you[2]; int addr_size = sizeof (sockaddr); int num_players = 0; cout << "Rock, Paper, Scissor, Shoot! Advanced Server\n"; cout << "By Stefan Hajnoczi\n\n"; WSADATA w; int error = WSAStartup (0x0202,&w); if (error) { cout << "Error: You need WinSock 2.2!\n"; return; } if (w.wVersion!=0x0202) { cout << "Error: Wrong WinSock version!\n"; WSACleanup (); return; } s[0] = socket (AF_INET,SOCK_STREAM,0); me.sin_family = AF_INET; me.sin_port = htons (5555); me.sin_addr.s_addr = htonl (INADDR_ANY); if (bind(s[0],(LPSOCKADDR)&me,sizeof(me))==SOCKET_ERROR) { cout << "Error: Unable to bind socket!\n"; WSACleanup (); return; } if (listen(s[0],1)==SOCKET_ERROR) { cout << "Error: Unable to listen!\n"; WSACleanup (); return; } cout << "Listening for connections...\n"; while (num_players<2) { s[num_players+1] = accept (s[0],&you[num_players],&addr_size); if (s[num_players+1]==INVALID_SOCKET) { cout << "Error: Unable to accept connection!\n"; WSACleanup (); return; } else { cout << "Player joined!\n"; char buffer[2]; sprintf (buffer,"%c%d",RPSS_NUMOFUSERS,num_players+1); send (s[num_players+1],buffer,2,0); num_players++; } } cout << "Starting Game!\n"; closesocket (s[0]); char buffer[2]; sprintf (buffer,"%c%d",RPSS_STARTGAME,0); error = send (s[1],buffer,2,0); if ((error==0)||(error==SOCKET_ERROR)) { cout << "Error: Player 1 quit!\n"; WSACleanup (); return; } error = send (s[2],buffer,2,0); if ((error==0)||(error==SOCKET_ERROR)) { cout << "Error: Player 2 quit!\n"; WSACleanup (); return; } char player1 = 0; char player2 = 0; while (true) { int error = recv (s[1],buffer,2,0); if ((error==0)||(error==INVALID_SOCKET)) { cout << "Error: Player 1 quit!\n"; closesocket (s[2]); WSACleanup (); return; } if ((buffer[0]==RPSS_ROCK)||(buffer[0]==RPSS_SCISSOR)||(buffer[0]==RPSS_PAPER)) { player1 = buffer[0]; } if (buffer[0]==RPSS_QUIT) { sprintf (buffer,"%c%d",RPSS_QUIT,0); send (s[2],buffer,2,0); closesocket (s[1]); closesocket (s[2]); WSACleanup (); cout << "Player 1 quit\n"; return; } error = recv (s[2],buffer,2,0); if ((error==0)||(error==INVALID_SOCKET)) { cout << "Error: Player 2 quit!\n"; closesocket (s[1]); WSACleanup (); return; } if ((buffer[0]==RPSS_ROCK)||(buffer[0]==RPSS_SCISSOR)||(buffer[0]==RPSS_PAPER)) { player2 = buffer[0]; } if (buffer[0]==RPSS_QUIT) { sprintf (buffer,"%c%d",RPSS_QUIT,0); send (s[1],buffer,2,0); closesocket (s[1]); closesocket (s[2]); WSACleanup (); cout << "Player 2 quit\n"; return; } if (((int)player1>0)&&((int)player2>0)) { sprintf (buffer,"%c%d",player2,0); send (s[1],buffer,2,0); sprintf (buffer,"%c%d",player1,0); send (s[2],buffer,2,0); cout << "Round completed!\n"; } } char a; cin >> a; } Whenever i run this it hangs at the beginning. so then i changed "int error = WSAStartup (0x0202,&w);" to "int error = WSAStartup (0x0101,&w);" to use winsock 1.1 and it told me "Wrong winsock version" because the wVersion is still 2.2. So i entered "if (w.wVersion!=0x0101)" instead of "if (w.wVersion!=0x0202)" and it froze like usual. Then i tried keeping the wVersion as it is and changed the WSAstartup back to 2.2 and it said "Error you need winsock 2.2 or whatever". It seems my computer doesn''t want to run winsock. Please i need help
ALL YOUR BASE ARE BELONG TO US!!!!
Advertisement
There''s no need to request Winsock version 2.02. Just request Winsock 2.0.

2.02 is just a bugfix release from 2.0, and in WSAStartup, it''s just loading the needed .dlls and creating the structures for your process. Give the code I wrote on your other thread a try and let me know if that works.

But more importantly, tell us which line your program is hanging on when you debug it.
I''ll look for your code.

All the program does is open up the dos menu and doesn''t do ANYTHING AT ALL. It''s like it freezes at startup!
ALL YOUR BASE ARE BELONG TO US!!!!
I''ll look for your code.

All the program does is open up the dos menu and doesn''t do ANYTHING AT ALL. It''s like it freezes at startup!
ALL YOUR BASE ARE BELONG TO US!!!!
I''ll look for your code.

All the program does is open up the dos menu and doesn''t do ANYTHING AT ALL. It''s like it freezes at startup!

Also i tried editing the 0x0202 to 0x0200 and it said i don''t have that version!
quote: Original post by kalldrex


WSADATA w;
int error = WSAStartup (0x0202,&w);
if (error)
{
cout << "Error: You need WinSock 2.2!\n";
return;
}
if (w.wVersion!=0x0202)
{
cout << "Error: Wrong WinSock version!\n";
WSACleanup ();
return;
}




I replaced this with your code and got 59 errors lol!

Edited by - kalldrex on February 14, 2001 5:44:30 PM
ALL YOUR BASE ARE BELONG TO US!!!!
umh, did you replace the ''...'' in my case statements? I just added those to indicate that you should do some kind of error handling there...

And this isn''t a slam, but I''m assuming you''re somewhat new to programming? I just notice you''re cut-and-pasting alot of code from other people... I''m more than willing to help, just need to know what kind of level to talk to you on. =)

If you want to post the first couple error messages you get when compiling(5 of them or so) I can try to figure out what''s going on.

I''m not new to programming but i''m new to network programming. To make things clear this is not my game which is why i''m just copy pasting it. I''m using this right now to find out why the hell my computer doesn''t like winsock.

here are the errors:
quote:
--------------------Configuration: RPSSS test - Win32 Debug--------------------
Compiling...
RPSSS test.cpp
E:\Programming\WS2Game\RPSSS test.cpp(16) : warning C4101: ''you'' : unreferenced local variable
E:\Programming\WS2Game\RPSSS test.cpp(14) : warning C4101: ''s'' : unreferenced local variable
E:\Programming\WS2Game\RPSSS test.cpp(22) : warning C4101: ''w'' : unreferenced local variable
E:\Programming\WS2Game\RPSSS test.cpp(15) : warning C4101: ''me'' : unreferenced local variable
E:\Programming\WS2Game\RPSSS test.cpp(28) : error C2143: syntax error : missing '';'' before ''if''
E:\Programming\WS2Game\RPSSS test.cpp(29) : error C2143: syntax error : missing '';'' before ''{''
E:\Programming\WS2Game\RPSSS test.cpp(29) : error C2447: missing function header (old-style formal list?)
E:\Programming\WS2Game\RPSSS test.cpp(47) : error C2466: cannot allocate an array of constant size 0
E:\Programming\WS2Game\RPSSS test.cpp(47) : error C2501: ''s'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(47) : error C2440: ''initializing'' : cannot convert from ''unsigned int'' to ''int []''
There are no conversions to array types, although there are conversions to references or pointers to arrays
E:\Programming\WS2Game\RPSSS test.cpp(48) : error C2143: syntax error : missing '';'' before ''.''
E:\Programming\WS2Game\RPSSS test.cpp(48) : error C2501: ''me'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(48) : error C2143: syntax error : missing '';'' before ''.''
E:\Programming\WS2Game\RPSSS test.cpp(49) : error C2143: syntax error : missing '';'' before ''.''
E:\Programming\WS2Game\RPSSS test.cpp(49) : error C2501: ''me'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(49) : error C2086: ''me'' : redefinition
E:\Programming\WS2Game\RPSSS test.cpp(49) : error C2143: syntax error : missing '';'' before ''.''
E:\Programming\WS2Game\RPSSS test.cpp(50) : error C2143: syntax error : missing '';'' before ''.''
E:\Programming\WS2Game\RPSSS test.cpp(50) : error C2501: ''me'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(50) : error C2086: ''me'' : redefinition
E:\Programming\WS2Game\RPSSS test.cpp(50) : error C2143: syntax error : missing '';'' before ''.''
E:\Programming\WS2Game\RPSSS test.cpp(51) : error C2143: syntax error : missing '';'' before ''if''
E:\Programming\WS2Game\RPSSS test.cpp(52) : error C2143: syntax error : missing '';'' before ''{''
E:\Programming\WS2Game\RPSSS test.cpp(52) : error C2447: missing function header (old-style formal list?)
E:\Programming\WS2Game\RPSSS test.cpp(57) : error C2143: syntax error : missing '';'' before ''if''
E:\Programming\WS2Game\RPSSS test.cpp(58) : error C2143: syntax error : missing '';'' before ''{''
E:\Programming\WS2Game\RPSSS test.cpp(58) : error C2447: missing function header (old-style formal list?)
E:\Programming\WS2Game\RPSSS test.cpp(63) : error C2143: syntax error : missing '';'' before ''<<''
E:\Programming\WS2Game\RPSSS test.cpp(63) : error C2501: ''cout'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(63) : error C2371: ''cout'' : redefinition; different basic types
c:\program files\microsoft visual studio\vc98\include\ostream.h(139) : see declaration of ''cout''
E:\Programming\WS2Game\RPSSS test.cpp(63) : error C2143: syntax error : missing '';'' before ''<<''
E:\Programming\WS2Game\RPSSS test.cpp(64) : error C2143: syntax error : missing '';'' before ''while''
E:\Programming\WS2Game\RPSSS test.cpp(65) : error C2143: syntax error : missing '';'' before ''{''
E:\Programming\WS2Game\RPSSS test.cpp(65) : error C2447: missing function header (old-style formal list?)
E:\Programming\WS2Game\RPSSS test.cpp(83) : error C2143: syntax error : missing '';'' before ''<<''
E:\Programming\WS2Game\RPSSS test.cpp(83) : error C2501: ''cout'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(83) : error C2371: ''cout'' : redefinition; different basic types
c:\program files\microsoft visual studio\vc98\include\ostream.h(139) : see declaration of ''cout''
E:\Programming\WS2Game\RPSSS test.cpp(83) : error C2143: syntax error : missing '';'' before ''<<''
E:\Programming\WS2Game\RPSSS test.cpp(84) : error C2501: ''closesocket'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(84) : error C2373: ''closesocket'' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1464) : see declaration of ''closesocket''
E:\Programming\WS2Game\RPSSS test.cpp(86) : error C2501: ''sprintf'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(86) : error C2373: ''sprintf'' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\stdio.h(343) : see declaration of ''sprintf''
E:\Programming\WS2Game\RPSSS test.cpp(86) : error C2078: too many initializers
E:\Programming\WS2Game\RPSSS test.cpp(87) : error C2501: ''error'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(88) : error C2143: syntax error : missing '';'' before ''if''
E:\Programming\WS2Game\RPSSS test.cpp(89) : error C2143: syntax error : missing '';'' before ''{''
E:\Programming\WS2Game\RPSSS test.cpp(89) : error C2447: missing function header (old-style formal list?)
E:\Programming\WS2Game\RPSSS test.cpp(94) : error C2501: ''error'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(94) : error C2086: ''error'' : redefinition
E:\Programming\WS2Game\RPSSS test.cpp(95) : error C2143: syntax error : missing '';'' before ''if''
E:\Programming\WS2Game\RPSSS test.cpp(96) : error C2143: syntax error : missing '';'' before ''{''
E:\Programming\WS2Game\RPSSS test.cpp(96) : error C2447: missing function header (old-style formal list?)
E:\Programming\WS2Game\RPSSS test.cpp(103) : error C2143: syntax error : missing '';'' before ''while''
E:\Programming\WS2Game\RPSSS test.cpp(104) : error C2143: syntax error : missing '';'' before ''{''
E:\Programming\WS2Game\RPSSS test.cpp(104) : error C2447: missing function header (old-style formal list?)
E:\Programming\WS2Game\RPSSS test.cpp(161) : error C2143: syntax error : missing '';'' before ''>>''
E:\Programming\WS2Game\RPSSS test.cpp(161) : error C2501: ''cin'' : missing storage-class or type specifiers
E:\Programming\WS2Game\RPSSS test.cpp(161) : error C2371: ''cin'' : redefinition; different basic types
c:\program files\microsoft visual studio\vc98\include\istream.h(173) : see declaration of ''cin''
E:\Programming\WS2Game\RPSSS test.cpp(161) : error C2143: syntax error : missing '';'' before ''>>''
E:\Programming\WS2Game\RPSSS test.cpp(162) : error C2143: syntax error : missing '';'' before ''}''
E:\Programming\WS2Game\RPSSS test.cpp(162) : error C2143: syntax error : missing '';'' before ''}''
E:\Programming\WS2Game\RPSSS test.cpp(162) : error C2143: syntax error : missing '';'' before ''}''
Error executing cl.exe.

RPSSS test.exe - 58 error(s), 4 warning(s)
ALL YOUR BASE ARE BELONG TO US!!!!
Ok, try this then... create a new console app project. Replace the main function with the following and link to ws2_32.lib:

  #include <winsock2.h>int main(int argc, char* argv[]){	WSADATA wsaData;	int nRet = 0;	if((nRet = WSAStartup(MAKEWORD(2,0), &wsaData)))	{		switch(nRet)		{		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;	}	//MAIN GAME LOOP	WSACleanup();	return 0;}  


Let me know if that works. If not, step into the application with the debugger and tell me exactly which line it hangs on.

Good luck. =)

Edited by - JonStelly on February 14, 2001 9:25:10 PM
I''ll try it when i get home as i''m at schol now but I''m telling you that all it does is open the console window and doensn''t do ANYTHING AT ALL! I think it''s a problem it''s having with initailizing winsock or something but there''s no one line it''s freezing on it''s not doing anything!

I''ll try that when I get home.
ALL YOUR BASE ARE BELONG TO US!!!!

This topic is closed to new replies.

Advertisement