• Advertisement
Sign in to follow this  

setting up winsock2 in dev_c++

This topic is 4677 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

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]

Share this post


Link to post
Share on other sites
Advertisement
What exactly is the problem? Copy and paste the error message[s] here.

Share this post


Link to post
Share on other sites
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 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;
}



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.

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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::

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I'm sorry; I actually meant my advice quite sincerely, with the following reasoning:

Reading the error output of a compiler is one of those things that you learn in your first three months of programming a computer, and learning the typical function signature of main() is something you do in your very first C++ program. These skills have nothing to do with your environment; they're cross-environment C++ diagnostics skills. Other such skills include the ability to read and act on link errors; the ability to use a source level debugger; and the ability to introduce logging output in your program where necessary to diagnose a problem. I have no idea whether you have these or not, but they are other basic skills in the same category as the apparent omissions you demonstrated.

Drawing on experience tutoring other programmers, I concluded that you need more experience developing for C++, itself, before you actually start learning something "deep" using that language. If you feel you're better off doing both at once, that's of course your choice. If you have problems understanding specific language constructs or compiler or linker errors, then I suggest the For Beginners forum, which is an excellent place for that kind of question.

Good luck! (That's sincere, too)

Share this post


Link to post
Share on other sites
Well as long as it is sincere [smile]. I will accept that I do not know what all of the errors mean, and I don't know very well how to convert code written in VC++ to DEV_C++, not even the basics of it. But I do know that my passion for programming will bring me further then writting simple programs using cout and cin. I know that I need more experiance, but I also know that what I do know is enough not to relearn the basics but missing a few "fundamental" aspecs. Truth be told, I am tired of reading, and I just want to get my hands into some "deep" coding. I am sorry though that because of my lack of a few important facts, I needed to waste peoples time on this forum, but I am still thankful for all the help.

Now if you don't mind, I (sincerly) think it is time to code. Then some sleep![dead]

Share this post


Link to post
Share on other sites
Can't... sleep... must... code... :-)

On the bright side, when you switch environments once, you start learning first-hand the differences between a language (C++) and an environment (MSVC, DEVCPP, etc), which is one of the many steps towards becoming a skilled programmer.

I'd suggest going for Linux/automake next. :grin:

Share this post


Link to post
Share on other sites
I really have been meaning to get my hands on a copy of Linux for a while... This conversation has been fun [lol] We must do this again soon. Thanks for the advice. Besides DEV_CPP I have used Rhide GNUCPP the DOS environment that came with one of the books I bought, I really didn't like the interface nor the debugger that came with it.

Share this post


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

  • Advertisement