Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sys/socket.h functions on windows

Tags:

c

windows

sockets

I'm attempting to utilize the socket.h functions within Windows. Essentially, I'm currently looking at the sample code at https://beej.us/guide/bgnet/html/multi/clientserver.html#datagram. I understand that socket.h is a Unix function -- is there anyway I can easily emulate that environment while compiling this sample code? Does a different IDE / compiler change anything?

Otherwise, I imagine that I need to utilize a virtualized Linux environment, which may be best anyways as the code will most likely be running in a UNIX environment.

Thanks.

like image 671
BSchlinker Avatar asked Jun 01 '10 19:06

BSchlinker


People also ask

How do I get sys socket H in Windows?

You have two options: Use Cygwin (Unix emulation library). Port to Winsock (Windows standard library).

What does Sys socket H do?

These macros are defined in `sys/socket. h'. This designates the IPv4 Internet namespace and associated family of protocols. This designates the IPv6 Internet namespace and associated family of protocols.

How do I use Winsock in Visual Studio?

To create a basic Winsock applicationCreate a new empty project. Add an empty C++ source file to the project. Ensure that the build environment refers to the Include, Lib, and Src directories of the Microsoft Windows Software Development Kit (SDK) or the earlier Platform Software Development Kit (SDK).


2 Answers

You have two options:

  1. Use Cygwin (Unix emulation library).
  2. Port to Winsock (Windows standard library).

Cygwin: lets you compile your Unix sources mostly untouched, but ties you to the Cygwin emulation library. This have two implications: general performance -no only network- will probably be less than optimal; and the target environment must have (at run time) the Cygwin DLL installed.

Winsock: this requires you to replace sys/socket.h (BSD sockets library, UNIX standard for the TCP/IP stack) with winsock2.h, and rewrite some parts of the code - not much, but some.

Some related questions with valuable info:

Differences between winsock and BSD socket implementations

Some Issues About Cygwin[Linux in Windows] (socket,thread,other programming and shell issues)

Examples for Winsock?

like image 165
leonbloy Avatar answered Sep 21 '22 15:09

leonbloy


Writing cross platform network applications is not easy with what the BSD standard provides you. Sure it will work but you'll have to make some replacements like replacing ioctl (if needed) with ioctlsocket (on windows). More differences here.

My advice is to use a library that hides these ugly differences and provides a unified way of communicating. I personally use ACE. You have plenty of examples that show you how to create a server and a client. Copy from the samples and see how they do it there. Their mailing lists are of great help also (don't forget to use the PRF - see the source tree for the Problem-Report-Form). You can borrow the books for more information. Important note: by using ace you can use ACE wrapper functions like socket, setsockopt, ioctl etc. without worry that they will not work. Unfortunately this adds a library dependency which is not always an option. But ACE has more powerfull features which I'm sure you'll like once you discover them. Hints: Reactor, Proactor.

like image 26
INS Avatar answered Sep 18 '22 15:09

INS