setting up winsock2 in dev_c++

Started by
12 comments, last by Desmosthenes2005 18 years, 11 months ago
Hi, I am new to the forums and to network programming. I know that you have to link the libws2_32.a to your project and include windows.h, winsock2.h, stdio.h, and iostream.h . I was wondering if there is anything else I need to do because I can't seem to compile any winsock2 sample programs that I download. I haven't yet writen any winsock programs because I don't understand it and I feel that the first step to understanding it is being able to compile sample programs! I guess what I am asking is whether there is actually something more I need to do to set up winsock2? And if not, does anyone know how to fix my problem? Thanks, Desmosthenes2005 [Edited by - Desmosthenes2005 on April 26, 2005 3:32:10 PM]
Advertisement
What exactly is the problem? Copy and paste the error message here.
well, for some, they compile but the client says it cant find the server, and the server says it cant use the port (it was programmed to scan port 8888). Although the program that I really want to get running doesn't compile, because of thirteen (or so the compiler says) errors. Now, I'm guessing that the program compiled on the writers machine, and he must have tested it on multiple servers, plus a lot of people have probably downloaded it without error (considering that there were no bad responses in the forums). The code for the server, which I am trying to compile first is listed below allong with the errors.
#include <iostream.h>#include <winsock2.h>#include <stdio.h>#define RPSS_NUMOFUSERS 0x03#define RPSS_STARTGAME 0x04#define RPSS_SCISSOR 0x05#define RPSS_ROCK 0x06#define RPSS_PAPER 0x07#define RPSS_QUIT 0x08void 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;}


That is the code, here are the errors:

-----------------------------------------------------------------------------

E:/DEV-CPP/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
Rpsss.cpp:13: error: `main' must return `int'
Rpsss.cpp: In function `int main(...)':
Rpsss.cpp:27: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:33: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:44: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:50: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:61: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:81: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:88: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:100: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:114: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:123: error: return-statement with no value, in function returning 'int'
Rpsss.cpp:137: error: return-statement with no value, in function returning 'int'
g++.exe Rpsss.o -o "Project2.exe" -L"E:/DEV-CPP/lib" ../Dev-CPP/LIB/libws2_32.a
G__~1.EXE: Rpsss.o: No such file or directory

--------------------------------------------------------------------------------

Thanks much for your reply and I hope that someone can some how assist me.

Desmosthenes2005.
Quote:
Rpsss.cpp:13: error: `main' must return `int'

You miss this?
[attention] Oh sorry, of course I should have remembered!

The error I forgot to put on the list was very important, and after looking for it for about an hour I found it...

error: return-statement with no value, in function returning 'int'

[lol]
So, did you fix the problem now?
I mean, I don't understand what exactly is the problem. Just address the issue in the error message, and you should be fine.
Well, I am still not sure why the program is having trouble with iostream and for some reason it is asking for int main, but the main function is void. Im stumped. [help]

[Edited by - Desmosthenes2005 on April 28, 2005 2:17:04 PM]
Quote:Original post by Desmosthenes2005
Well, I am still not sure why the program is having trouble with iostream and for some reason it is asking for int main, but the main function is void. Im stumped. [help]


1.) Change main to int and make return values.
2.) iostream.h is depreciated in C++, use the standard <iostream> and reference with namespaces.. std::
0) Learn to program C++ before trying to learn network programming in C++.
enum Bool { True, False, FileNotFound };
I thought it was namespace::std... oh and thanks hplus0603. I mean, its not like I have read through thousands of pages on C++ and have been programming in other languages like C++ for many years. The only thing I have to learn is how to use DEV_CPP, I am new to it. I came here with an honest question for a problem that has had me tearing out hair for a while, the reason I came here is because I greatly respect the people who use this forum and who have years more experiance then I. I am so glad that you could share your oh so great wisdom, and flame someone looking for some help. I respect your possition as Moderator of this forum and how hard it must have been to earn that possition, but I am pretty sure that you shead little light on my situation (if not made it worse... now I have very low self esteem [bawling] )I am glad though that people could indeed help me and spend time making my life a little easier. And I am very greatful for the help provided by Saruman and Raduprv, and I thank you both very much, I hope that I can make it up to you.

My question has been answered, thank you all.
Desmosthenes2005

This topic is closed to new replies.

Advertisement