C++ Winsock Networking Tutorials [04-24-2017]

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by albinopapa » November 17th, 2016, 11:03 pm

If you want to use Windows specific APIs maybe look at WinRT
No thanks lol, don't like the syntax, though after watching a CppCon video, I might not have to use WinRT directly. They came out with a C++ um, wrapper? for it. I don't remember the specifics, perhaps I'll look into it one day.

Thanks for the reply, I always took NULL as empty, which is why I think of it as a 0. Null terminated string == 0 terminated, so there's another connection to 0, where the VALUE '\0' or 0 is considered NULL. Since this is MY mindset, I find it harder to reason what the underlying variable is suppose to represent, a 0 value or a 0 address because so many use NULL for pointers even after nullptr was introduced. I'll just stay away from NULL all together, so there isn't any confusion.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by chili » November 18th, 2016, 3:53 am

I mostly stick to std::thread stuff, but there is an argument to be made for using platform-specific APIs. The std library is made to be lean by design, and it also has to work at the LCD in terms of features. You can get access to more sophisticated/higher performance constructs by digging down into the WinAPI.

E.g I'm doing some tests on my Xeon system with 16 hardware cores. Since I am dealing with 2 NUMA pools, the ability to set processor affinity is paramount.
Chili

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by LuisR14 » November 18th, 2016, 6:49 am

... is taking the fun out of programming, anyways, to consider NULL as an always-related-to-ptrs kind of concept just kind of seems contradictory (which i deem wrong for those other compilers to issue a warning, but what can i do *shrugs*), considering what albpapa mentioned
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

reductor
Posts: 49
Joined: October 24th, 2016, 12:23 pm

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by reductor » November 18th, 2016, 7:24 am

chili wrote:I mostly stick to std::thread stuff, but there is an argument to be made for using platform-specific APIs. The std library is made to be lean by design, and it also has to work at the LCD in terms of features. You can get access to more sophisticated/higher performance constructs by digging down into the WinAPI.

E.g I'm doing some tests on my Xeon system with 16 hardware cores. Since I am dealing with 2 NUMA pools, the ability to set processor affinity is paramount.
The standard isn't specifically about keeping things lean or targeting the lower-common-denominator, there are many things involved in the philosophy (Safety, Performance, etc).

Using OS API's don't always provide better performance, more then likely you will get equal performance. Performance should not always be your number one concern instead profile and then look at the problem instead of prematurely optimizing.

For doing things such as controlling processor affinity the standard provides std::thread::native_handle() for being able to call the OS functions when necessary.
LuisR14 wrote:... is taking the fun out of programming, anyways, to consider NULL as an always-related-to-ptrs kind of concept just kind of seems contradictory (which i deem wrong for those other compilers to issue a warning, but what can i do *shrugs*), considering what albpapa mentioned
I don't think it's a matter of taking the fun out of programming, understanding why to use one technique vs another technique is where alot of fun exists. Knowing the pros and cons is very useful.

The warnings are there for very good reasons, because people get things wrong and to provide better type safety. Not only that it is documentation for the code reader that (this is a pointer, this is 0)

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by LuisR14 » November 18th, 2016, 11:05 am

reductor wrote:
LuisR14 wrote:... is taking the fun out of programming
I don't think it's a matter of taking the fun out of programming, understanding why to use one technique vs another technique is where alot of fun exists. Knowing the pros and cons is very useful
nah, ..was ..... referring to something else, ... although i agree about finding out the pros and cons by experimenting, and that surely would give way to \giving suggestions/ to the community.
reductor wrote:The warnings are there for very good reasons, because people get things wrong and to provide better type safety. Not only that it is documentation for the code reader that (this is a pointer, this is 0)
in my code i'd be using (and have been) nullptr anyhow
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by egizz983 » November 18th, 2016, 6:12 pm

I heard the thing that Winstock is not a good idea for a Server Side is it a true ? and what then is the better for Server Side ?

And another thing is how to properly include winstock into Chili framework cuz i got tons of error once i tried , i also tried do like this :

Code: Select all

#include <WinSock2.h>
#include <Windows.h>
didt work

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by albinopapa » November 18th, 2016, 9:30 pm

@egizz, I was able to include the Winsock2.h file just above the Windows.h file in ChiliWin.h

You need to make sure you add the ws2_32.lib somewhere
#pragma comment(lib, "ws2_32.lib")

I was able to put it in the MainWindow.cpp file after all the #includes and it compiled and ran fine, was even able to run the WSAStartup function.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Pindrought
Posts: 432
Joined: September 26th, 2013, 4:57 pm
Location: Kentucky
Contact:

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by Pindrought » November 18th, 2016, 9:57 pm

egizz983 wrote:I heard the thing that Winstock is not a good idea for a Server Side is it a true ? and what then is the better for Server Side ?

And another thing is how to properly include winstock into Chili framework cuz i got tons of error once i tried , i also tried do like this :

Code: Select all

#include <WinSock2.h>
#include <Windows.h>
didt work
It has been a while since I have done anything with the Chili framework. If you could post your solution, I will take a look at it to try to help you figure out how to properly include winsock.
PM me if you need to contact me. Thanks to all the helpful people on this forum especially to Chili.

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by egizz983 » November 18th, 2016, 10:03 pm

The i include Client.h inside a GameState.h
albinopapa wrote:@egizz, I was able to include the Winsock2.h file just above the Windows.h file in ChiliWin.h

You need to make sure you add the ws2_32.lib somewhere
#pragma comment(lib, "ws2_32.lib")

I was able to put it in the MainWindow.cpp file after all the #includes and it compiled and ran fine, was even able to run the WSAStartup function.
i tried to comment that library just above winstock include that didt work for me
Attachments
Game.rar
(669.54 KiB) Downloaded 154 times

Pindrought
Posts: 432
Joined: September 26th, 2013, 4:57 pm
Location: Kentucky
Contact:

Re: C++ Winsock Networking Tutorials [11-16-2016]

Post by Pindrought » November 18th, 2016, 10:33 pm

Move #include "Client.h" from GameState.h to MainWindow.h to be the first include.

This is required, because you need to include Winsock2.h before you include Windows.h

Also, since WinSock2.h will be included before Windows.h, you need to move #define NOMINMAX from ChiliWin.h to the beginning of Client.h before any includes.

I've attached the solution to this post.
Attachments
Game egizz.rar
(1.34 MiB) Downloaded 232 times
Last edited by Pindrought on November 18th, 2016, 10:41 pm, edited 2 times in total.
PM me if you need to contact me. Thanks to all the helpful people on this forum especially to Chili.

Post Reply