I have dabbled in C, and I am currently picking up C++.
I want to learn the standard library (std::
namespace), so I am working on a few projects. One is a simple email program that logs into my local SMTP server and shows my email, and has the ability to send mail. I add a touch of ncurses for that old feel.
The problem is, I haven't been able to find a standard library way to use network sockets. It seems like I have to use network sockets in C sys/socket.h
, but then I end up with a file descriptor, which leaves read()
and write()
and send()
function calls. I wanted to be able to use the std::fstream
or std::ostream
classes. But the open()
member functions take a filename, not a file descriptor.
I know that the Boost library has this capability, but templates and such are way over my head, and I want to stick with just the standard library for now.
Before I proceed, I just want to make sure that there isn't a better way to read()
and write()
to a file descriptor that's been returned by the connect()
network socket C function.
A socket is a generalized interprocess communication channel. Like a pipe, a socket is represented as a file descriptor. Unlike pipes sockets support communication between unrelated processes, and even between processes running on different machines that communicate over a network.
A socket is one endpoint of a two way communication link between two programs running on the network. The socket mechanism provides a means of inter-process communication (IPC) by establishing named contact points between which the communication take place.
Network programming enables processes to communicate with each other over a computer network, but it is a complex task that requires programming with multiple libraries and protocols. With its support for third-party libraries and structured documentation, C is an ideal language to write network programs.
This pair is known as a network socket. For example, if application A wants to transact with application B, they need to do so via their network sockets (we'll just refer to them as sockets from now on). These would be IPAddress-A:PortNumber-A and IPAddress-B:PortNumber-B.
The standard library does not contain sockets or any networking support.
Your choices are
Of all of those, it is my personal opinion that, Boost ASIO is the easiest to use. I would recommend at least going through a tutorial on blocking BSD sockets though to get the concepts down.
All of these, except 5 and 6, are going to be very C like. Network programming really isn't a niche where object orientated programming and C++ rule over old C style. You can wrap things, but that's the best you can hope for.
You really won't be using fstream or ostream in your socket code. Perhaps in a layer above it, but in the end, all these implementations are similar, in that you will be sending and receiving bytes of binary data or text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With