views:

1128

answers:

4

How do I use Windows Sockets 2 in Visual Studio 2008. I'm using precompiled headers, so far what I have tried is:

  1. Included winsock2.h in my StdAfx.h file
  2. and entered WS2_32.LIB as an additional dependency in Project Settings

I get these errors

------ 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 ==========

By the way, if I include the winsock2.h in my main.cpp (where my main() function resides) then I get different errors

------ 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 ==========

Following is the content for my StdAfx.h header file

#pragma once

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <winsock2.h>
+3  A: 

You need to include winsock2.h before windows.h

nos
And why that so?
hab
+3  A: 

Well your in_addr issue is caused byt he fact there IS no S_addr field in in_addr. There is an in_addr.s_addr which re-directs to in_addr.S_un.S_addr.

Goz
Thanks sir, that worked perfectly
hab
+4  A: 

From memory I think windows.h includes the winsock.h (i.e. sockets v1), which conflicts with winsock2.h. You can prevent this by defining WIN32_LEAN_AND_MEAN :

#include <stdio.h>
#include <tchar.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
jon hanson
A: 

Try browsing

http://www.sockets.com/ for examples

sameer karjatkar