Please tell me the most painless way of porting WSAAsyncSelect() function to GCC...
While select() and pselect() may work for your application, they are very much not the same thing as WSAAsyncSelect(). Those functions let you do controlled blocking on an otherwise non-blocking socket, or collection of sockets. The same goes for poll().
Winsock's asynchronous sockets, on the other hand, do not block, ever. There's also the large matter of async notifications, which your code doubtless depends on.
I do not believe there are any native APIs on OS X that provide similar behavior. However, it's possible to build up such a thing. A little Googling turned up CocoaAsyncSocket.
If you would rather not depend on third-party libraries, I suggest building up something on top of Cocoa's CFSocket, as the CocoaAsyncSocket developers did, if you will be porting over a GUI program, rather than dig down to core functions like select(). There's something to be said for using a single development framework for everything.
If you need your code to be cross-platform, the wxWidgets library has the wxSockets* class hierarchy, which emulates the Winsock async socket mechanism. Overall, wxWidgets is structured much like MFC, which eases porting if you're familiar with that.
I believe you want to look at the select function
I use pselect function.
Related
Node.js has a very good and well thought Net API.
I wonder is there a good C++ only implementation of that API as for example LuaNode do for Lua?
Take a look at node.native - attempt to implement api similar to node.js core lib, but with c++11 (and evented IO is also based on libuv)
There is nothing very similar that I know of.
However there are several reactor frameworks out there which give the same event queue driven environment. For example boost::asio provides an event queue that makes callbacks to handle network events, timers, and arbiatary events that you push onto the event queue.
It's largely the same idea, used in the same way. Howver it's nowhere near as simple as node.js to get started with, and does provide any non-blocking functions other than the basics I said above.
It does provide you with the environment to build your own system though. It's an excellent library, but probably rather lower level than you are looking for. There are other simiar libraries such as ACE and parts of the POCO c++ libraries too, but again, they are lower level than node.js with much less library support.
edit:
I've not looked at it too much but how about this https://github.com/joyent/libuv . This is a library that is used to implement some of the node.js features in a cross platform way. Maybe it's possible to use some of it for what you need?
Boost.Asio is conceptually very similar to Node.js. The primary difference being Asio is implemented as a library and Node.js is a language construct. Asio therefore exposes the event queue, requiring some initial setup to post callback handlers, and eventually a blocking call to start the event loop (io_service.run()).
If you're looking for a pure C++ API similar to Node.js, Boost.Asio is definitely the way to go. It is the de-facto networking library for many C++ applications. It's also discussed heavily on SO in the boost-asio tag.
I'm quite sure you could embed a Javascript engine into your program
v8 (building guide)
SpiderMonkey (building guide)
Actually tying that to your C code needs tinkering with the eval functions of both, but I think I remember seeing sample programs doing that for both engines
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!
First, I'm using Qt at the moment. However, I want the program eventually able to run without a GUI environment, leaving the graphical aspects for configuration mainly. The program makes hefty use of Qt timers and signals/slots, partially for QtScript. So if I want to make it non-GUI operable, hopefully parts of Qt can run without a GUI environment. If not, maybe I'll look into a different Javascript implementation, although QtScript is very convenient how it integrates into Qt's and C++'s OO structure. First, can parts of Qt be used in a non-GUI environment, and if not what other choices are there as far as an events and scheduling library? Preferably OO design.
If you don't use the QtGui module, you don't need a GUI. QtCore etc. will work just fine.
Have you looked at the
Boost.Signals library? (I haven't used it myself.)
libevent may be what you are looking for. This is in C, however.
The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts.
libsigc++ has a signals and slots mechanism very similar to Qt's though it's pure C++ (no extra pre-processor). It can also be used with gtkmm, a C++ binding for GTK+.
That said, I'd be surprised if Qt requires that you have a GUI, so you'll probably be able to stick with Qt.
The Poco project offers two interesting solutions:
Notification center: based on Cocoa/OpenStep's NSNotificationCenter
Events and delegates
The Boost signals library is very nice too, but it's one of the few boost libraries that need to be built and linked with.
Are there any c++ networking libs that are very useful and robust? and libs to help them be run better? something like automatically endian conversion when using <<, blocking reads until the struct or w/e your reading completely transfers, something to help debug your protocol, etc
Have you had a look at Boost.Asio? It's a networking library supporting both asynchronous and synchronous operation. I've made some experiments with it in the past, and found it quite useful.
I like the ADAPTIVE Communication Environment. It has built in constructs for just about all the networking patterns. I particullarly like ACE_Task. It makes message passing SO much easier.
I'd recommend sockets... its quite easy to make cross-platform code for Win32/*nix if you use it, although it is quite low level it does provide blocking functionality (i.e. halts execution until message is recieved). There are a ton of tutorials for sockets programming available... just google for "sockets" or "WinSock" (the Win32 flavour).
http://www.google.co.uk/search?q=sockets+programming
It won't deal with endian-ness for you, but there are a number of simple ways around this problem, like using a signature byte/word (e.g. 0xC1 (11000001b), 0x00C1) at the start of a message to determine the endian-ness.