gowrath
gowrath

Reputation: 3224

Cross Platform C++ Networking (without big library)

I think it is better if I explain the situation so this doesn't seem too arcane a question. I want to release some starter code for a project I want some of my students to work on. The project involves scraping through some internet webpages and as such, I want to provide them with a URLStream class that will download the html of an input url and return it as a string to them.

The issue is that I can't seem to find a particularly nice way to deal with networking in a way that will be cross platform (the students have mac/windows/linux machines). I know of libraries like Boost asio and libCurl, but the issue with using these is that I can't enforce all my students download them. So my question is twofold:

  1. Is there any nice way to provide them this cross platform networking code?
  2. If a library is the only way to do this, is there any way to attach the library to the starter project so that students don't have to download it? I know this might be a stupid question but I can't seem to find out if this is possible.

Upvotes: 9

Views: 8562

Answers (3)

user8468899
user8468899

Reputation:

cross-platform C++ library for network programming

asio is a cross-platform C++ library for network programming that provides developers with a consistent asynchronous I/O model using a modern C++ approach. It has recently been accepted into Boost.

I copied that from the info window in Synaptic. If you're using Linux, install the library (and its documentation) thus:

sudo apt-get install libasio-dev libasio-doc

Upvotes: 0

Andriy Tylychko
Andriy Tylychko

Reputation: 16276

Boost.Asio is really not suitable for your needs as it involves huge Boost and building at least some of its non-header-only libs. You can still consider Asio lib that can be used w/o Boost and is header-only lib, so much less hassle for you and your students. As it's probably the most popular and modern networking C++ lib this exercise can provide some useful experience to the students. Asio examples also have a simple HTTP client.

As a side note, are you bound to C++ for this assignment? It would be much simpler in Python or similar languages that provide networking out of the box.

Upvotes: 7

Some programmer dude
Some programmer dude

Reputation: 409442

The Berkeley sockets API is the most common low-level socket API. It is supported on all POSIX platforms which means both Linux and macOS will have it.

Even Windows have it, but with a slight twist since sockets aren't descriptors like they are on POSIX systems.

Using sockets directly will lead to more boiler-plate code, but it is definitely possible to use it to make a simple HTTP client that supports only simple GET requests.

There are many tutorials and references on using sockets. Beej's Guide to Network Programming seems to be a popular tutorial, which should have notes about the tweaks needed for Windows.

Upvotes: 4

Related Questions