Click to See Complete Forum and Search --> : Problem with connect()


Shtemp
January 22nd, 2010, 04:04 PM
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.


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?

Codeplug
January 22nd, 2010, 04:52 PM
Try calling WSAGetLastError() before calling any other Winsock functions. Calling closesocket() will mostly likely reset any last errors.

gg

Shtemp
January 22nd, 2010, 05:10 PM
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.

Shtemp
January 23rd, 2010, 07:16 AM
Problem was resolved by creating new project in VS and moving all files there.
Shalom to developers of Visual Studio 2008;