is there any way to cross platform TCP/IP in C?

Discussion in 'Programming' started by Onceler, Feb 27, 2013.

  1. Onceler

    Onceler Golden Member

    Joined:
    Feb 28, 2008
    Messages:
    1,264
    Likes Received:
    0
    I am wanting to learn TCP/IP programing but want my apps to run on Windows,OSX,Linux, and possibly Droid. IS there any way of doing this(the learning I mean and also the implementation)? Google turns up Unix pages and stuff that only works on Linux or that depends on C#. I use Code::Blocks on Win7.
    Thanks
     
  2. Loading...

    Similar Threads - cross platform Forum Date
    Quick / Simple / Safe e-commerce platform? Programming Sep 18, 2017
    What's the best cross platform GUI library for C++? Programming Aug 25, 2017
    Favorite cross-platform GUI solution? Programming Apr 25, 2017

  3. Markbnj

    Markbnj Elite Member <br>Moderator Emeritus
    Moderator

    Joined:
    Sep 16, 2005
    Messages:
    15,688
    Likes Received:
    11
    I'm sure it's possible to write a cross-platform library using sockets in C, with some help from the preprocessor. But "apps" usually means some sort of interface, and at the GUI/console level it would be harder. I think you can call C from Java, so that might be one alternative.
     
  4. dighn

    dighn Lifer

    Joined:
    Aug 12, 2001
    Messages:
    22,820
    Likes Received:
    1
    If you stick to standard BSD socket calls such as send, recv, select etc., you can write a thin abstraction layer that lets you deal with TCP/IP in a platform-agnostic manner. I would not say it's easy though, there are many subtleties and archaic semantics you have to be aware of. You may want to use a library such as Boost.Asio.
     
  5. brandonb

    brandonb Diamond Member

    Joined:
    Oct 17, 2006
    Messages:
    3,731
    Likes Received:
    1
    Winsock in windows is basd on Berkeley sockets, many other systems are as well.

    http://en.wikipedia.org/wiki/Berkeley_sockets

    All you really need to do is watch the byte order... Big endian/little endian conversions. The network specs for TCPIP indicate everything as big endian. Windows is Little Endian.

    So when you place data into the packet, you have to potentially flipflop the byte order. This should be easy with a function named fliipflop that takes all the different data types (int, short, etc) when building your packet.

    And on Windows, you just exchange the byte order using the preprocessor definition functionality, while on systems that are big endian by default, you just ignore the code within the flipflop or leave it.
     
  6. Ancalagon44

    Ancalagon44 Diamond Member

    Joined:
    Feb 17, 2010
    Messages:
    3,114
    Likes Received:
    153
    Just use the Boost libraries. They are cross platform.

    Okay, they are C++, but why would you want to use C over C++ anyway, especially if Android is one of your targets?
     
  7. slugg

    slugg Diamond Member

    Joined:
    Feb 17, 2002
    Messages:
    4,683
    Likes Received:
    40
    This. C++ is just as portable as C, so long as you stay out of the OS API. If you need OS API, just abstract it into your own classes/interface.
     
  8. Markbnj

    Markbnj Elite Member <br>Moderator Emeritus
    Moderator

    Joined:
    Sep 16, 2005
    Messages:
    15,688
    Likes Received:
    11
    You can use the provided htonx and ntohx functions. I believe they're a standard feature of the berkeley sockets api.

    http://pubs.opengroup.org/onlinepubs/009695399/functions/htonl.html
     
  9. DaveSimmons

    DaveSimmons Elite Member

    Joined:
    Aug 12, 2001
    Messages:
    39,753
    Likes Received:
    310
    If you need to use HTTPS and/or to support proxies and smart cards, using a common wrapper around per-OS high level libraries might make more sense.

    For example on Windows if you use WinInet instead of "raw" sockets then it's pretty easy to support HTTPS and proxies, the libraries do it all for you. You also get cookie support, FTP, and more.

    If you really want to learn sockets and not programmatic access to HTTP/S, FTP, etc. then of course a library like WinInet is too high-level.
     
  10. TutorIndia

    TutorIndia Junior Member

    Joined:
    Mar 2, 2013
    Messages:
    3
    Likes Received:
    0
    Do a google for "Beej's Guide" and you should find the best guide to sockets you could ever want.