Cross-Platform Sockets in C?

G

Garam

I need to write a substantial amount of network code in C. The thing
is, it has to be able to run on both Unix and Windows. Rather than
writing separate code for each, I was looking for something that would
abstract away the difference between sys/socket.h and winsock, and
provide me with a simple socket API that would work on both operating
systems.

Does anyone here know of such a library?

Thank you.
 
M

Mac

I need to write a substantial amount of network code in C. The thing
is, it has to be able to run on both Unix and Windows. Rather than
writing separate code for each, I was looking for something that would
abstract away the difference between sys/socket.h and winsock, and
provide me with a simple socket API that would work on both operating
systems.

Does anyone here know of such a library?

Thank you.

Your question is off-topic here, as are all questions about sockets.

FWIW, You may be able to write the code unix style, then use the mingw or
cygwin environment to compile the code for windows. If you search the web
on mingw and cygwin you should get plenty of hits.

There's also a chapter in the book "C unleashed" which deals with this
issue.

To get a better idea of what is and what is not on topic here, you can
read the clc faq here:

http://www.eskimo.com/~scs/C-faq/top.html

HTH
Mac
--
 
D

Dave Thompson

If you only want to do simple things -- just connect, send, recv,
etc., not nonblocking/aio, not treating as fd including fdopen and
passing to a child -- Unix (Berkeley) sockets and winsock can be
source compatible except for WSAStartup (and WSACleanup, which is no
longer really needed), a few typedefs names especially sd/SOCKET, and
the #definable errno location.
Your question is off-topic here, as are all questions about sockets.
(Standard) sockets belong AIUI in comp.unix.programmer; I don't know
where, if anywhere, directly addresses Berkeley vs winsock, although
my newserver lists a few winsock groups that sound like prospects.
FWIW, You may be able to write the code unix style, then use the mingw or
cygwin environment to compile the code for windows. If you search the web
on mingw and cygwin you should get plenty of hits.
Cygwin probably; mingw with only a few exceptions uses and exposes the
underlying Windows/MSC libraries, including winsock, and thus is no
more compatible than straight Windows code is (as above).
There's also a chapter in the book "C unleashed" which deals with this
issue.
Hence coming back almost on topic. :-?
To get a better idea of what is and what is not on topic here, you can
read the clc faq here:

http://www.eskimo.com/~scs/C-faq/top.html
Actually that's not a good reference for topicality; it doesn't
discuss such directly at all, and if you try to deduce from cases, it
includes a number of system specifics, plus some things that are not
really about C programming, which *are* or were frequently asked even
though off-topic.

http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
is simpler and clearer on topicality, and related netiquette.

- David.Thompson1 at worldnet.att.net
 
M

Mac

If you only want to do simple things -- just connect, send, recv,
etc., not nonblocking/aio, not treating as fd including fdopen and
passing to a child -- Unix (Berkeley) sockets and winsock can be
source compatible except for WSAStartup (and WSACleanup, which is no
longer really needed), a few typedefs names especially sd/SOCKET, and
the #definable errno location.

(Standard) sockets belong AIUI in comp.unix.programmer; I don't know
where, if anywhere, directly addresses Berkeley vs winsock, although
my newserver lists a few winsock groups that sound like prospects.

Cygwin probably; mingw with only a few exceptions uses and exposes the
underlying Windows/MSC libraries, including winsock, and thus is no
more compatible than straight Windows code is (as above).

I thought that might be the case. Cygwin code has license issues, since,
as I understand it (I might be wrong) any code which links with the cygwin
dll can be distributed only under the gpl. This wouldn't affect me, since
I only program for fun, but it might affect the OP.
Hence coming back almost on topic. :-?

Almost only counts in horseshoes. ;-)

Actually that's not a good reference for topicality; it doesn't
discuss such directly at all, and if you try to deduce from cases, it
includes a number of system specifics, plus some things that are not
really about C programming, which *are* or were frequently asked even
though off-topic.

http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
is simpler and clearer on topicality, and related netiquette.

Thanks for the tip. I'll try to remember that in the future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top