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. Markbnj

    Markbnj Elite Member <br>Moderator Emeritus
    Moderator

    Joined:
    Sep 16, 2005
    Messages:
    15,688
    Likes Received:
    8
    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.
     
  3. dighn

    dighn Lifer

    Joined:
    Aug 12, 2001
    Messages:
    22,818
    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.
     
  4. 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.
     
  5. Ancalagon44

    Ancalagon44 Platinum Member

    Joined:
    Feb 17, 2010
    Messages:
    2,828
    Likes Received:
    39
    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?
     
  6. slugg

    slugg Diamond Member

    Joined:
    Feb 17, 2002
    Messages:
    4,588
    Likes Received:
    26
    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.
     
  7. Markbnj

    Markbnj Elite Member <br>Moderator Emeritus
    Moderator

    Joined:
    Sep 16, 2005
    Messages:
    15,688
    Likes Received:
    8
    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
     
  8. DaveSimmons

    DaveSimmons Elite Member

    Joined:
    Aug 12, 2001
    Messages:
    39,142
    Likes Received:
    136
    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.
     
  9. 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.