CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Location
    Ukraine, Simferopol
    Posts
    3

    Problem with connect()

    Hi. I try to connect to remote host on port which closed, but connect() == 0 && WSAGetLastError() == 0, and second call of send() returns SOCKET_ERROR. If port is open all works perfectly. Therefore I can't determine was connection successful or not.
    I using Visual Studio 2008, project with MFC.

    Code:
    int Rpc::Connect()
    {
    	ADDRINFOW 		hint;
    	PADDRINFOW		pAi;
    	sockaddr_in		sin;
    	int				res=0;
    
    	CSingleLock			lock(&mutex);
    
    	lock.Lock();
    
    	if (hSock != INVALID_SOCKET)
    	{
    		SetLoggedIn(false);
    		closesocket(hSock);
    		hSock=INVALID_SOCKET;
    		connected=false;
    	}
    
    	memset(&hint,0,sizeof(hint));
    	memset(&sin,0,sizeof(sin));
    	
    	hint.ai_family=AF_INET;
    	hint.ai_socktype=SOCK_STREAM;
    	hint.ai_protocol=IPPROTO_TCP;
    
    	if (res=GetAddrInfoW(host_name,NULL,&hint,&pAi))
    	{
    		return res;
    	}
    
    
    	sin.sin_addr.S_un.S_addr=((sockaddr_in*)(pAi->ai_addr))->sin_addr.S_un.S_addr;
    	sin.sin_family=AF_INET;
    	sin.sin_port=htons(port);
    	//sin.sin_addr=Ip("127.0.0.1").Addr();
    	//sin.sin_port=htons(19);
    
    	if ((hSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == INVALID_SOCKET)
    	{
    		return WSAGetLastError();
    	}
    
    	if (connect(hSock,(sockaddr*)&sin,sizeof(sin)))
    	{
    		closesocket(hSock);
    		hSock=INVALID_SOCKET;
    		return WSAGetLastError();
    	}
    
    	connected=true;
    	SetAsync();
    
    	return 0;
    }
    Can anybody help me?

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Problem with connect()

    Try calling WSAGetLastError() before calling any other Winsock functions. Calling closesocket() will mostly likely reset any last errors.

    gg

  3. #3
    Join Date
    Jan 2010
    Location
    Ukraine, Simferopol
    Posts
    3

    Re: Problem with connect()

    I tried to do it, but result the same: connect() to closed port always success , WSAGetLastError() == 0. When I create new project in VS and paste this code there, all works well: connect() != 0.

  4. #4
    Join Date
    Jan 2010
    Location
    Ukraine, Simferopol
    Posts
    3

    Re: Problem with connect()

    Problem was resolved by creating new project in VS and moving all files there.
    Shalom to developers of Visual Studio 2008;

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