Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 02-27-2013, 09:33 PM   #1
Onceler
Golden Member
 
Onceler's Avatar
 
Join Date: Feb 2008
Posts: 1,247
Default 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
Onceler is offline   Reply With Quote
Old 02-27-2013, 10:42 PM   #2
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 12,887
Default

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.
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is online now   Reply With Quote
Old 02-28-2013, 02:03 AM   #3
dighn
Lifer
 
dighn's Avatar
 
Join Date: Aug 2001
Location: Canada
Posts: 22,644
Default

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.
dighn is online now   Reply With Quote
Old 02-28-2013, 09:11 AM   #4
brandonb
Diamond Member
 
Join Date: Oct 2006
Posts: 3,495
Default

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.
brandonb is offline   Reply With Quote
Old 02-28-2013, 09:17 AM   #5
Ancalagon44
Platinum Member
 
Join Date: Feb 2010
Posts: 2,163
Default

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

...and proceed to beat up Shinji Ikari for being a whiny little bitch.
Ancalagon44 is offline   Reply With Quote
Old 02-28-2013, 11:28 AM   #6
slugg
Diamond Member
 
slugg's Avatar
 
Join Date: Feb 2002
Location: Tampa, FL
Posts: 4,122
Default

Quote:
Originally Posted by Ancalagon44 View Post
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)
slugg is online now   Reply With Quote
Old 02-28-2013, 02:18 PM   #7
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 12,887
Default

Quote:
Originally Posted by brandonb View Post
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
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is online now   Reply With Quote
Old 02-28-2013, 02:34 PM   #8
DaveSimmons
Elite Member
 
Join Date: Aug 2001
Location: Bellevue, WA
Posts: 37,241
Default

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.
DaveSimmons is offline   Reply With Quote
Old 03-02-2013, 10:21 AM   #9
TutorIndia
Junior Member
 
Join Date: Mar 2013
Posts: 3
Default

Do a google for "Beej's Guide" and you should find the best guide to sockets you could ever want.
TutorIndia is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 02:28 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.