Sign in to follow this  
blaze02

Socket Programming Without Windows

Recommended Posts

I have a server app that I need to port to Linux. I removed all references to windows.h. Also, the app does not need to keep time so GetTickCount and QPC are not needed. I think the only trouble GCC has with my code is the networking socket stuff. GCC does not find the "winsock2.h" and "ws2tcpip.h" headers that I am using. I'm also using the wsock32.lib library. What headers and libraries to I need to reference for Linux? I'm assuming most of the function calls will be the same because everything is using Berkley sockets, right? Anything specific I will have to change? Thanks for any help.

Share this post


Link to post
Share on other sites
Sockets are usually part of libc; if not, it's typically -lsocket.

Typically, you use <sys/socket.h> and <sys/types.h> to get sockets.

In general, on Linux, if you want to know how to use function X, the easiest thing to do is to just "man X," and at the top, there will be a synopsis, which tells you what to include.

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603
Sockets are usually part of libc; if not, it's typically -lsocket.

Typically, you use <sys/socket.h> and <sys/types.h> to get sockets.

In general, on Linux, if you want to know how to use function X, the easiest thing to do is to just "man X," and at the top, there will be a synopsis, which tells you what to include.


Awesome, thanks. I've been thinking that if I get the headers right, the code will just compile and work. *Crosses fingers*

Share this post


Link to post
Share on other sites
Not really, do do linux / windows cross platform networking (in code) you typically have a lot of:

#if defined (__WIN32)

#elseif defined...

#endif

Windows uses the winsock libs, Linux, vxworks etc have the same effects but normally slightly different config / names for each methods so a straight port is likely to fail.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord Darkblade
Not really, do do linux / windows cross platform networking (in code) you typically have a lot of:

#if defined (__WIN32)

#elseif defined...

#endif

Windows uses the winsock libs, Linux, vxworks etc have the same effects but normally slightly different config / names for each methods so a straight port is likely to fail.


Just a few small differences in type names (can be typedef-ed), in the constants (remove WSA), and in the closesocket() being called close() (define an alias).

Share this post


Link to post
Share on other sites
Making a socket non-blocking is different between platforms as well (as Windows doesn't have fcntl())

I have a header called sock_port.h (or something similar) which takes care of the differences. I think that header is part of some of my socket sample code, such as Etwork.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this