I see a lot of people ask why code sockets in C and not C++. I know you can code them in C++ but i see a great deal more written in C instead. Why is this? Is there an advantage to writing Linux TCP sockets in C vs C++?
I see a lot of people ask why code sockets in C and not C++. I know you can code them in C++ but i see a great deal more written in C instead. Why is this? Is there an advantage to writing Linux TCP sockets in C vs C++?
Just hazarding a guess, but "class Socket" by itself would only be a very thin wrapper around the existing socket API. It's not that you can't do it, but it is a lot of effort for not a great deal of benefit.
On the other hand, "class HTMLSocket" which wrapped up a lot of the messyness and gave you interfaces like "readHeader" and "readBody" would be worth doing.
But why not try it for yourself - nothing beats some real concrete experience to drive those lessons home.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I do not want to learn C++, I have zero interest in it. ALthough I just might because of your suggestion to "Drive the lesson home!". I was just asking out of curiosity. The answer I came up with is; C++ to my understanding is just a buffed up version of C. So C++ just uses a library to wrap the C Berkely socket API anyway so why not just use C directly? Cut out the middle man? My answer probably far off tho.
There is a concept known as "return on investment" that is relevant to answering your question.
C++ is often treated as a "buffed up version of C" by people who don't properly understand either C or C++. Neither the C nor C++ standards support anything to do with networking. APIs and libraries like those concerned with Berkley sockets are therefore an add-on library for both languages. C is not the "middleman" between C++ and any sockets API.
The successful class libraries do a lot more than just wrap thinly wrap some existing API .... they support higher levels of abstraction and functionality (for example, a HTMLSocket mentioned by Salem). In practice, the effort of using a thin wrapper over an API is comparable with using the API directly .... so there is little point in doing it.
But that has nothing to do with the difference between C and C++. It is also possible to write thin function wrappers, in C, on top of a basic sockets API. But, again, it is not worth it unless a significant amount of additional functionality or abstraction is introduced.
Last edited by grumpy; 01-23-2012 at 01:10 PM.
There is no such thing as a C++ socket API. You can use sockets from C++ the same way you use them in C. If you want to get fancy with a class or something, that's your choice.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
So then Brewbuck, back to my riginal question; Is there an advantage to writing Linux TCP sockets in C vs C++?
Yes.Originally Posted by Annonymous
Yes.Originally Posted by laserlight
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)