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

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
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
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
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.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
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.
 

Ancalagon44

Diamond Member
Feb 17, 2010
3,274
202
106
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?
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
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?

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.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
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.
 

TutorIndia

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