C++ header files for UDP in Windows?

旧巷老猫 提交于 2020-06-24 12:34:09

问题


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

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

Which header files should I substitute them for in 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

回答1:


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

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




回答2:


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



回答3:


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

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




回答4:


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

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


来源:https://stackoverflow.com/questions/4432989/c-header-files-for-udp-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!