View Full Version : C++ Network Programming
My new "hook" so to speak has been looking at proper networking for games and network prediction, and as such Ive been reading a lot into the topic, and found a decent set of articles on the topic that were general enough to have various applications, but precise enough for me to use it to get some code going. Now, my problem is this. Ive written (and abstracted to an extent) the winsock2 code so that I can use UDP sockets to throw data at other computers (this does have a higher purpose when I can get it working) but have hit a stumbling block, for some reason my code doesnt work.
Ive gone through all the various checks that one should go through when trying to find where data is going to (firewalls, addresses, whether the app is listening, are the sockets creating properly, etc) and every last details seems fine, but for some reason the code for send and receive loses the data somewhere in between them. Ive gotten a bunch of people to read through it in case I missed some overly obvious problem with the code, and Ive given up, so Im turning to you guys as you are probably the next likely people to know.
main.cpp (http://edg3.pastebin.com/bAsEpiZC)
- g_port is defined const int 10010
- the #defines are all fine
networking.cpp (http://edg3.pastebin.com/SR8rerGg)
The articles are a good read. (http://gafferongames.com/networking-for-game-programmers/sending-and-receiving-packets)
Necrolis
18-10-2010, 09:53 AM
well apart from all the bad code habbits in your files :P you should be checking for SOCKET_ERROR then querying WSAGetLastError for the error code on sendto/recvfrom. also, its a good idea when doing network programming to have something like wireshark installed, makes checking wtf is actually going on a dream.
what you might try before going onto building IP's your self is using getaddrinfo (http://msdn.microsoft.com/en-us/library/ms738520%28v=VS.85%29.aspx) on "localhost" to get your own local IP and send to that, instead of using 192.168.101.1 which is a router allocated address and probably not always going to be your local address(atleast use 127.0.0.1 for local instead of what ipconfig reports) or even valid, you can also just use inet_addr (http://msdn.microsoft.com/en-us/library/ms738563%28VS.85%29.aspx) instead if you don't want to bother getting the localhost
the examples at the bottom of sendto (http://msdn.microsoft.com/en-us/library/ms740148) and recvfrom (http://msdn.microsoft.com/en-us/library/ms740120%28VS.85%29.aspx) are a lot better than your link imo, give them a try as is, and see if you still get problems.(stackoverflow (http://stackoverflow.com/questions/679145/how-to-set-up-a-winsock-udp-socket) has some better examples too).
just a note, I haven't tried to compile your code(yet), will report back when I get time to.
Ok, check the code out, who ever you got to look over this is pretty blind :p there where a few errors i came across, biggest being the use of recvfrom and the buffer passed to Recieve. I'll upload the fixed code when i get to uni in about 30 min
My calls for WSAGetLastError() are when there was an error, errors return values of <1 from the socket functions, you dont need to explicitely look for SOCKET_ERROR as long as you check for negative values. The hard coded IP was because I tried every combination of PC at my house, and I cant send from the client program to the server program on the same computer (only 1 socket can be opened on the port).
I do know of getaddrinfo() and inet_addr(), and MSDN examples are always great (or better) but Im following that article because it touched on game dev programming not just socket programming. As for stack overflow, the stuff I read there couldnt help solve my problem.
As for the buffers being passed to recvfrom that has been remedied, but it doesnt solve the problem. I dont update the code on pastebin everytime I fix something, esspecially small things :)
I hope you can help solve the problem though
Necrolis
18-10-2010, 04:27 PM
My calls for WSAGetLastError() are when there was an error, errors return values of <1 from the socket functions, you dont need to explicitely look for SOCKET_ERROR as long as you check for negative values.you shouldn't actually do that, cause if a new case is added for -2, your screwed, and 0 isn't an error. also, that comment was really directed at the sendto/recvfrom which had no checks at all
The hard coded IP was because I tried every combination of PC at my house, and I cant send from the client program to the server program on the same computer (only 1 socket can be opened on the port).nope, that was a design flaw on your side(or from that article) :P, the binding is only needed for the server, even then its not needed... my fix up runs fine on pc
As for the buffers being passed to recvfrom that has been remedied, but it doesnt solve the problem. I dont update the code on pastebin everytime I fix something, esspecially small things :)you should when your asking others with help on fixing it ;)
I hope you can help solve the problem though, already done, see the fixed up code here (http://necrolis.pastebin.org/290847)(don't take any offence to the comments), merged the two files just for ease of use
Evolution
21-10-2010, 05:19 PM
You shouldn't use inlines really, let the compiler decide for you. Also you might be better off creating a base Socket class, and let Win/Mac/LinuxSocket inherit from that. Managing code with allot of precompiler stuff can give you headaches. Use typedefs for unsigned real number types. Also add #ifndef NETWORKING_H \ #define NETWORKING_H at the start of your header, and #endif at the end. I can go on and on....
Necrolis
21-10-2010, 08:56 PM
You shouldn't use inlines really, let the compiler decide for you.that depends very much on what your doing, the compiler your using(msvc is shockingly bad at inlining the right stuff), and the aim of the overall code.
Managing code with allot of precompiler stuff can give you headaches.*a lot. depends on the strategy you use, problems arise when the code is too spread and then needs changing/updating, but if you look at most big open source projects, they do a lot of inline cpp work for cross compatibility
Use typedefs for unsigned real number types. stdint.h is far better, especially if your aiming at multi platforms(apart from the safety, it prevents the chance for collisions or annoyance at unneeded typedefs).
I can go on and on....good to see its not just me :)
Evolution
21-10-2010, 09:21 PM
I use allot of different compilers, I usually have something like #define COMPILER_INLINES which makes all my INLINES invalid and lets the compiler take over.
If you look at allot of popular open source cross platform projects (OGRE / OIS) you will notice that they have their base class, and then they have platform specific classes which inherit from them. This allows you to keep code for a specific platform separated from your core code, and not have all platform's code jumbled up into one class. I would rather go through 4 files, than go through 1 file that has all the code mixed up into it. Also I doubt it will spread out the classes that much, considering that platform specific code is the minority in a project.
FuzzYspo0N
21-10-2010, 10:53 PM
You two are hilarious. Why not argue about linux versus windows instead? Anyone wanna argue python and tabs versus spaces!! OH YEA !
Edg3, as you know well enough (and our countless gtalk sessions) :
1) Don't reinvent the wheel.
2) Use existing cross platform libraries (they are there for a reason)
3) Attacking beginners code is cool.
That is just my 2 cents ;)
dislekcia
22-10-2010, 01:58 AM
You two are hilarious. Why not argue about linux versus windows instead? Anyone wanna argue python and tabs versus spaces!! OH YEA !
Heh, glad I'm not the only one thinking that ;) I kept going "Thank **** I don't bother with C++ much anymore" while reading.
Although to be fair I'm sure you guys are making a lot of sense. Quick question @Evolution, how does that definition of yours remove inlines, different declarations of the same methods? That doesn't seem smart to me, so I assume you're doing something else...
Necrolis
22-10-2010, 08:19 AM
You two are hilarious. Why not argue about linux versus windows instead? Anyone wanna argue python and tabs versus spaces!! OH YEA !who said we are arguing?, apart from a little debate, we're giving edg3, and anyone else interested, tips for working with C++ based on the code he posted. Your 'examples' don't even relate to whats going on here...
3) Attacking beginners code is cool.attacking someones code to be a prick/troll and criticizing their code to improve their knowledge & understanding are two very different things...
Quick question @Evolution, how does that definition of yours remove inlines, different declarations of the same methods? That doesn't seem smart to me, so I assume you're doing something else...seeing as I like this method too: it uses the cpp(c preprocessor) to 'alias' a macro to the inline keyword when enabled, when disabled it 'aliases' the macro to nothing(basically its stripped out, like it wasn't even there)
Evolution
22-10-2010, 09:05 AM
Well said. I'm amazed at your technical skills for your age, when I was your age I only started programming :( Is it your Comp Sci course that is teaching you this, or is it partly your own efforts?
Necrolis
22-10-2010, 10:41 AM
Well said. I'm amazed at your technical skills for your age, when I was your age I only started programming :( Is it your Comp Sci course that is teaching you this, or is it partly your own efforts?I'm just finishing second year at UJ doing BSc Comp Sci, but I had been programming long before that, about 5 years in x86 asm and 3 years in C & C++ before uni, problem about learning so early is it makes uni very boring :P
dislekcia
22-10-2010, 12:00 PM
seeing as I like this method too: it uses the cpp(c preprocessor) to 'alias' a macro to the inline keyword when enabled, when disabled it 'aliases' the macro to nothing(basically its stripped out, like it wasn't even there)
That's definitely a lot smarter ;) I always ignored macros, so no wonder I didn't think of that.
Evolution
22-10-2010, 12:55 PM
The smarter person doesn't need to prove to other people how smart they are by pointing out how stupid the next person is.... unless they second guessing their smartness????? ;)
As I said use your imagination when it comes to what ever I say. I hate using overly complexed sentences to explain something unless I'm writing a tutorial. If you want me to explain in detail then I will for those that are not as skilled with c++ to get what I'm saying. I pass in COMPILER_INLINES as a prepocessor declaration to the compiler's arguments. In one of the headers I will place if defined COMPILER_INLINES then define INLINE as nothing, otherwise define it as inline. You also get compiler specific preprocessors that will do the same, but we want multi platform support. There was that smart enough for you to comprehend ;)
What's up with the forum. I updated Ubuntu and after that when I post a message I end up downloading php scripts from your server. FTW?
I'm just finishing second year at UJ doing BSc Comp Sci, but I had been programming long before that, about 5 years in x86 asm and 3 years in C & C++ before uni, problem about learning so early is it makes uni very boring :P
Yeah, I dont know why I didnt think of it before but I should have emailed Necrolis immediately in the first place (having done stuff with him before).
Also, I wasnt trying to come across as rude or insulted or anything. Just been having a rough week. lol
Lastly, I havent tried Necrolis' code, but I saw what I did wrong, and am now using an opensource networking library.
Thanks for the help guys.
Powered by vBulletin® Version 4.2.4 Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.