Simple Winsock Code using DevCPP

This is a discussion on Simple Winsock Code using DevCPP within the Networking/Device Communication forums, part of the General Programming Boards category; I just need a little help before getting deep into network programming.. I am attempting to make what is nothing ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question Simple Winsock Code using DevCPP

    I just need a little help before getting deep into network programming.. I am attempting to make what is nothing more than a "hello world" of winsock api by playing with a couple of functions.

    The first problem is that I cannot get my code to compile using either the <winsock.h> or <winsock2.h> header. I have attached a pic of all compiler errors. I have browsed the, "include" folder located in DevCPP and have located both the winsock.h and winsock2.h header files. Compiler options are at default settings and provide paths to the devcpp 'include' folder.

    There may be additional errors.. I am kinda new at winsock api and may have made a few syntactical mistakes.

    Please help if ye' can.

    Code:
    #include<iostream>
    #include<winsock2.h>
    
    using namespace std;
    
    int main()
    {
        WSADATA wsd;
        WSAPROTOCOL_INFO *proto;
        LPDWORD bufferlength = NULL;
        
        WSAStartup(MAKEWORD(2,2), &wsd);
        
        cout << "\n\nUsing winsock version " << wsd.wVersion;
        
        //This call will fail, but will return the correct buffer size
        WSAEnumProtocols(NULL, NULL, bufferlength);
        
        //Allocate the correct buffer size of WSAPROTOCOL_INFO structures
        proto = new WSAPROTOCOL_INFO[*bufferlength];
        
        //Populate an array of system protocols
        WSAEnumProtocols(NULL, proto, bufferlength);
        
        //Step through the array and view some system supported protocol information
        for(int i=0; i<*bufferlength; i++)
        {
           cout << "\n\n\nVersion:    " << proto[i].iVersion;
           cout << "\nAddress Family: " << proto[i].iAddressFamily;
           cout << "\nMax Sockets:    " << proto[i].iMaxSockAddr;
           cout << "\nMin Sockets:    " << proto[i].iMinSockAddr;
           cout << "\nProtocol:       " << proto[i].szProtocol;
        }   
        
        WSACleanup();
        
        return 0;
    }
    Attached Images Attached Images  
    Last edited by The Brain; 11-12-2006 at 08:10 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Those are linker errors. You must link to the winsock library.
    Winsock Problem

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question

    I have located and added the libwsock32.a path in my library settings.. but I still get the same linker errors at compile time. What am I missing/overlooking?
    Attached Images Attached Images  
    Last edited by The Brain; 11-11-2006 at 08:26 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It could be libws2_32.a

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I finally got winsock code to compile by compiling it as a windows project.. as opposed to c++ source.

    I successfully added the llibws2_32.a by going to Project-> Project Options -> parameters, but after compilation execution is terminated.. why???

    Code:
    #include<iostream>
    #include<winsock2.h>
    
    using namespace std;
    
    int main()
    {
        WSADATA wsd;
        WSAPROTOCOL_INFO *proto;
        LPDWORD bufferlength = 0;
        
        if(WSAStartup(MAKEWORD(2,2), &wsd) != 0)
        
           cout << "\n\aWSAStartup failed.";
           
        else
        
           cout << "\n\nUsing winsock version " << wsd.wVersion;
        /*
        //This call will fail, but will return the correct buffer size
        WSAEnumProtocols(NULL, NULL, bufferlength);
        
        //Allocate the correct buffer size of WSAPROTOCOL_INFO structures
        proto = new WSAPROTOCOL_INFO[*bufferlength];
        
        //Populate an array of system protocols
        WSAEnumProtocols(NULL, proto, bufferlength);
        
        //Step through the array and view some system supported protocol information
        for(int i=0; i<*bufferlength; i++)
        {
           cout << "\n\n\nVersion:    " << proto[i].iVersion;
           cout << "\nAddress Family: " << proto[i].iAddressFamily;
           cout << "\nMax Sockets:    " << proto[i].iMaxSockAddr;
           cout << "\nMin Sockets:    " << proto[i].iMinSockAddr;
           cout << "\nProtocol:       " << proto[i].szProtocol;
        }   
        */
        cout << "Press [ENTER] to continue...";   
        cin.get();
        
        WSACleanup();
        
        return 0;
    }

    compiler log:
    Code:
    Compiler: Default compiler
    Building Makefile: "F:\Dev-Cpp\Makefile.win"
    Executing  make...
    make.exe -f "F:\Dev-Cpp\Makefile.win" all
    make.exe: Nothing to be done for `all'.
    
    Execution terminated
    Compilation successful
    Attached Images Attached Images  
    Last edited by The Brain; 11-12-2006 at 08:09 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > LPDWORD bufferlength = 0;
    When things say they want a pointer to something, it usually means you need a 'something' then you have &something when you call it.

    Simply declaring a pointer-to-something just to make the compiler happy isn't the right thing to do, you have to look at what the parameter is being used for and declare accordingly.

    Say
    Code:
        DWORD bufferlength = 0;
    
        //This call will fail, but will return the correct buffer size
        WSAEnumProtocols(NULL, NULL, &bufferlength);
        
        //Allocate the currect buffer size of WSAPROTOCOL_INFO structures
        proto = new WSAPROTOCOL_INFO[bufferlength];
        
        //Populate an array of system protocols
        WSAEnumProtocols(NULL, proto, bufferlength);
        
        //Step through the array and view some system supported protocol information
        for(int i=0; i<bufferlength; i++)
        {
           cout << "\n\n\nVersion:    " << proto[i].iVersion;
           cout << "\nAddress Family: " << proto[i].iAddressFamily;
           cout << "\nMax Sockets:    " << proto[i].iMaxSockAddr;
           cout << "\nMin Sockets:    " << proto[i].iMinSockAddr;
           cout << "\nProtocol:       " << proto[i].szProtocol;
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    lib/libws2_32.a
    Change it to -lws2_32, otherwise the path won't work if you copy/cut your files to somewhere else.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question

    I tried all this stuff.. nothing is working. Can anyone else get this code to compile?
    Attached Images Attached Images  
    Last edited by The Brain; 11-12-2006 at 02:13 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    The 'lib' and '.a' are assumed.
    So it's just ws2_32

    Like so (see image)
    Attached Images Attached Images  
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    That did the trick..
    thanks
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Can anyone give me any clues as to why this code gives winsock version 514 and a buffer size of 9672 ??

    Code:
    #include<iostream>
    #include<winsock2.h>
    
    using namespace std;
    
    int main()
    {
        WSADATA wsd;
        WSAPROTOCOL_INFO *proto;
        DWORD bufferlength = 0;
        
        if(WSAStartup(MAKEWORD(2,2), &wsd) != 0)
        
           cout << "\n\aWSAStartup failed...";
           
        else    
        
           cout << "\n\nUsing winsock version " << wsd.wVersion;
        
        //This call will fail, but will return the correct buffer size
        WSAEnumProtocols(NULL, NULL, &bufferlength;
        
        cout << "\n\nThere are " << buffersize << " protocols.";
        
        //Allocate the currect buffer size of WSAPROTOCOL_INFO structures
        proto = new WSAPROTOCOL_INFO[bufferlength];
        
        //Populate an array of system protocols
        WSAEnumProtocols(NULL, proto, &bufferlength);
        
        //Step through the array and view some system supported protocol information
        //I had to use a loop size of 26 because 'buffersize' was huge
        for(int i=0; i<26; i++)
        {
           cout << "\n\n\nProtocol #"   << i+1;
           cout << "\nVersion:        " << proto[i].iVersion;
           cout << "\nAddress Family: " << proto[i].iAddressFamily;
           cout << "\nMax Sockets:    " << proto[i].iMaxSockAddr;
           cout << "\nMin Sockets:    " << proto[i].iMinSockAddr;
           cout << "\nProtocol:       " << proto[i].szProtocol;
        }   
        
        cout << "\nPress [ENTER] to continue...";
        cin.get();
        
        WSACleanup();
        
        return 0;
    }
    Last edited by The Brain; 11-13-2006 at 03:42 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    First, the code you posted does not compile.

    Version "514" in hex is 0x0202 - or version 2.2. Use HIBYTE/LOBYTE to get major/minor numbers.

    The "buffer size" is huge because (I think) it specifies the buffer size in bytes, and not items. Really, you should be looping not the buffersize, but buffersize / sizeof(WSAPROTOCOL_INFO) - you're mixing bytes with array indecies.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  13. #13
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I just want to thank everyone for their help thus far. Finally, the program works.

    I attempted to use HIWORD/LOWORD before.. good call on using the HIBYTE/LOBYTE macros. By themselves, they only yielded little smiley face icons when displayed on dos console, but I casted each value to 'int' and it worked fine. Also, good call on the buffer/sizeof( ).

    I think this little exercise illustrates the importance of "hello world" when getting into something new.

    Here is the final code.. works as advertised thanks to salem, maxtor, Tonto, and the hugger o' cacti:

    Code:
    #include<iostream>
    #include<winsock2.h>
    
    using namespace std;
    
    int main()
    {
        WSADATA wsd;
        WSAPROTOCOL_INFO *proto;
        DWORD bufferlength = 0;
        
        if(WSAStartup(MAKEWORD(2,2), &wsd) != 0)
        
           cout << "\n\aWSAStartup failed...";
           
        else    
        
           cout << "\n\nUsing winsock version " 
                << (int)HIBYTE(wsd.wVersion) << '.' << (int)LOBYTE(wsd.wVersion);
        
        //This call will fail, but will return the correct buffer size
        WSAEnumProtocols(NULL, NULL, &bufferlength);
        
        cout << "\n\nThere are " << bufferlength/sizeof(WSAPROTOCOL_INFO) 
             << " protocols.";
        
        //Allocate the currect buffer size of WSAPROTOCOL_INFO structures
        proto = new WSAPROTOCOL_INFO[bufferlength];
        
        //Populate an array of system protocols
        WSAEnumProtocols(NULL, proto, &bufferlength);
        
        //Step through the array and view some system supported protocol information
        for(int i=0; i<bufferlength/sizeof(WSAPROTOCOL_INFO); i++)
        {
           cout << "\n\n\nProtocol #"   << i+1;
           cout << "\nVersion:        " << proto[i].iVersion;
           cout << "\nAddress Family: " << proto[i].iAddressFamily;
           cout << "\nMax Sockets:    " << proto[i].iMaxSockAddr;
           cout << "\nMin Sockets:    " << proto[i].iMinSockAddr;
           cout << "\nProtocol:       " << proto[i].szProtocol;
        }   
        
        cout << "\n\nPress [ENTER] to continue...";
        cin.get();
        
        WSACleanup();
        
        return 0;
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Thumbs up Thanx!!! It helped me a lot.

    I read whole thread, It was helpful, Thanx again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple piece of code
    By Furious5k in forum C++ Programming
    Replies: 10
    Last Post: 12-12-2008, 04:25 PM
  2. Weird error in this simple code! C2248!
    By gross100 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2005, 12:31 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  5. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21