What are the 'must know' concepts in the socket programming?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What are the 'must know' concepts in the socket programming?
I have worked previously with the Qt UDP and TCP sockets. We had to read from the sockets, store it somewhere, flush the buffer and read again and write.
But there must be something more to socket programming than just to read and write.
If I say I "know" socket programming, what the things, that are expected to be under my belt?
P.S:
People on StackOverflow told me to read about 'serialization' and 'apache thrift'.
Any other inputs are welcome.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Nobody's taken an expert approach (or any other) to your question here, so this amateur will give it a go.
"Knowing" socket programming is similar to "knowing" English. You'll never know everything, and what you know depends on what you need or want.
I had never heard of serialization (in a network context) or thrift (in a network context) until you mentioned them. But I've been doing network programming for years.
Anyway, here's where my wandering has led me:
Even in the ordinary area of TCP reading and writing, it's a good idea to get some experience doing that completely asynchronously (O_NONBLOCK), including the connect()s and accept()s, in a select() loop. (Or maybe poll()).
Also, if you want a thorough basis for your knowledge, get (and read) these books by W. Richard Stevens of happy memory:
The responses you got over on StackOverflow (a fine forum, by the way) ... sounds like they must have been smoking crack or something
It's kind of like if you asked "What should I learn about programming?", and they replied "You need to study up on closures and start using Haskell". C'mon!
Here's a MUCH better answer ... to a very SIMILAR question ... on the SAME forum:
I completely agree with wje_lq's suggestion - I have a well-worn copy of Stevens' "Unix Network Programming, vol 1" myself. You can't go wrong with any of his books. They're incredibly lucid - and they delve into great depth. Highly recommended!
Another suggestion is Beej's Guide to Network Programming:
Although I'm not a network programmer, IMO you should at least be aware of endianness. However, Qt provides helper classes for that (QDataStream). What else you would need to know depends on your task. It is possible to write network application without knowing all the internals.
I'd like to emphasize understanding the different ways socket communications can be used. Not just non-blocking I/O, but handling the SIGURG (out-of-band data arrived) or SIGPIPE (stream socket breaks) signals via fcntl() or ioctl(), too. How and why using send() or sendto() with MSG_MORE flag will often increase thoroughput. How to send out-of-band data using send() or sendto() with MSG_OOB. What are the pitfalls (failure modes, typical problems, risks) in each use case.
Understanding the methods and concepts is much more important than remembering any details. I make a point of not trying to memorize e.g. the flags to sendto(); when you write code, it takes only a few seconds to check the function in a reference, and to make sure you have the correct syntax. A good counterexample is the memset() call: the first parameter is the pointer, but is the size or the value the second parameter? Size is last, and memset(,,0) does nothing; still, memset(,,0) is regularly seen in C sources, including the Linux kernel.
You'll see a lot of interesting methods if you check how for example Apache and cgid or fastcgi communicate, or how MySQL or PostgreSQL do local connections.
As they say, if the only tool you have is a hammer, all problems look like nails. Having a wide and varied base to draw upon will help you find better solutions.
Thank you SigTerm and Nominal Animal for the helpful posts.
Following are the examples of the kinds of things which I need to understand:
1. QNetworkSession:
How that class works is explained in the Qt docs, but I am not clear with what is meant by a network session and how is it set in general?
2. QNetworkConfiguration:
The description says: In most cases a single access point configuration can be mapped to one network interface. However a single network interface may not always map to only one access point configuration. Multiple configurations for the same network device may enable multiple access points.
<sigh> These basic concepts are not clear to me Any brilliant and not too lengthy book for clearing off these basics? Networks was never my favourite subject. Perhaps never paid attention to it!
The beej's socket programming book deals with socket API descriptions, mostly.
No matter what it is that you're doing, it is extremely likely that you'll be solving a problem that has already been solved before, and that someone over the years has developed a higher-level protocol, based on sockets (perhaps), for doing it. They've already solved the protocol-problems (that are by definition built-upon the socket-or whatever-it-is "transport" layer), such that, if your due-diligence enables you to find it, you can utilize it "off the shelf." Before diving too far into a new implementation (of anything at all), "look well, before you leap."
I prefer to work with the back-end and low-level stuff. In fact, I normally stay very far away from frameworks, and much prefer modular libraries instead.
That said, I thought the BearerCloud and BearerMonitor examples illustrate the ideas and concepts pretty well, even if the names are atrocious, stupid buzzwords glued together. Yuk.
Here's my understanding:
The QNetworkSession class provides a way for a Qt application to choose how it connects to the network. The details are specified in a QNetworkConfiguration object (supplied to the QNetworkSession constructor).
When your application calls QNetworkSession::open(), it essentially tells the OS that this is the way your application wishes to connect to the network. If successful, that call opens a new network session. So, Qt calls the network connectivity configuration, and all associated network connections, a network session.
The OS uses that information to keep track of which interfaces it should keep available. When the last session on an interface closes, the OS can then for example power down the interface. That is also why your application needs to open a network session before making network connections: the network connection is likely unavailable otherwise.
For example, if your application runs on a device that can connect to the net via both GPRS and WLAN, it can use QNetworkConfigurationManager class to monitor connectivity and receive notifications of connectivity changes. The application can then use the QNetworkSession class to select which ones it wishes to use for its network connections.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.