我是C89的新手,并试图做一些套接字编程:
void get(char *url) { struct addrinfo *result; char *hostname; int error; hostname = getHostname(url); error = getaddrinfo(hostname,NULL,&result); }
我正在Windows上开发。 Visual Studio抱怨说,如果使用这些include语句,就不会有这样的文件:
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h>
我该怎么办? 这是否意味着我将无法移植到Linux?
在Windows中执行SetMonitorBrightness函数时,无效的监视处理错误 – C ++
wdm.h中types名称“PRKMUTEX”中的“R”是什么意思?
replace为Windows XP上的:: SHCreateItemFromParsingName()
我怎样才能得到在.NET中的文件扩展名的描述
无法在新目录上检查“包含可inheritance的权限”
Python,试图打开一个命令
文件的安全stream更新
如何在Windows 8中安装,编译和使用钢筋
在Windows上,而不是你提到的包括,下面应该足够了:
#include <winsock2.h> #include <windows.h>
您还必须链接到ws2_32.lib 。 这样做很丑,但对于VC ++,你可以这样做: #pragma comment(lib,"ws2_32.lib")
在使用任何套接字函数之前,您将不得不调用WSAStartup() 。
close()现在被称为closesocket() 。
而不是通过套接字作为int ,有一个typedef SOCKET等于一个指针的大小。 尽管Microsoft有一个名为INVALID_SOCKET的宏来隐藏这个错误,但仍然可以使用-1来进行比较。
对于设置非阻塞标志的事情,您将使用ioctlsocket()而不是fcntl() 。
你必须使用send()和recv()来代替write()和read() 。
至于你是否会失去与Linux代码的可移植性,如果你开始编码Winsock …如果你不小心,那么是的。 但是你可以编写代码,试图用#ifdef来填补空白。
例如:
#ifdef _WINDOWS /* Headers for Windows */ #include <winsock2.h> #include <windows.h> #else /* Headers for POSIX */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> /* Mimic some of the Windows functions and types with the * POSIX ones. This is just an illustrative example; maybe * it'd be more elegant to do it some other way,like with * a proper abstraction for the non-portable parts. */ typedef int SOCKET; #define INVALID_SOCKET ((SOCKET)-1) /* OK,"inline" is a C99 feature,not C89,but you get the idea... */ static inline int closesocket(int fd) { return close(fd); } #endif
然后,一旦你做了这样的事情,你可以对两个操作系统中出现的函数进行编码,在适当的时候使用这些包装器。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。