如何使用Winsock 2?

aki*_*kif 0 c++ header file winsock

如何在Visual Studio 2008中使用Windows套接字2.我使用的是预编译的头文件,到目前为止我尝试的是:

  1. 在我的StdAfx.h文件中包含了winsock2.h
  2. 并在项目设置中输入WS2_32.LIB作为附加依赖项

我收到这些错误

------ Build started: Project: TestIVR, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\hussain\my documents\visual studio 2008\projects\testivr\testivr\main.cpp(30) : error C2065: 'WSAEVENT' : undeclared identifier
c:\documents and settings\hussain\my documents\visual studio 2008\projects\testivr\testivr\main.cpp(30) : error C2146: syntax error : missing ';' before identifier 'socketEvent'
c:\documents and settings\hussain\my documents\visual studio 2008\projects\testivr\testivr\main.cpp(30) : error C2065: 'socketEvent' : undeclared identifier
c:\documents and settings\hussain\my documents\visual studio 2008\projects\testivr\testivr\main.cpp(35) : error C2039: 'S_addr' : is not a member of 'in_addr'
        c:\program files\microsoft sdks\windows\v6.0a\include\inaddr.h(22) : see declaration of 'in_addr'
c:\documents and settings\hussain\my documents\visual studio 2008\projects\testivr\testivr\main.cpp(40) : error C2065: 'socketEvent' : undeclared identifier
c:\documents and settings\hussain\my documents\visual studio 2008\projects\testivr\testivr\main.cpp(40) : error C3861: 'WSAEventSelect': identifier not found
Build log was saved at "file://c:\Documents and Settings\Hussain\My Documents\Visual Studio 2008\Projects\TestIVR\TestIVR\Debug\BuildLog.htm"
TestIVR - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)

顺便说一句,如果我在我的main.cpp(我的main()函数所在的位置)中包含winsock2.h,那么我会得到不同的错误

------ Build started: Project: TestIVR, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\hussain\my documents\visual studio 2008\projects\testivr\testivr\main.cpp(36) : error C2039: 'S_addr' : is not a member of 'in_addr'
        c:\program files\microsoft sdks\windows\v6.0a\include\inaddr.h(22) : see declaration of 'in_addr'
Build log was saved at "file://c:\Documents and Settings\Hussain\My Documents\Visual Studio 2008\Projects\TestIVR\TestIVR\Debug\BuildLog.htm"
TestIVR - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)

以下是我的StdAfx.h头文件的内容

#pragma once

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <winsock2.h>
Run Code Online (Sandbox Code Playgroud)

Goz*_*Goz 5

那么你的in_addr问题是由于事实上in_addr中没有S_addr字段引起的.有一个in_addr.s_addr重定向到in_addr.S_un.S_addr.