跨平台插座 [英] Cross-platform sockets
问题描述
我知道,而Mac OS不Windows不使用Unix套接字。直到此时我的工具是没有任何code变化跨平台的。但现在我想要它做的一些网络通信。我知道POSIX插座,但我一无所知Windows的人。我们的目标是实现一个简单的跨平台套接字服务器。
I know, Windows doesn't use UNIX sockets while Mac OS does. Until this point my tool was cross-platform without any code changes. But now I want it to do some network communication. I know about POSIX sockets, but I know nothing about Windows' ones. The goal is to implement a simple cross-platform socket server.
能否请您推荐一个库或code例如,可以用在UNIX和Windows平台?
Could you please recommend a library or code example that could help with usage of sockets on both UNIX and Windows platforms?
推荐答案
<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms741394%28v=vs.85%29.aspx\">WinSock和 POSIX 插座以类似的方式工作 - 主要是因为的Windows插座最初是基于code从BSD :
WinSock versus POSIX Sockets
WinSock and POSIX sockets work in a similar manner - mainly because Windows sockets were originally based on code from BSD:
虽然这些专有BSD衍生品是由UNIX System V发行版4和OSF / 1系统在上世纪90年代(这两者结合BSD code和其他现代Unix系统的基础上),后来BSD版本在很大程度上取代几个开源开发项目,如提供了基础FreeBSD的,OpenBSD系统,NetBSD的,达尔文或PC-BSD,也正在进行中。这些,反过来,已经全部或在现代专有操作系统,例如部分并入的TCP / IP(仅限IPv4)的网络code在Microsoft Windows 并大部分苹果的OS X和iOS的基础。
Although these proprietary BSD derivatives were largely superseded by the UNIX System V Release 4 and OSF/1 systems in the 1990s (both of which incorporated BSD code and are the basis of other modern Unix systems), later BSD releases provided a basis for several open source development projects, e.g. FreeBSD, OpenBSD, NetBSD, Darwin or PC-BSD, that are ongoing. These, in turn, have been incorporated in whole or in part in modern proprietary operating systems, e.g. the TCP/IP (IPv4 only) networking code in Microsoft Windows and most of the foundation of Apple's OS X and iOS.
不过,也有如果你想写就需要不同的处理一些事情插座库不可知code。
However, there are a few things you'll need to handle differently if you want to write "socket-library-agnostic" code.
的注:下面的例子已经用code ::在Windows XP(x86)的和Debian测试(AMD64)块和GCC测试的
您需要包括这取决于你使用的是Windows还是不不同的头文件:
You'll need to include different header files depending on whether you're using Windows or not:
#ifdef _WIN32
/* See http://stackoverflow.com/questions/12765743/getaddrinfo-on-win32 */
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501 /* Windows XP. */
#endif
#include <winsock2.h>
#include <Ws2tcpip.h>
#else
/* Assume that any non-Windows platform uses POSIX-style sockets instead. */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h> /* Needed for getaddrinfo() and freeaddrinfo() */
#include <unistd.h> /* Needed for close() */
#endif
您还需要在Windows上使用 WS2_32
LIB文件链接。
You'll also need to link with Ws2_32
lib file on Windows.
下面的函数说明如何初始化的WinSock v1.1和事后清理:
The functions below illustrate how you can initialise WinSock v1.1 and clean up afterwards:
int sockInit(void)
{
#ifdef _WIN32
WSADATA wsa_data;
return WSAStartup(MAKEWORD(1,1), &wsa_data);
#else
return 0;
#endif
}
int sockQuit(void)
{
#ifdef _WIN32
return WSACleanup();
#else
return 0;
#endif
}
套接字句柄是无符号Winsock的
有关POSIX式套接字,你可以简单地使用 INT
来存储一个套接字句柄。无效插座由负值表示。
Socket handles are UNSIGNED on Winsock
For POSIX-style sockets, you can simply use int
to store a socket handle. Invalid sockets are indicated by a negative value.
不过,的WinSock套接字是无符号整数一>,以代替负数的特殊常量( INVALID_SOCKET
)。
However, WinSock sockets are UNSIGNED integers, with a special constant (INVALID_SOCKET
) used instead of negative numbers.
您可以抽象的POSIX和隐藏的有效的套接字检查的差异通过的typedef
ING SOCKET为 INT
后面的宏或功能。
You can abstract the differences by typedef
ing SOCKET as int
on POSIX and hiding the "valid socket" check behind a macro or function.
下面的函数说明了差异:
The function below illustrates the differences:
/* Note: For POSIX, typedef SOCKET as an int. */
int sockClose(SOCKET sock)
{
int status = 0;
#ifdef _WIN32
status = shutdown(sock, SD_BOTH);
if (status == 0) { status = closesocket(sock); }
#else
status = shutdown(sock, SHUT_RDWR);
if (status == 0) { status = close(sock); }
#endif
return status;
}
虽然在一般,他们是pretty相似。
如果你坚持共同功能(如发送()
或的recv()
),并避免平台具体的东西(如<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms742219%28v=vs.85%29.aspx\"><$c$c>WSAWaitForMultipleEvents()$c$c>)那么你应该罚款。
In general though, they're pretty similar.
If you stick to "common" functions (such as send()
or recv()
) and avoid platform-specific stuff (such as WSAWaitForMultipleEvents()
) then you should be fine.
这篇关于跨平台插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!