Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ and network sockets

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.

like image 721
Tristan Avatar asked May 10 '17 22:05

Tristan


People also ask

What are C sockets?

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.

What are network sockets used for?

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.

What is networking in C language?

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.

What is a network socket example?

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.


1 Answers

The standard library does not contain sockets or any networking support.

Your choices are

  1. BSD sockets, if you are on *nix via OS specific libraries
  2. BSD blocking sockets, if you are on Windows, via Winsock
  3. Async sockets, if you are on Windows, via Winsock and the Windows Message Pump
  4. IOCP, if you are on Windows, via Winsock
  5. Boost ASIO, which is portable.
  6. Some other 3rd party library where someone else used one of the above and wrapped it up for you.

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.

like image 116
Christopher Pisz Avatar answered Oct 16 '22 08:10

Christopher Pisz