CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2006
    Posts
    1

    Winsock2 connect error

    I am using winsock2 to create a tcp stream. My listen function works correctly but every time i try to connect it fails. Can someone tell me why the code below always fails?

    sockaddr_in target;

    target.sin_family = AF_INET; // address family Internet
    target.sin_port = htons (5001); // set server’s port number
    target.sin_addr.s_addr = inet_addr ("127.0.0.1"); // set server’s IP

  2. #2
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: Winsock2 connect error

    There is nothing wrong with this bit of code.
    Please post the complete winsock related code, so we can take a look at it!
    What error do you get when you connect?
    Please don't forget to rate users who helped you!

  3. #3
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Winsock2 connect error

    Code:
    SOCKET m_Sock;
    SOCKADDR_IN sock_addr;		//socket Address Variable
    WSACleanup();		  // Clears The Socket
    m_Sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    // Create Socket, Conenect using Sock Stream
    sock_addr.sin_port = htons(p_iPortNo);	// Set Port No for Socket
    sock_addr.sin_addr.s_addr = inet_addr(p_strIPAdd);// Set IP Address for Socket
    sock_addr.sin_family = AF_INET;
    memset(sock_addr.sin_zero, 0, sizeof(sock_addr.sin_zero));
    int result = connect(m_Sock, (sockaddr*)&sock_addr, sizeof(sock_addr));// Connect socket to server
    //check for result value if it is SOCKET_ERROR means Connection failed else you can proced
    Thanx
    Last edited by humptydumpty; June 6th, 2006 at 01:17 AM.

  4. #4
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: Winsock2 connect error

    You can't call any socket functions after you called WSACleanup().
    It should be the last thing you call in your program, because it means "I don't need winsock any longer".
    Please don't forget to rate users who helped you!

  5. #5
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Winsock2 connect error

    Quote Originally Posted by philkr
    You can't call any socket functions after you called WSACleanup().
    It should be the last thing you call in your program, because it means "I don't need winsock any longer".
    Just Check it out Once Again First Cleanup Then i create a Socket.There is nothing Wrong with the Code.if you want you can check this one with your self

    Thanx

  6. #6
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Winsock2 connect error

    WSACleanup must not be called before any other winsock2 call, MSDN says
    The WSACleanup function terminates use of the WS2_32.DLL.
    You need closesocket call in case if you want to close any open socket.
    Regards,
    Ramkrishna Pawar

  7. #7
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Winsock2 connect error

    Actuallu it Should Like in Following Manner
    Code:
    int retVal = 0;	// Varable that stores return value type
    SOCKADDR_IN sock_addr;//socket Address Variable
    SOCKET m_Sock;
    WSACleanup();// Clears The Socket
    WORD wVersionRequested = 0x202;
    WSADATA m_data;
    if(0 == ::WSAStartup(wVersionRequested, &m_data))
    { 
      m_Sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);	// Create Socket, Conenect using Sock Stream
    	// Run if Socket Creation Failed
    	if(m_Sock == INVALID_SOCKET)
    	{
    		retVal = -1;
    		return retVal;
    	}
    	// Run if Socket Creation Successfull
    	else
    	{
    	 sock_addr.sin_port = htons(p_iPortNo);					// Set Port No for Socket
    	 sock_addr.sin_addr.s_addr = inet_addr(p_strIPAdd);		// Set IP Address for Socket
    	sock_addr.sin_family = AF_INET;
    	memset(sock_addr.sin_zero, 0, sizeof(sock_addr.sin_zero));
    	int result = connect(m_Sock, (sockaddr*)&sock_addr, sizeof(sock_addr));	// Connect socket to server
    	// Run If Unable to connect to server
    	if(result == SOCKET_ERROR)
    	{
    		int i = WSAGetLastError();
    		retVal = -1;
    		return retVal;
    	}
    } 
    }

  8. #8
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: Winsock2 connect error

    Originally WSACleanup probably had some use; currently this is just for backward compatibility but it is always safer to call it as some implementations use this to terminate use of WS2_32.DLL
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  9. #9
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Winsock2 connect error

    Quote Originally Posted by minlial
    ....My listen function works correctly but every time i try to connect it fails. ...
    The code you posted does not show that listen() was called, naturally, since listen() would typically be called in a server and connect() would typically be called in a client.

    If you are still having problems, post your real code, and explain clearly the problems you are seeing. To say that "listen works correctly" but that "it" fails does not give us any hint as to what is really failing.

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured