So I need to introduce myself to sockets and general network programming. I consider myself fluent in C++, but I basically can build stuff with the basics of C++ (think having only a basic set of legos)
Now I really need to venture forth and expand my lego set to different pieces (pardon the lego metaphor) But socket programming/network is my next target. So I thought a simple program to write would be a file sending program.
Basically a program that you type in the IP of your friend, (he has the program too) and it asks "Would you like to receive fileXXX.yyy" If he says yes, then it proceeds to send the file.
I know i'd need to know about sockets, but what else would I need to know about? Should this be a simple program to accomplish? Any recommended tutorials I read before I dive head first?
Any additional info is helpful. Thanks. And NO this is not for school, it's just kinda something I wanna learn.
C++ preferred language.
You should get yourself the boost libraries.
There is no standard socket API for C++ but boost provides one in their ASIO library that will work on multiple platforms (as is the case with all boost libraries).
beej has a good guide for unix socket programming:
http://beej.us/guide/bgnet/
You can use the same code on a windows platform if you include the winsock library and an additional startup call
The Boost.Asio library mentioned in CashCow's answer is a fantastic cross-platform and easy to use library. Don't let the asynchronous name scare you, it can be a bit daunting at times but the documentation is straightforward and easy to understand.
The boost-asio tag is also fairly active on stack overflow.
Related
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:
Is there any nice way to provide them this cross platform networking code?
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.
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.
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.
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
I'm learning C++ and so I've decided to begin coding a IRC-Client.
I basically want it to be consolebased, and I've looked in to libraries such as ncurses, but I don't really know whether or not this would be the best approach.
I imagine the UI being divided into one part where whatever messages are written appear, and one part where the users input goes. Ncurses seemed to be able to do this, but now I've discovered another issue.
Because I want the message-part to be event driven (whenever somebody sends a message, this should appear in the message-part) the message-part of the UI should run independently from the input-part. Also, sockets would have to be non-blocking as well.
I've looked around on the internet and haven't found any good tutorials on this, as most are either really, really old, poorly written or simply just to long.
Anyways, my questions are, how would this be done using the ncurses and socket libraries? Are their any good C++ wrappers (one thing I've learned from reading ncurses tutorials is that OOP is really wonderful...)?
You'll probably need to use a few threads to do that, so I'd take a look at pthreads.
However, you shouldn't be afraid of long tutorials, because what you're trying to achieve is not so simple, specially if C++ is the first computer language you learn.
Using ncurses for the interface sounds like a good idea. You can do a single-threaded select-based network and terminal client -- check out Beej's guide. Alternatively, Boost.asio, single-or-multithreaded, should be a solid choice, too.
I really thought this would be easier to find...
I need a portable c++ sockets wrapper. I'm planning to use it for a windows server application and a client that will be running on a embedded device running ulinux (or something similar). I would use Boost but I need it to be lightweight and easy to add to the embedded device project.
Also I would like it to be a "higher level" wrapper... so it starts a background thread to read data and informs be over a callback...
Any ideas?
I'd suggest Boost.Asio. Despite it's name, you are not forced to use asynchronous I/O. You could use synchronous I/O and threads, as your question implies.
Boost.Asio is a cross-platform C++
library for network and low-level I/O
programming that provides developers
with a consistent asynchronous model
using a modern C++ approach.
Just learn to use the socket API directly. You can then easily wrap it yourself. It's not that hard, and you can get started with Beej's excellent guide. As Beej says:
The sockets API, though started by the
Berkeley folk, has been ported to many
many platforms, including Unix, Linux,
and even Windows.
In his guide he details the very small addition you need to do to get the same API in Windows and *nix systems.
Once you've learned, wrap it yourself if you're so inclined. Then you can control exactly how "lightweight" you want it.
If you really don't like Boost asio then you might like the sockets support in dlib. It is simpler in the sense that it uses traditional blocking IO and threads rather than asio's asynchronous proactor pattern. For example, it makes it easy to make a threaded TCP server that reads and writes from the iostreams. See this example for instance. Or you can just make a simple iosockstream if not acting as a server.
I know this is old, but there is a very nice and simple implementation in below location which I'm using for personal use. Had implemented my own wrapper a while back but lost the code and found this one online which is much better than mine:
http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/
Take a look at ENet http://enet.bespin.org/ it is very lightweight and portable and works on top of UDP, with optional support for reliable packets. It is easy to use, the API is low-level and with little performance overhead. You have a high degree of control over the memory management, which could be good if networking is a bottleneck for you and the malloc/new implementation you use performs badly under multithreading.
It would not be that hard to implement your high level thread “optimally”, since there is optional support for blocking receive and the library is a “library” and not a framework therefore you are the decision maker instead of the library.
Perhaps you can have a look at http://www.pt-framework.org/
Old question, but for C++, BSD style synchronous sockets this is about as minimal baggage wrapper as you can find
http://code.google.com/p/ting/source/browse/trunk/src/ting/net/
It does come with exceptions. You could make a bit more lightweight one as a header-only template library, and maybe make exceptions optional, but that would change the API a bit
POCO network classes are quite similar, but do require more dependencies from other parts of the Poco lib
I'm personally creating my own AsIO wrapper for both TCP and Serial sockets, and I started by reviewing the following tutorial:
https://www.gamedev.net/blogs/blog/950-they-dont-teach-this-stuff-in-school/
and
https://objectcomputing.com/resources/publications/mnb/multi-platform-serial-interfacing-using-boost-a-gps-sensor-and-opendds-part-i/
I found the first one very useful and simple to understand.
C++CSP2
Used it loved it. Stable and powerful
I'm trying to implement my own IRC client as a personal proejct and I realized I needed a way to read and write from the socket at the same time. I realized I could have a reading thread which reads from the socket in the background and puts data in a queue and I could have another thread which writes data from a queue to the socket. However I have no idea on how to start with multithreaded programing or how to do it with c++. Where do I go from here?
For C++ threads, boost::thread (which is the basis for the upcoming std::thread) is the best way to go. That said, while threads might be the correct solution for your particular case, I just wanted to throw it out there that select and non-blocking sockets are a common approach to interleaving the reading/writing and writing of multiple sockets without the need for threads. The boost::asio library wraps the functionality of select and non-blocking sockets in a cross-platform, C++ manner.
It's specific to C and *nix, but I can't think of a better starting place than Beej's Guide to Network Programming. "You will learn from the Jedi Master who instructed me."
You'll learn the basics of reading and writing to sockets, and more importantly, that multi-threading isn't necessarily the right answer.
I would suggest using Qt Threading. It is highly documented with really excellent sample code on almost every feature. Plus they are LGPL licensed now and will run on most every platform and include the source code with the binaries. They also have very good network supoort.
Whatever way you choose, make sure that they have good documentation and samples
I'd suggest looking at the POCO libraries. In my opinion they are easier to get on with than boost and have excellent documentation. These libs provide great frameworks for writing multithreaded networking code. You can learn a lot from them and get up and running pretty quickly.
I suggest ACE. It has portable abstractions for many operating system functions (*nix, Windows etc): BSD sockets, Threads, Mutexes, Semaphores etc - write once compile anywhere (See ACE_OS namespace of ACE).
It has a lot of network application patterns you can use (ACE_Reactor would be good for the beginning) but you can use the portable abstractions of the BSD functions (socket, send, recv, close, select - they are enough for your IRC client)
As previously mentioned boost is also an option and usually any cross-platform library providing portable abstractions for each operating system (I can think of wxWidgets, qt for the graphical part - if you want to do this).
And one advice: do not use threads unless you really need to. They are not as easy as it seems.
When referring to the network communication I believe that what you want to do is easily achievable in a single threaded application(ACE_Reactor helps you a lot here but you are free to use the BSD socket functions). First understand how sockets work, then - if you want to - understand how the reactor makes use of sockets in its network application patterns(ACE_Reactor works in conjuction with ACE_Event_Handler objects).
Hope it helps!
I'd like to do some network socket programming in C++ and have found the C++ Sockets library.
First, is this a good way to go in C++? Normally in C, I'd use some of the stuff beej describes in his tutorial.
Second, how do I compile the examples given on the site? I can't figure it out from their installation/configuration guide. So I download the tar.gz to my Linux box, then what?
To have a specific example, how do I compile and run the DisplaySocket example?
Thanks.
EDIT: Thank you for the quick answers. A comment though. I'm not really looking into "understanding" network programming as I think I do that well enough already. I want to know if there's anything in particular to take advantage of in C++, and - if "the C++ Sockets Library" is a good choice - how to use it.
That's not "the" C++ sockets library, it's "a" C++ sockets library. Boost.asio has another (http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio.html).
(Community Wiki since I can't actually help you with your question - I've never compiled the code you ask about, so I don't know at what point you might have tripped over a problem).
Network programming would be better understood by using basic socket api (BSD or WinSock) rather than a socket library which hides most of the intricacies about sockets and their behaviour.
I would second the vote for boost::asio since it encapsulates the inversion of control model that is the current, preferred model, and appears to be standard-bound. To learn what the documentation doesn't tell you, google Douglas Schmidt and his books.
I like to use the ACE networking library when I write networking code in C++. I think it does a nice job abstracting some of the intricate details away that make network coding painful but doesn't do it to the point where it hides what is going on under the hood. It also has facilities for threading and messaging which usually are needed for any project.