WSAStartup() error?!!! ( basic winsock application )

This is a discussion on WSAStartup() error?!!! ( basic winsock application ) within the C Programming forums, part of the General Programming Boards category; Hello, I was creating a basic winsock application when strange error message appeared ( while initializing winsock ) .. here's ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    WSAStartup() error?!!! ( basic winsock application )

    Hello,

    I was creating a basic winsock application when strange error message appeared ( while initializing winsock ) .. here's the code:
    Code:
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    #include <stdio.h>
    
    #pragma comment(lib, "Ws2_32.lib")
    
    int main(void)
    {
    	WSADATA wsadata;
    	int iresult = WSAStartup(MAKEWORD(2, 2), &wsadata);
    	if(iresult != 0) printf("WSASTARTUP faild: %d\n", iresult);
    	else printf("WSAStartup() Call succeeded\n");
    	
    	if(LOBYTE(wsadata.wVersion != 2) || HIBYTE(wsadata.wVersion != 2))
    	{
    		printf("Couldn't find the winsock.dll file!!!\n");
    		WSACleanup();
    	}
    	else printf("Found the required winsock.dll file\n");
    	WSACleanup();
    	return 0;
    }
    And here's the output:
    Code:
    WSAStartup() Call succeeded
    Couldn't find the winsock.dll file!!!
    so I added the line:
    Code:
    printf("low byte: %d, High byte: %d\n", LOBYTE(wsadata.wVersion), HIBYTE(wsadata.wVersion));
    in the second if clause
    and found the strange output:
    Code:
    WSAStartup() Call succeeded
    Couldn't find the winsock.dll file!!!
    low byte: 2, High byte: 2
    so what?
    Am I missing something?

    thanks in advance ..
    Note:
    OS Name: Microsoft Windows 7 Home Premium
    OS Version: 6.1.7600 N/A Build 7600

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Just a hunch, try enclosing both macros in parantheses.

    e.g (HIBYTE(...)) || (LOBYTE(...))

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Quote Originally Posted by DeadPlanet View Post
    Just a hunch, try enclosing both macros in parantheses.

    e.g (HIBYTE(...)) || (LOBYTE(...))
    What on Earth is that?!!!!!
    guess what?
    the code is working!!!!

    in my main post, I have only mentioned 1 of 2 problems .. check the whole code:
    Code:
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    #include <stdio.h>
    
    #define HTTP_PORT "80"
    #define SMTP_PORT "25"
    #define HOSTNAME_PORT "101"
    
    #pragma comment(lib, "Ws2_32.lib")
    
    int main(void)
    {
    	WSADATA wsadata;
    	int iresult = WSAStartup(MAKEWORD(2, 2), &wsadata);
    	DWORD dwretval;
    	if(iresult != 0) printf("WSASTARTUP faild: %d\n", iresult);
    	else printf("WSAStartup() Call succeeded :)\n");
    	
    	if((LOBYTE(wsadata.wVersion) != 2) || (HIBYTE(wsadata.wVersion) != 2))
    	{
    		printf("Couldn't find the winsock.dll file!!!\n");
    		printf("low byte: %d, High byte: %d\n", LOBYTE(wsadata.wVersion), HIBYTE(wsadata.wVersion));
    		WSACleanup();
    	}
    	else printf("Found the required winsock.dll file :)\n");
    
    	printf("System status\n%s\n\nEnd Of Initiation\n\nStarting the SOCKET variable\n\n", wsadata.szSystemStatus);
    
    	struct addrinfo *result = NULL, *ptr = NULL, hints;
    	ZeroMemory(&hints, sizeof(hints));
    	hints.ai_family = AF_UNSPEC;
    	hints.ai_socktype = SOCK_STREAM;
    	hints.ai_protocol = IPPROTO_TCP;
    
    	dwretval = getaddrinfo("www.google.com", HTTP_PORT, &hints, &result);
    	if(dwretval != 0) printf("ERROR when connecting to google.com\n%d", dwretval);
    	else printf("CONNECTED\n");
    	WSACleanup();
    	return 0;
    }
    see the "Error when connecting to google.com" string?
    that was the second problem I encountered!!
    This string was always printed

    but after ur suggestion ( wrapping LOBYTE and HIBYTE in brackets ), the string "Found the required winsock.dll file " is printed and the string "Connected" too

    now that we have our problem solved ( thanks to u ), I want to know what was the problem? where was the error?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    You don't need all of those brackets, you just needed to make sure you take the != 2 outside of the LOBYTE macro.
    You don't want the low byte of a boolean expresion etc
    This would be fine:
    Code:
        if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Quote Originally Posted by iMalc View Post
    You don't need all of those brackets, you just needed to make sure you take the != 2 outside of the LOBYTE macro.
    You don't want the low byte of a boolean expresion etc
    This would be fine:
    Code:
        if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2)
    ooops
    thanks
    and sorry for that FATAL mistake

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Oh wow yeah iMalc is right I can't believe I didn't spot that.

    I couldn't see an error (facepalm) so thought it may have been due to the macro expansions of HIBYTE and LOBYTE. I know it's very unlikely that that was the problem that's why I said it was just a hunch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help- basic WinSock application
    By tom_bali in forum Networking/Device Communication
    Replies: 9
    Last Post: 03-07-2009, 05:16 PM
  2. Replies: 4
    Last Post: 03-01-2008, 01:44 AM
  3. Basic Window application Help
    By The Brain in forum Windows Programming
    Replies: 2
    Last Post: 02-11-2005, 03:02 PM
  4. Unresolved External Error (WSAStartup, WSACleanup)
    By Epo in forum Networking/Device Communication
    Replies: 1
    Last Post: 01-16-2004, 11:58 PM
  5. Winsock client code help.... very basic.
    By Jay_Tech in forum Windows Programming
    Replies: 7
    Last Post: 10-18-2002, 06:27 AM

Tags for this Thread


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