C++ Windows中UDP的C++头文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4432989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:24:08  来源:igfitidea点击:

C++ header files for UDP in Windows?

c++visual-c++socketsudpwinsock

提问by Richard Knop

I have a linux applications which sends data over UDP protocol. It uses these header files:

我有一个通过 UDP 协议发送数据的 linux 应用程序。它使用这些头文件:

#include <stdio.h>
/* standard C i/o facilities */
#include <stdlib.h>
/* needed for atoi() */
#include <unistd.h>

/* defines STDIN_FILENO, system calls,etc */
#include <sys/types.h> /* system data type definitions */
#include <sys/socket.h> /* socket specific definitions */
#include <netinet/in.h> /* INET constants and stuff */
#include <arpa/inet.h> /* IP address conversion stuff */
#include <netdb.h>
#include <string.h> /* for string and memset etc */
/* gethostbyname */
#include <iostream>

#include <fstream>

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h> 

I want to make a WIndows version of my app. But some of the above header files do not work in WIndows, especially those for UDP.

我想制作我的应用程序的 WINdows 版本。但是上面的一些头文件在 WINdows 中不起作用,尤其是那些用于 UDP 的头文件。

Which header files should I substitute them for in Windows (Visual Studio 2010)?

我应该在 Windows (Visual Studio 2010) 中替换哪些头文件?

UPDATE:

更新:

Ok, so my header now looks like this:

好的,所以我的标题现在看起来像这样:

#include <iostream>
#include <fstream>
#include "stdafx.h"

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

#include <winsock2.h>

I get this error when trying to compile (and many other similar errors):

尝试编译时出现此错误(以及许多其他类似错误):

Error   13  error C2011: 'fd_set' : 'struct' type redefinition  c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winsock2.h  132 1   Client

回答by Steve Townsend

At compile-time, you need to use Winsock2.hinstead of the Unix headers.

在编译时,您需要使用Winsock2.hUnix 头文件而不是 Unix 头文件。

At link-time, include ws2_32.libto provide linkage to the required system DLL.

在链接时,包含ws2_32.lib以提供到所需系统 DLL 的链接。

回答by Jay

Comment out the include files that are "missing" or put them into the following:

注释掉“丢失”的包含文件或将它们放入以下内容:

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <winsock2.h>
#include <windows.h>
#else
// unix includes here
#endif

回答by vanneto

You don't need most of those includes. The only file you will need is winsock2.hand link with ws2_32.lib.

您不需要其中的大部分内容。你唯一需要的文件是winsock2.h和链路与WS2_32.LIB

So, for all networking stuff, just include winsock2.h.

因此,对于所有网络内容,只需包含winsock2.h

回答by John Dibling

You want to #include winsock2.h. One peculiarity, you need to #include beforeincluding anything else, including :

你想#include winsock2.h。一个特性,您需要包含其他任何内容之前#include ,包括:

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