Thread: C Linux sockets vs C++ linux sockets

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    C Linux sockets vs C++ linux sockets

    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++?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  3. #3
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    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.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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);
    //}

  6. #6
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    So then Brewbuck, back to my riginal question; Is there an advantage to writing Linux TCP sockets in C vs C++?

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Annonymous View Post
    So then Brewbuck, back to my riginal question; Is there an advantage to writing Linux TCP sockets in C vs C++?
    The problem with doing it in C is it's not C++.

    The problem with doing it in C++ is it's not C.

    Select either/both, depending on what language you WANT to be using.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Annonymous
    Is there an advantage to writing Linux TCP sockets in C vs C++?
    Yes.

    Quote Originally Posted by laserlight
    Is there an advantage to writing Linux TCP sockets in C++ vs C?
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. UDP Sockets (linux)
    By avidkaos999 in forum C++ Programming
    Replies: 26
    Last Post: 01-17-2012, 04:30 AM
  2. UDP Sockets in C under Linux
    By goodsamaritan in forum C Programming
    Replies: 6
    Last Post: 03-23-2011, 06:02 AM
  3. Linux Sockets in C
    By Scythed in forum C Programming
    Replies: 8
    Last Post: 11-07-2010, 10:23 AM
  4. (Where) learning linux tcp/ip sockets
    By codecaine_21 in forum Networking/Device Communication
    Replies: 4
    Last Post: 09-17-2010, 05:54 PM
  5. Need help with Linux sockets
    By junbin in forum Linux Programming
    Replies: 1
    Last Post: 07-21-2002, 12:42 PM