Thread: Function problem (Winsock)

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    29

    Function problem (Winsock)

    I having a problem to start winsock2, check it out (It's dosn't connect):

    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <stdio.h>
    
    
    int connect()
    {
    
    	int sockfd;
    	struct sockaddr_in dest_addr;
    
    		WSADATA info;
    	    if (WSAStartup(MAKEWORD(1,1), &info) != 0)
      		  MessageBox(NULL, "Cannot initialize WinSock!", "WSAStartup", MB_OK);
    		else
    			{
    			MessageBox(NULL, "Cannot initialize WinSock!", "ERROR 216", MB_OK);
    			return(0);
    		}
    			
    	    sockfd = socket(AF_INET,SOCK_STREAM,0);
    		dest_addr.sin_family = AF_INET;
    		dest_addr.sin_port = htons(6667);
        		dest_addr.sin_addr.s_addr = inet_addr("bombers.no-ip.org");
    		if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) >= 0)
    			printf("\n%d", "Connected succesfully"); 
    		else
    		{
    			MessageBox(NULL, "Cannot initialize WinSock!", "ERROR 219", MB_OK);
    			return(0);
    		}
    }
    
    
    int main()
    {
    connect();
    }
    Last edited by Pandora; 04-02-2003 at 11:32 AM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Read this and edit your post. While you're editting, specify the problem you are having with the posted code.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    29

    Steel having a prob...

    Here is the full source again, It's dosn't connect :\ Anyone?


    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <stdio.h>
    
    
    int IRCconnect()
    {
    	int sockfd;
    	struct sockaddr_in dest_addr;
    
    		WSADATA info;
    	    if (WSAStartup(MAKEWORD(1,1), &info) != 0)
      		  MessageBox(NULL, "Cannot initialize WinSock!", "WSAStartup", MB_OK);
    		else
    			{
    			//MessageBox(NULL, "Cannot initialize WinSock!", "ERROR 216", MB_OK);
    			return(0);
    		}
    			
    	    sockfd = socket(AF_INET,SOCK_STREAM,0);
    		dest_addr.sin_family = AF_INET;
    		dest_addr.sin_port = htons(6667);
        		dest_addr.sin_addr.s_addr = inet_addr("80.105.107.170");
    		if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) >= 0)
    			printf("\n%d", "Connected succesfully"); 
    		else
    		{
    			printf("%d",sockfd);
    			return(0);
    		}
    }
    
    
    
    int main()
    {
    	IRCconnect();
    }
    Last edited by Pandora; 04-02-2003 at 11:32 AM.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    29

    SO...

    If you say that is working you, can you please write here your code?

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    29

    I think it's working...

    Well, i think it's working:


    --------------------------------------------------------------------
    Microsoft Windows 2000 [Version 5.00.2195]
    (C) Copyright 1985-2000 Microsoft Corp.

    C:\>sock

    4325424
    C:\>

    --------------------------------------------------------------------


    The source code, can you tell me if the
    if (WSAStartup(MAKEWORD(1,1), &info) != 0)
    and the else are correct?


    Source code:
    Code:
    #include <stdio.h>
    #include <winsock2.h>
    #include <windows.h>
    
    int IRCconnect()
    {
    	int sockfd;
    	struct sockaddr_in dest_addr;
    
    		WSADATA info;
    	    if (WSAStartup(MAKEWORD(1,1), &info) != 0) {
    	MessageBox(NULL, "Cannot initialize WinSock!", "WSAStartup", MB_OK);
    	return(1);
    	}
    		else
    			{
    			MessageBox(NULL, "Winsock started!", "OK", MB_OK);
    		}
    			
    	    sockfd = socket(AF_INET,SOCK_STREAM,0);
    		dest_addr.sin_family = AF_INET;
    		dest_addr.sin_port = htons(6667);
        		dest_addr.sin_addr.s_addr = inet_addr("80.105.107.170");
    		if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) >= 0)
    			printf("\n%d", "Connected succesfully"); 
    		else
    		{
    			//MessageBox(NULL, "Cannot initialize WinSock!", "ERROR 219", MB_OK);
    			printf("\n%d", "Connected...");
    		}
    }
    
    
    
    int main()
    {
    	IRCconnect();
    }
    Last edited by Pandora; 04-02-2003 at 11:31 AM.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Pandora, USE CODE TAGS when posting code.

    gg

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    29

    Haa, ok

    NP

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    29

    So, please, anyone can tell me if it's Ok?

    Just say if it's ok... :\

    Code:
    #include <stdio.h>
    #include <winsock2.h>
    #include <windows.h>
    
    int IRCconnect()
    {
    int sockfd;
    struct sockaddr_in dest_addr;
    
    WSADATA info;
    if (WSAStartup(MAKEWORD(1,1), &info) != 0) {
    MessageBox(NULL, "Cannot initialize WinSock!", "WSAStartup", MB_OK);
    return(1);
    }
    else
    {
    MessageBox(NULL, "Winsock started!", "OK", MB_OK);
    }
    
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(6667);
    dest_addr.sin_addr.s_addr = inet_addr("80.105.107.170");
    if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) >= 0)
    printf("\n%d", "Connected succesfully"); 
    else
    {
    //MessageBox(NULL, "Cannot initialize WinSock!", "ERROR 219", MB_OK);
    printf("\n%d", "Connected...");
    }
    }
    
    
    
    int main()
    {
    IRCconnect();
    }
    Last edited by Pandora; 04-02-2003 at 11:49 AM.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Shouldn't you tell your printf()'s to output strings instead of integers?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    29

    No, he don't say...

    No, he don't say anything about printf & string insted integer...

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>printf("\n%d", "Connected succesfully");
    This should be
    >>printf("\n%s", "Connected succesfully");

    or better still

    >>printf("\nConnected succesfully");

    or maybe even

    >>puts("Connected succesfully");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    29

    maybe, but

    But you missed my point, when i run it, it's tell me that the winsock culd not start...

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> it's tell me that the winsock culd not start...

    Then it is also telling you why. From MSDN...

    >>>
    Code:
    Return Values
    The WSAStartup function returns zero if successful. Otherwise, it returns one of the error codes listed below.
    
    *** clipped ***
    
    Error Codes
    WSASYSNOTREADY Indicates that the underlying network subsystem is not ready for network communication. 
    WSAVERNOTSUPPORTED The version of Windows Sockets support requested is not provided by this particular Windows Sockets implementation. 
    WSAEINPROGRESS A blocking Windows Sockets 1.1 operation is in progress. 
    WSAEPROCLIM Limit on the number of tasks supported by the Windows Sockets implementation has been reached. 
    WSAEFAULT The lpWSAData is not a valid pointer.
    <<<

    ... see which it is and fix it!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: maybe, but

    Originally posted by Pandora
    But you missed my point, when i run it, it's tell me that the winsock culd not start...
    I didn't miss it, you did state that before, you said:
    It's dosn't connect
    Accurate questions get accurate answers (in most cases )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM