|
|
 |
02-27-2013, 09:33 PM
|
#1
|
|
Golden Member
Join Date: Feb 2008
Posts: 1,063
|
is there any way to cross platform TCP/IP in C?
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
|
|
|
02-27-2013, 10:42 PM
|
#2
|
|
Moderator Programming
Join Date: Sep 2005
Posts: 8,144
|
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.
|
|
|
02-28-2013, 02:03 AM
|
#3
|
|
Lifer
Join Date: Aug 2001
Location: Canada
Posts: 21,932
|
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.
|
|
|
02-28-2013, 09:11 AM
|
#4
|
|
Platinum Member
Join Date: Oct 2006
Posts: 2,620
|
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.
__________________
Computer Setup
Intel Ivy Bridge 3550, AsRock Z75 Pro3, 8 gig GSkill 1600 8-8-8-24 timing, Crucial m4 64gig, 500meg WD RE4, ATI 7970 3GB, LG IPS236V x 3, Windows 8, Antec Sonata Solo 2, Antec 550watt PS.
|
|
|
02-28-2013, 09:17 AM
|
#5
|
|
Golden Member
Join Date: Feb 2010
Posts: 1,497
|
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?
__________________
Paul Atreides, Rand al'Thor and Luke Skywalker walk into a bar.
Sh*t gets serious.
|
|
|
02-28-2013, 11:28 AM
|
#6
|
|
Diamond Member
Join Date: Feb 2002
Location: Tampa, FL
Posts: 3,547
|
Quote:
Originally Posted by Ancalagon44
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.
__________________
- ThinkPad T520 - srs bizniz
- MacBook Air 11'' - iOS dev bizniz
- 1999 Mazda Miata - (street legal gokart on steroids!)
- 2002 Mazda Protege - (Work in progress)
|
|
|
02-28-2013, 02:18 PM
|
#7
|
|
Moderator Programming
Join Date: Sep 2005
Posts: 8,144
|
Quote:
Originally Posted by brandonb
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.
|
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...ons/htonl.html
|
|
|
02-28-2013, 02:34 PM
|
#8
|
|
Elite Member
Join Date: Aug 2001
Location: Bellevue, WA
Posts: 35,484
|
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.
|
|
|
03-02-2013, 10:21 AM
|
#9
|
|
Junior Member
Join Date: Mar 2013
Posts: 3
|
Do a google for "Beej's Guide" and you should find the best guide to sockets you could ever want.
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:19 PM.
|