C89:Windows 上的 getaddrinfo() ?

发布于 2024-08-22 12:24:49 字数 457 浏览 10 评论 0原文

我是 C89 的新手,正在尝试进行一些套接字编程:

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

    hostname = getHostname(url);

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

}

我正在 Windows 上进行开发。如果我使用这些 include 语句,Visual Studio 会抱怨没有这样的文件:

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

我该怎么办?这是否意味着我无法移植到 Linux?

I'm new to C89, and trying to do some socket programming:

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. Visual Studio complains that there is no such file if I use these include statements:

#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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情换悲伤 2024-08-29 12:24:49

在 Windows 上,不需要您提到的包含内容,以下内容就足够了:

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

您还必须链接到 ws2_32.lib。这样做有点丑陋,但对于 VC++,您可以通过以下方式做到这一点: #pragma comment(lib, "ws2_32.lib")

Winsock 和 POSIX 之间的其他一些区别包括:

  • 您必须调用 WSAStartup()在使用任何套接字函数之前。

  • close() 现在称为 closesocket()

  • 不是将套接字作为 int 传递,而是使用一个等于指针大小的 typedef SOCKET。尽管 Microsoft 有一个名为 INVALID_SOCKET 的宏来隐藏此错误,但您仍然可以使用与 -1 进行比较。

  • 对于设置非阻塞标志之类的操作,您将使用 ioctlsocket() 而不是 fcntl()

  • 您必须使用 send()recv() 而不是 write()read()< /code>.

至于如果您开始为 Winsock 编写代码是否会失去 Linux 代码的可移植性……如果您不小心,那么是的。但是您可以编写尝试使用 #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

然后,一旦您执行了类似的操作,您就可以使用这些包装器针对两个操作系统中出现的函数进行编码在适当的情况下。

On Windows, instead of the includes you have mentioned, the following should suffice:

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

You'll also have to link to 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")

Some other differences between Winsock and POSIX include:

  • You will have to call WSAStartup() before using any socket functions.

  • close() is now called closesocket().

  • Instead of passing sockets as int, there is a typedef SOCKET equal to the size of a pointer. You can still use comparisons with -1 for error, though Microsoft has a macro called INVALID_SOCKET to hide this.

  • For things like setting non-blocking flags, you'll use ioctlsocket() instead of fcntl().

  • You'll have to use send() and recv() instead of write() and 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. But you can write code that tries to bridge the gaps using #ifdefs..

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文