akif
akif

Reputation: 12334

How to use Winsock 2?

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>

Upvotes: 0

Views: 5896

Answers (4)

sameer karjatkar
sameer karjatkar

Reputation: 2057

Try browsing

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

Upvotes: 0

jon hanson
jon hanson

Reputation: 9408

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>

Upvotes: 4

Goz
Goz

Reputation: 62323

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.

Upvotes: 5

nos
nos

Reputation: 229108

You need to include winsock2.h before windows.h

Upvotes: 3

Related Questions