windows.h and winsock.h in visual c++ 2005

Hi I was writing an Network Application in visual C++ 6.0 any time I wrote #include<windows.h>nothing was wrong as well a winsock.h
but when it comes to visual c++ 2005 Error comes saying "windos.h" no such file or directory and when I skipped the windows.h the next error comes saying "winsock.h" no such file or directory.Also I need some tutorials on how links dll in the application eg.winsock.dll ,opengl32.dll which are from system directory.
The code is :
//winsock.cpp
#include <windows.h>
#include <iostream.h>
#include <winsock.h>
#define NO_FLAGS_SET 0
#define PORT (u_short) 44966
#define MAXBUFLEN 256

INT main(VOID)
{
WSADATA Data;
SOCKADDR_IN recvSockAddr;
SOCKET recvSocket;
int status;
int numrcv;
char buffer[MAXBUFLEN];

/* initialize the Windows Socket DLL */
status=WSAStartup(MAKEWORD(1, 1), &Data);
if (status != 0)
cerr << "ERROR: WSAStartup unsuccessful"
<< endl;

/* zero the sockaddr_in structure */
memset(&recvSockAddr, 0, sizeof(recvSockAddr));
/* specify the port portion of the address */
recvSockAddr.sin_port=htons(PORT);
/* specify the address family as Internet */
recvSockAddr.sin_family=AF_INET;
/* specify that the address does not matter */
recvSockAddr.sin_addr.s_addr=htonl(INADDR_ANY);

/* create a socket */
recvSocket=socket(AF_INET, SOCK_DGRAM, 0);
if (recvSocket == INVALID_SOCKET)
cerr << "ERROR: socket unsuccessful" << endl;

/* associate the socket with the address */
status=bind(recvSocket,
(LPSOCKADDR) &recvSockAddr,
sizeof(recvSockAddr));
if (status == SOCKET_ERROR)
cerr << "ERROR: bind unsuccessful" << endl;

while(1)
{
numrcv=recvfrom(recvSocket, buffer, MAXBUFLEN,
NO_FLAGS_SET, NULL, NULL);
if (numrcv == SOCKET_ERROR)
{
cerr << "ERROR: recvfrom unsuccessful"
<< endl;
status=closesocket(recvSocket);
if (status == SOCKET_ERROR)
cerr << "ERROR: closesocket unsuccessful"
<< endl;
status=WSACleanup();
if (status == SOCKET_ERROR)
cerr << "ERROR: WSACleanup unsuccessful"
<< endl;
return(1);
}
cout << buffer << endl;
} /* while */
}

compiled it in ms vc++ 2005
and I got the following errors

c:\programs\a\a\a.cpp(17) : error C2065: 'WSADATA' : undeclared identifier
c:\programs\a\a\a.cpp(17) : error C2146: syntax error : missing ';' before identifier 'Data'
c:\programs\a\a\a.cpp(17) : error C2065: 'Data' : undeclared identifier
c:\programs\a\a\a.cpp(18) : error C2065: 'SOCKADDR_IN' : undeclared identifier
c:\programs\a\a\a.cpp(18) : error C2146: syntax error : missing ';' before identifier 'recvSockAddr'
c:\programs\a\a\a.cpp(18) : error C2065: 'recvSockAddr' : undeclared identifier
c:\programs\a\a\a.cpp(19) : error C2065: 'SOCKET' : undeclared identifier
c:\programs\a\a\a.cpp(19) : error C2146: syntax error : missing ';' before identifier 'recvSocket'
c:\programs\a\a\a.cpp(19) : error C2065: 'recvSocket' : undeclared identifier
c:\programs\a\a\a.cpp(25) : error C3861: 'WSAStartup': identifier not found
c:\programs\a\a\a.cpp(25) : error C3861: 'MAKEWORD': identifier not found
c:\programs\a\a\a.cpp(31) : error C2070: ''unknown-type'': illegal sizeof operand
c:\programs\a\a\a.cpp(33) : error C2228: left of '.sin_port' must have class/struct/union
type is ''unknown-type''
c:\programs\a\a\a.cpp(33) : error C2065: 'u_short' : undeclared identifier
c:\programs\a\a\a.cpp(33) : error C2143: syntax error : missing ')' before 'constant'
c:\programs\a\a\a.cpp(33) : error C2059: syntax error : ')'
c:\programs\a\a\a.cpp(35) : error C2228: left of '.sin_family' must have class/struct/union
type is ''unknown-type''
c:\programs\a\a\a.cpp(35) : error C2065: 'AF_INET' : undeclared identifier
c:\programs\a\a\a.cpp(33) : error C3861: 'htons': identifier not found
c:\programs\a\a\a.cpp(37) : error C2228: left of '.sin_addr' must have class/struct/union
type is ''unknown-type''
c:\programs\a\a\a.cpp(37) : error C2228: left of '.s_addr' must have class/struct/union
c:\programs\a\a\a.cpp(37) : error C2065: 'INADDR_ANY' : undeclared identifier
c:\programs\a\a\a.cpp(37) : error C3861: 'htonl': identifier not found
c:\programs\a\a\a.cpp(40) : error C2065: 'SOCK_DGRAM' : undeclared identifier
c:\programs\a\a\a.cpp(40) : error C3861: 'socket': identifier not found
c:\programs\a\a\a.cpp(41) : error C2065: 'INVALID_SOCKET' : undeclared identifier
c:\programs\a\a\a.cpp(46) : error C2065: 'LPSOCKADDR' : undeclared identifier
c:\programs\a\a\a.cpp(47) : error C2070: ''unknown-type'': illegal sizeof operand
c:\programs\a\a\a.cpp(45) : error C3861: 'bind': identifier not found
c:\programs\a\a\a.cpp(48) : error C2065: 'SOCKET_ERROR' : undeclared identifier
c:\programs\a\a\a.cpp(53) : error C3861: 'recvfrom': identifier not found
c:\programs\a\a\a.cpp(59) : error C3861: 'closesocket': identifier not found
c:\programs\a\a\a.cpp(63) : error C3861: 'WSACleanup': identifier not found
I dont know whats the problem and can any one who has got the tutorials on how to links dll's in the application will also be helpful.thanks.
ok try this:

1. Put the include files in the following order.
1
2
#include <winsock2.h> //can also be winsock.h
#include <windows.h> 


2. To set the library
Bring up the properties box for the project.
Select all configurations.
In the linker section on the left handside - select Input and in the right hand side add
ws2_32.lib in the Additional Dependencies box



(Note 1. On my Visual Studio 2008 - just #include <windows.h> was enough _ don't know if it's the same on 2005)

(Note 2 just #include <iostream> not #include <iostream.h>


Last edited on
Ok Guestgulkan I'll try but I have a Question when I include the winsock2.h is the winsock.dll have to be in the same directory with where the ".cpp" is of the winsock2.h have got all I need?Thanks.
you shouldn't have to worry about winsock.dll - it is in the windows system(32) directory.
If you are unsure - do a search you should find it there.
I tried to do the way you told me it seems to work coz now I receive errors just like before but the problem is here:
code:
#include<windows.h>
#include<winsock.h>
#include<iostream>
using namespace std;
int main()
{
WSADATA data; //winsock will fill this with structure with information
int status;
//just this function to see if the winsock.dll is initialized
status=WSAStartup(MAKEWORD(2,2),&data);
if(status==0)
{
cout<<"Socket created."<<endl;
}
else
{
cout<<"Error code:"<<GetLastError()<<endl;
WSACleanup();
}
return 0;
}

from here a certain error come saying "winsock ":redefinition something like that.Can you show me a little example how you create a simple socket (a windows socket or a normal socket).thanks.
Include winsock.h before windows.h and those redefinitions should go away.
Actually I took your code and changed it a little bit -
I'll post it tonight (UK time) - as it's time to go do the day job. :-)
IT WORKS! Just like the way you told me to I told the compiler to link the winsock library and everything went smoothly but The first application had no problem during compilation time but the second .cpp file in the same project I receive an Error:
Lnk2005:_main already defined in wn32.obj
Debug/socket(this is the app. name) :fatal error LNK1169 :one or more multiply defined symbols found
Error executing link.exe.
But when I tried to create A new project and I copy n pasted the same code of the first project nothing is wrong I believe its not a problem.I'l wait for you corrections on the code posted ealier.Thanks
Last edited on
here is some code as a sort of starter - pretty much the same as yours.
Please note:
1. The connect, send, recv functions are called blocking functions. This means that they will not return until the socket is connected, or the requested amount of data has been sent or receive or they time out - which can take some time.
The similar name functions but starting with WSA are what should be used in parctice. (for example WSAConnect).

2. The IP address I used is that for www.microsoft.com. That's why the port number is 80 (the default for a WEB server).
The data I send is just part of a request header - but not all of it - so the server should reapond 'Bad Request'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <winsock.h>
#include <windows.h>
#include <iostream>
#define NO_FLAGS_SET 0
#define PORT (u_short) 80 //44966
#define MAXBUFLEN 256

using namespace std;

INT main(VOID)
{
    WSADATA Data;
    SOCKADDR_IN recvSockAddr;
    SOCKET recvSocket;
    int status;
    int numrcv=0;
    char buffer[MAXBUFLEN];
    memset(buffer,0,MAXBUFLEN);

    /* initialize the Windows Socket DLL */
    status=WSAStartup(MAKEWORD(1, 1), &Data);
    if (status != 0)
    {
        cerr << "ERROR: WSAStartup unsuccessful" << endl;
        return 0;
    }

    /* zero the sockaddr_in structure */
    memset(&recvSockAddr, 0, sizeof(recvSockAddr));
    /* specify the port portion of the address */
    recvSockAddr.sin_port=htons(PORT);
    /* specify the address family as Internet */
    recvSockAddr.sin_family=AF_INET;
    /* specify that the address does not matter */
    recvSockAddr.sin_addr.s_addr= inet_addr( "65.55.21.250" );



/* create a socket */
recvSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (recvSocket == INVALID_SOCKET)
{
    cerr << "ERROR: socket unsuccessful" << endl;
    system("pause");
    return 0;
}



    //Try to connect
    if ( connect(recvSocket,(SOCKADDR*)&recvSockAddr,sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
    {
        cout << "Socket connection Failed" << endl;
        closesocket(recvSocket);
        WSACleanup();
        return 0;
    }

    //send a request
    send(recvSocket, "GET \r\n\r\n",12,0);

    numrcv=recv(recvSocket, buffer, MAXBUFLEN, NO_FLAGS_SET);
    
    if (numrcv == SOCKET_ERROR)
    {
        cerr << "ERROR: recvfrom unsuccessful" << endl;

        status=closesocket(recvSocket);
        if (status == SOCKET_ERROR)
        cerr << "ERROR: closesocket unsuccessful" << endl;
        status=WSACleanup();
        if (status == SOCKET_ERROR)
        cerr << "ERROR: WSACleanup unsuccessful" << endl;
        system("pause");
        return(1);
    }

    cout << buffer << endl;
    system("pause");
}
Hi!

The problem is with the newest versions of Visual C++, both 2005 and 2008. They both now piggie back the "Windows SDK", formerly known as "Platform SDK", within their own directory structures.

This is the upside, no separate download/search/hassle.

The downside, neither of the new Window SDK's have include directories and the include directory in the VC directory does not have windows.h:~[

I'm searching around the web right now, if I find a reliable solution I'll post back...
I have both the SDK and Visual studio 2008 installed.
If I remember correctly you have to set certain environment variables and your path setting to show where various things are located.

There should be some dos batch files that you run to set these correctly.
Hi!

Okay, here's the skinny. It would appear that Microsoft is taking the "if we offer it for free, strip it!" attitude again. What comes with the current Express 2005/2008 is a minimal, stripped down version of the SDK. So, we're back to multiple downloads/searches/hassels again!:-<

First off, one has to rid one's self of the "habit" of referring to it as the "Platform" SDK. That, except in legacy, no longer exists!8-O

If you want to be able to build code now, you need to remember you are searching for the Windows SDK. (strange, I seem to recall it used to be that until Microsoft changed it to Platform SDK!?:~{ )

Go to:

http://www.microsoft.com/express/

then search for:

Windows SDK

This should result in a Live Search listing whose 2nd entry (or thereabouts) is:

Download details: Windows SDK for Windows Server 2008 and .NET ...

taking you to:

http://www.microsoft.com/downloads/details.aspx?FamilyID=E6E1C3DF-A74F-4207-8586-711EBE331CDC&displaylang=en
Windows SDK for Windows Server 2008 and .NET Framework 3.5

From here you download Setup.exe, which will install:

C:\Program Files\Microsoft SDKs\Windows

Which has v6.0A and v6.1 directories with:

bin
Include
Lib

and:

Bin
Help
Include
Lib
License
Redist
Samples
Setup

subdirectories, respectively.

I have this now installed on my Vista Ultimate 64-bit system and have successfully built 3rd party library software with Visual C++ 2005/2008 Express (both)!:-D

Note that my system is 64-bit. This appears to make no difference, at least, not in building this library. The Windows SDK only gets installed into the Program Files directory, not the Program Files (x86) directory, the default for 32-bit on a 64-bit system. This is probably because only the headers are being used, other items, like executables and libraries are being found in the C:\Program Files (x86)\Microsoft Visual Studio 8/9 directories.

HTH:

thx,
Dave S.

When you are up to your eyeballs in alligators it is sometimes hard to remember that your original objective was to DRAIN the SWAMP!

wxMS_developers - Development with wxWidgets on MSWindows
http://tech.groups.yahoo.com/group/wxMS_developers/

wxWidgets Code Exchange
http://www.wxCodex.net/
Try writing it without the .h extension
Topic archived. No new replies allowed.