# Networking in C++

This topic is 3011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, Basically me and my friends want to make a game, and I thought if we're gonna make it we may aswell do it properly in C++. Since I only know a bit of Win32 C++ (Im still reading 'Windows game programming for dummies' which is helping alot), basically I want to know how to to the whole networking bit; So I was thinking I should make a chatroom or a simple server/client connection to figure out the whole networking thing (The rest of the book im reading (and some other books) will help out with the other parts a bit). Does anyone know how I can create this Client/Server connection? I looked a bit at winsock and some other thing, but no one really described how I got the header files and how I acctually used the functions or anything, which kinda rules them out as options until I get some idea on how to use them.. Thanks In Advance, Joe

##### Share on other sites
Quote:
 Original post by Joesavage1Basically me and my friends want to make a game, and I thought if we're gonna make it we may aswell do it properly in C++.

There's plenty of easier, 'proper' alternatives to C++ nowadays, ya know?

Quote:
 Does anyone know how I can create this Client/Server connection?I looked a bit at winsock and some other thing, but no one really described how I got the header files and how I acctually used the functions or anything, which kinda rules them out as options until I get some idea on how to use them..

Have you checked the Forum FAQ, namely question #1? There's plenty of good information and links for general questions like this there.

##### Share on other sites
Quote:
 Quote:Original post by Joesavage1Basically me and my friends want to make a game, and I thought if we're gonna make it we may aswell do it properly in C++.There's plenty of easier, 'proper' alternatives to C++ nowadays, ya know?

Yeah, but I think we all know C++ is the mainly used. Also I know quite a bit of C++ so it seemed the right decision..

Quote:
 Quote:Does anyone know how I can create this Client/Server connection?I looked a bit at winsock and some other thing, but no one really described how I got the header files and how I acctually used the functions or anything, which kinda rules them out as options until I get some idea on how to use them..Have you checked the Forum FAQ, namely question #1? There's plenty of good information and links for general questions like this there.

Ah, I look a bit of an idiot now; Thanks, Ill read through and if I have any other difficulty Ill post it.

##### Share on other sites
Hm. Ive had a read through many, MANY pages of tutorials on how to do this but they all seem to really rush into it, and I dont really understand what does what etc.

I would really appreciate it if someone posted some winsock sending and receiving messages code (preferably heavily commented)..

Joe

##### Share on other sites
These two are a bit at odds with each other:
Quote:
 I would really appreciate it if someone posted some winsock sending and receiving messages code (preferably heavily commented)..

Quote:
 Also I know quite a bit of C++ so it seemed the right decision..

Winsock, or sockets in general, are just an API. There is really not much to them. There is a code stanza to initialize it, then there are socket(), bind(), recv(), send() and select().

The order in which they are put has a reason, but can be just followed blindly. The tutorials haven't really changed in last 20 years, since the interface hasn't changed since then.

The "modern C++" alternative is boost asio, the middle ground is ACE, but I doubt those would be clearer.

I'm not really sure what the actual IP networking reading materials are, usually these are provided by various network programming courses, but they tend to cover much more than just dealing with connections, and tend to be somewhat OS-biased, since there is usually some overlap between kernel and networking.

##### Share on other sites
Quote:
 There is a code stanza to initialize it, then there are socket(), bind(), recv(), send() and select().

Can you link me to one, or give an example please? (sorry for being such a pain, I just really dont understand it (im sure it'll just click into place when I figure it out).

Quote:
 I'm not really sure what the actual IP networking reading materials are, usually these are provided by various network programming courses, but they tend to cover much more than just dealing with connections, and tend to be somewhat OS-biased, since there is usually some overlap between kernel and networking.

Im only really bothered about Windows to be honest.. (I shouldn't really rule the other people out, but Windows is the most popular OS (as im sure we all know)).

##### Share on other sites
Quote:
Original post by Joesavage1
Quote:
 There is a code stanza to initialize it, then there are socket(), bind(), recv(), send() and select().

Can you link me to one, or give an example please? (sorry for being such a pain, I just really dont understand it (im sure it'll just click into place when I figure it out).

MSDN Winsock Reference

##### Share on other sites
Quote:
 Original post by Joesavage1Can you link me to one, or give an example please? (sorry for being such a pain, I just really dont understand it (im sure it'll just click into place when I figure it out).

This one is in FAQ (Q1) as well, along with many more.

##### Share on other sites
Ok so I got this far:

#include <iostream>#include <winsock2.h>using namespace std;int main(){WSADATA WsaDat; //For initializationif (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0) //Worked?{cout << "WSA initialization failed." << endl;}SOCKET Socket; //SocketSocket = socket(AF_INET, SOCK_STREAM, 0);if (Socket == INVALID_SOCKET) //Worked?{cout << "Socket creation failed." << endl;} //We want to use port 50SockAddr.sin_port = 50;//We want an internet type connection (TCP/IP)SockAddr.sin_family = AF_INET;//We want to listen on IP address 127.0.0.1SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;SockAddr.sin_addr.S_un.S_un_b.s_b1 = 1;//Ok all the information is set, lets bind()if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)//Worked?{cout << "Attempt to bind failed." << endl;}system("PAUSE");return 0;}

However Ive started getting some errors and I dont know why.. (I havent even got to the sending and receiving bit yet *sad face*)

Joe

EDIT: Forgot to post the errors :S.
Here they are:

1>------ Build started: Project: ConsoleNetworkingTest, Configuration: Debug Win32 ------
1>Compiling...
1>ConsoleNetworkingTest.cpp
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(23) : error C2065: 'SockAddr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(23) : error C2228: left of '.sin_port' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(26) : error C2065: 'SockAddr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(26) : error C2228: left of '.sin_family' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(29) : error C2065: 'SockAddr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(29) : error C2228: left of '.sin_addr' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(29) : error C2228: left of '.S_un' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(29) : error C2228: left of '.S_un_b' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(29) : error C2228: left of '.s_b1' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(30) : error C2065: 'SockAddr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(30) : error C2228: left of '.sin_addr' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(30) : error C2228: left of '.S_un' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(30) : error C2228: left of '.S_un_b' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(30) : error C2228: left of '.s_b2' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(31) : error C2065: 'SockAddr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(31) : error C2228: left of '.sin_addr' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(31) : error C2228: left of '.S_un' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(31) : error C2228: left of '.S_un_b' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(31) : error C2228: left of '.s_b3' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(32) : error C2065: 'SockAddr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(32) : error C2228: left of '.sin_addr' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(32) : error C2228: left of '.S_un' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(32) : error C2228: left of '.S_un_b' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(32) : error C2228: left of '.s_b1' must have class/struct/union
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(35) : error C2065: 'SockAddr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(35) : error C2065: 'SockAddr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\consolenetworkingtest\consolenetworkingtest\consolenetworkingtest.cpp(35) : error C2070: ''unknown-type'': illegal sizeof operand
1>Build log was saved at "file://c:\Users\user\Documents\Visual Studio 2008\Projects\ConsoleNetworkingTest\ConsoleNetworkingTest\Debug\BuildLog.htm"
1>ConsoleNetworkingTest - 27 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

[Edited by - HiddenHaxor on February 22, 2010 11:00:54 AM]

##### Share on other sites
Apparently I have t wo accounts. Sorry :S. Just to confirm the above IS me..

• 23
• 10
• 19
• 15
• 14