Programming Unix Sockets under Windows

Started by
11 comments, last by VisualFX 20 years, 1 month ago
Thanks a lot everyone!
Advertisement
quote:Original post by Structural
Other compilers won''t accept this kind of nonsense by the way .


That''s because pragmas are intended by the C standard to be compiler-dependent directives.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
From my socket class (works on windows or linux):
#ifndef __SOCKET_H__#define __SOCKET_H__// Headers //#include <stdlib.h>#include <stdio.h>#include <memory.h>#ifdef WIN32#	include <winsock2.h>#else#	include <sys/types.h>#	include <sys/socket.h>#	include <netinet/in.h>#	include <arpa/inet.h>#	include <tcpd.h>#	include <netdb.h>#	include <errno.h>#	include <unistd.h>#endif#include <string>#include <vector>// Libraries //#pragma comment (lib,"ws2_32.lib")// Prototypes#ifndef WIN32#	define SOCKET	int#	define DWORD	unsigned long#	define WORD		unsigned short#	define BYTE		unsigned char#	define Error	errno#	define INVALID_SOCKET		-1#	define SOCKET_ERROR			-1#	define closesocket(sock)	close(sock)#	define ZeroMemory(d,l)		memset((d),0,(l))#	define CopyMemory(d,s,l)	memcpy((d),(s),(l))#else#	define Error		WSAGetLastError()#	define socklen_t	int#endif

If you want the whole class, you can get it from Here, although its an old version and might have some bugs in it

Edit:
Oh yeah, i also do this in Socket.cpp (to handle the WSAStartup, etc):
#ifdef WIN32class CInit{public:	CInit()	{	WSADATA wsaData;#ifdef _DEBUG		if(WSAStartup(MAKEWORD(1,1),&wsaData))			OutputDebugString("WARNING: Failed to initialise windows sockets!\n");		else			OutputDebugString("Sockets Library: WSAStartup() Successful\n");#else		WSAStartup(MAKEWORD(1,1),&wsaData);#endif	}	~CInit()	{		WSACleanup();#ifdef _DEBUG		OutputDebugString("Sockets Library: WSACleanup() Successful\n");#endif	}};CInit g_Init;#endif



[edited by - Evil Steve on March 3, 2004 6:06:08 AM]

This topic is closed to new replies.

Advertisement