简体   繁体   English

C89:Windows上的getaddrinfo()?

[英]C89: getaddrinfo() on Windows?

I'm new to C89, and trying to do some socket programming: 我是C89的新手,并试图做一些套接字编程:

void get(char *url) {
    struct addrinfo *result;
    char *hostname;
    int error;

    hostname = getHostname(url);

    error = getaddrinfo(hostname, NULL, NULL, &result);

}

I am developing on Windows. 我在Windows上开发。 Visual Studio complains that there is no such file if I use these include statements: 如果我使用这些include语句,Visual Studio会抱怨没有这样的文件:

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

What should I do? 我该怎么办? Does this mean that I won't have portability to Linux? 这是否意味着我将无法移植到Linux?

On Windows, instead of the includes you have mentioned, the following should suffice: 在Windows上,而不是您提到的包含,以下内容应该足够:

#include <winsock2.h>
#include <windows.h>

You'll also have to link to ws2_32.lib . 您还必须链接到ws2_32.lib It's kind of ugly to do it this way, but for VC++ you can do this via: #pragma comment(lib, "ws2_32.lib") 这样做有点难看,但对于VC ++,你可以通过以下方式实现: #pragma comment(lib, "ws2_32.lib")

Some other differences between Winsock and POSIX include: Winsock和POSIX之间的其他一些区别包括:

  • You will have to call WSAStartup() before using any socket functions. 在使用任何套接字函数之前,您必须调用WSAStartup()

  • close() is now called closesocket() . close()现在称为closesocket()

  • Instead of passing sockets as int , there is a typedef SOCKET equal to the size of a pointer. 而不是将套接字作为int传递,而是有一个等于指针大小的typedef SOCKET You can still use comparisons with -1 for error, though Microsoft has a macro called INVALID_SOCKET to hide this. 尽管Microsoft有一个名为INVALID_SOCKET的宏来隐藏它,你仍然可以使用-1-1进行比较。

  • For things like setting non-blocking flags, you'll use ioctlsocket() instead of fcntl() . 对于设置非阻塞标志的事情,您将使用ioctlsocket()而不是fcntl()

  • You'll have to use send() and recv() instead of write() and read() . 你必须使用send()recv()而不是write()read()

As for whether or not you will lose portability with Linux code if you start coding for Winsock... If you are not careful, then yes. 至于你是否会因为开始为Winsock编码而失去Linux代码的可移植性......如果你不小心,那么是的。 But you can write code that tries to bridge the gaps using #ifdef s.. 但是你可以编写代码来尝试使用#ifdef来弥补差距。

For example: 例如:

#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

Then once you do something like this, you can code against the functions which appear in both OS's, using these wrappers where appropriate. 然后,一旦你执行了这样的操作,就可以在适当的情况下使用这些包装器对两个操作系统中出现的函数进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM