Re: Winsock connect
From: Mike (Mike_at_discussions.microsoft.com)
Date: 06/24/04
- Next message: Paul G. Tobey [eMVP]: "Re: Winsock connect"
- Previous message: Panther71L: "Re: Layered Service Provider"
- In reply to: Mike: "Re: Winsock connect"
- Next in thread: Paul G. Tobey [eMVP]: "Re: Winsock connect"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 24 Jun 2004 11:11:01 -0700
Any ideas yet? Here's another example I tried that still doesn't fail if nothing is listening on the port I'm connecting to.
BOOL bReturn = FALSE;
SOCKET s = INVALID_SOCKET;
ADDRINFO *AddrInfo = NULL;
ADDRINFO Hints;
char charHostAddress[15];
char charPortNumber[5];
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2), &wsaData))
{
return FALSE;
}
wcstombs(charHostAddress, cstrHostAddress, cstrHostAddress.GetLength());
sprintf(charPortNumber, "%d", uintPortNumber);
memset(&Hints, 0, sizeof(Hints));
Hints.ai_family = PF_INET;
Hints.ai_socktype = SOCK_STREAM;
if(getaddrinfo(charHostAddress, charPortNumber, &Hints, &AddrInfo)!=0)
{
bReturn = FALSE;
}
else
{
s = socket(AddrInfo->ai_family, AddrInfo->ai_socktype, AddrInfo->ai_protocol);
if(s==INVALID_SOCKET)
{
bReturn = FALSE;
}
else
{
if(connect(s, AddrInfo->ai_addr, AddrInfo->ai_addrlen) == SOCKET_ERROR)
{
bReturn = FALSE;
}
else
{
bReturn = TRUE;
shutdown(s, SD_BOTH);
closesocket(s);
}
}
if(AddrInfo!=NULL)
freeaddrinfo(AddrInfo);
}
return bReturn;
"Mike" wrote:
> Thanks Paul:
>
> Here a code snippet a threw together - I can't get the connect call to fail for anything?
>
> SOCKET s;
> sockaddr_in sckaddr;
>
> s = socket(PF_INET, SOCK_STREAM, 0);
> if(s==INVALID_SOCKET)
> {
> hRes = E_FAIL;
> }
> else
> {
> memset(&sckaddr, 0, sizeof(sockaddr_in));
>
> sckaddr.sin_port = htons(55);
> sckaddr.sin_family = PF_INET;
> sckaddr.sin_addr.s_addr = inet_addr("192.168.2.7");
>
> if(connect(s, (struct sockaddr*)&sckaddr, sizeof(sckaddr)) == SOCKET_ERROR )
> {
> hRes = E_FAIL;
> }
> else
> {
> hRes = S_OK;
> closesocket(s);
> }
>
> }
>
>
> "Paul G. Tobey [eMVP]" wrote:
>
> > If you are using asynchronous sockets, it will do this. Otherwise, it does
> > not return success until the connection *has* been established. If the
> > connection is denied, as you describe for Hyperterminal, you will get
> > SOCKET_ERROR back from connect() and WSAGetLastError() will return
> > WSAECONNREFUSED (connection refused).
> >
> > Don't use the MFC CSocket stuff. Use WinSock directly, definitely. Did you
> > create the socket correctly? Show us the code and I bet the problem will be
> > obvious (this is 100% reliable, in my experience, so you must be doing
> > something wrong).
> >
> > Paul T.
> >
> > "Mike" <Mike@discussions.microsoft.com> wrote in message
> > news:0F6EEDC9-576D-4375-9DE4-6FD6FF67837D@microsoft.com...
> > > Hello all,
> > >
> > > Why does the Winsock connect function return success even though the
> > connection has not been accepted on the other side? What I've found is that
> > if even if nothing is "listening" at the destination on the specified port
> > this function call returns success. Is there any way to determine if the
> > connect was actually accepted?
> > >
> > > For instance, if I go through hyperterminal on my WinXP computer and I
> > attempt to connect to an IP:port, the connection is denied unless I have
> > something listening on that port. Is there a way I can get this same
> > functionality on Windows CE Winsock?
> > >
> > > I was initially having problems with CCeSocket.Connect because if the
> > connect wasn't actually accepted, the CE Device was essentially hung and had
> > to be reset (unless I let it recover for several minutes). I did some
> > investigation and connect always returns success. In fact the OnConnect
> > callback is also ALWAYS supplied 0 indicating success, unless the host
> > address can't be reached for some reason. I looked around in the message
> > posts and the general advice was to use Winsock directly. I started using
> > Winsock connect and this function also returns success even if the
> > connection isn't accepted.
> > >
> > > Thanks for any advice in advance,
> > > mike...
> >
> >
> >
- Next message: Paul G. Tobey [eMVP]: "Re: Winsock connect"
- Previous message: Panther71L: "Re: Layered Service Provider"
- In reply to: Mike: "Re: Winsock connect"
- Next in thread: Paul G. Tobey [eMVP]: "Re: Winsock connect"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|