This topic is now archived and is closed to further replies.


BSD sockets with C++

Recommended Posts

Does anyone know of any good online tutorials on how to do network coding with BSD sockets, using C++. I''ve found dozens of C tutorials but I want a C++ tutorial, since it''s my language of choice. I''d much rather use classes than structs...

Share this post

Link to post
Share on other sites
I''m not sure what C++ classes there are for doing socket programming, other than the MFC CSocket and CAsyncSocket classes. So if you''re not coding for Windows, I can''t really help you.

The CAsyncSocket class is very easy to use, and is asynchronous (it doesn''t block). You create a derived class, override members such as OnReceive() which are automatically called when data is received on the socket. It''s really nice.

I don''t know of any tutorials specific to the MFC socket classes, but I had no trouble learning how to use them by using the MSDN as a reference, and Beej''s Guide To Sockets. It''s a C BSD sockets tutorial, but it''s still very helpful since the MFC classes are essentially the same.

Beej''s Guide -

Another option would be to search for an MFC sockets tutorial.

Good luck,

Share this post

Link to post
Share on other sites
Well, once you get a good grasp on sockets programming, you should be able to figure out a good class layout. I have written a library for myself that encompasses Winsock 2 with Overlapped I/O... The following are the names of the classes with a short description of each:

CIp - Handles string -> IP and IP -> string conversions etc...
CHost - Host object responsible for name resolution.
CSock - Abstract base class
CBSock - Blocking Socket
CCBSock - Overlapped I/O Socket clsss using Completion Functions.
CCPSock - Overlapped I/O Socket class using a Completion Port
CnetMsg - Wraps a WSABUF structure and contains a Reference Counting Interface much like COM Interfaces. When I queue a Send, I increment the reference count, when it completes, I decrement it and if it is zero, the message is deleted. Allows me to use a single WSABUF and have multiple I/O operations outstanding at once against that single buffer.

That''s pretty much it. There are of course things I am missing CAsyncSock is one, but I pretty much exclusively use CBSock for client apps, and CCPSock for server apps I know will only be running on NT/2K.

Share this post

Link to post
Share on other sites