tags:

views:

252

answers:

3

Hello,

I'm attempting to utilize the socket.h functions within Windows. Essentially, I'm currently looking at the sample code at http://beej.us/guide/bgnet/output/html/multipage/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.

A: 

I think you are looking for Winsock library.

Nikolai N Fetissov
+4  A: 

You have two options:

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

The first option lets one compile UNIX programs mostly untouched, but restricts to the CYGWIN emulation library. The second 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 your code.

Some related questions with valuable info:

http://stackoverflow.com/questions/1383286/differences-between-winsock-and-bsd-socket-implementations

http://stackoverflow.com/questions/2087435/cygwin-socket-thread-other-programming-issues-some-question-about-cygwin

http://stackoverflow.com/questions/2399377/examples-for-winsock

leonbloy
+1 for winsock and examples. I have been using winsock. I only need to change a small fraction of codes in order to port my Unix program to Windows. It is easy.
There are more surprises the other way around though :)For example, `select` on windows ignores the first argument, so naturally, winsock programmers put zero there for `maxfd`. Royal pain :)
Nikolai N Fetissov
A: 

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.

Iulian Şerbănoiu