Programming Unix Sockets under Windows

Started by
11 comments, last by VisualFX 20 years, 1 month ago
Hi people, I''d like to know if it is possible to program Unix sockets under Windows, using DevC++ or VC++. I mean, is it possible to compile a program with VC++ using libraries such as: sys/time.h sys/types.h sys/socket.h netinet/in.h and so on.. If so, where can I download those libraries. Yours Faithfully VisualFX
Advertisement
Unix and Windows are for 90% the same in their socket interface.
The only real difference is the WSAStartup and WSAShutdown you have to call in Windows.
And for Windows you need winsock2.h and windows.h to be included and winsock32.lib... I think.
Both OS-es use the Berkley Socket Interface, both for UDP and TCP.

I assume you also wish to use threads if you program sockets, but for that I would like to refer to MSDN.microsoft.com , but I know the essence is the same (works with handles), except that the calls are a tad different.

A few #ifdef''s and #defines can make your whole socket application cross platform.
STOP THE PLANET!! I WANT TO GET OFF!!
Here are some things you should take into account when using sockets in windoze:

- The only include file is winsock.h or winsock2.h , depending on which version of winsock you''re planning to use
- The library to link with is either wsock32.lib or ws2_32.lib, again depending on version. However, ws2_32.lib + winsock2.h should allow you to use winsock 1.0/1.1, too
- All socket code must be surrounded with WSAStartup() and WSACleanup()
- You cannot use eg. select() with stdio etc. (I don''t remember whether it can be used with files or not)
Thanks people,
But what type of file should I select in Visual C++?
Is is Console Application?
How can I link the library ws2_32.lib ?


Yours Faithfully
VisualFX
The application type doesn''t matter.

To link ws2_32.lib in VC6 select Settings... from the Project -menu, choose the Link -tab and add ws2_32.lib to the Object/library modules -text field.
Or, in visual, and ONLY for visual, use:
#pragma comment(lib, "WS2_32.LIB")

I mostly use the #pragma comment thingies because I can''t be arsed to go to the settings. Other compilers won''t accept this kind of nonsense by the way .
STOP THE PLANET!! I WANT TO GET OFF!!
In Dev-C++ the linking procedure is:
- Select Project->Project Options, Parameters -tab and click on Add Library or Object .
- Locate your library path (eg. g:\Dev-Cpp\lib )
- The library is libws2_32.a

There might be an easier way, not sure.

Also, one more exception in windoze vs unix sockets:
- The unix call close() is closesocket() in winsock


[edited by - nonpop on February 28, 2004 9:20:09 AM]
Hi,
I still get this error when I try to compile:

error C2660: ''WSAStartup'' : function does not take 0 parameters

Thanks
Unfortunately I can''t use
-> close(sd);
either...
Although I''m including:
#include <winsock2.h>
#include <winsock.h>
#include <windows.h>
#include <stdio.h>

Where may this function be?

Thanks
quote:Original post by VisualFX
I still get this error when I try to compile:

error C2660: ''WSAStartup'' : function does not take 0 parameters


Here''s how you use it:
WSADATA wsaData;WORD version = MAKEWORD(2,2);   // For Winsock 2.2if (WSAStartup (version, &wsaData) != 0){    // Error: Cannot initialize Winsock}if (wsaData.wVersion != version){    // Error: Requested WS version not available}


quote:
Unfortunately I can''t use
-> close(sd);


You have to replace close() with closesocket() (a macro will do that if you''re writing a cross-platform app

This topic is closed to new replies.

Advertisement