Re: Winsock connect

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Mike (Mike_at_discussions.microsoft.com)
Date: 06/24/04


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...
> >
> >
> >



Relevant Pages

  • Re: Socket and cycle problem
    ... listening this port countinously. ... So I need make some loop to print data from 3883 port permanent. ... the data it receives from each connection after the remote side drops ... If you were to use the socket module, then it would look something like this: ...
    (comp.lang.python)
  • Re: Conditional Accept and Overlapped I/O Question
    ... it is imperative to hide the listening port on the server ... connection requests will come in from the same thread on the client side. ...
    (microsoft.public.win32.programmer.networks)
  • Re: port forwarding problem
    ... > I rebooted into safe mode, shieldsup gave me the port as closed. ... Please put the router back and since this is a new XP install go to ... Make sure that your Internet connection is working and run vnc. ... Use TCPView to make sure that vnc is listening. ...
    (comp.security.firewalls)
  • RE: unblock port 25
    ... Im not sure what smtp server you are using, if it's sendmail, ... netstat -i shows me that port 25 is listening only for local ... Connection closed by foreign host. ...
    (RedHat)
  • Re: Socket and cycle problem
    ... My script send me via 3883 port data, ... listening this port countinously. ... the data it receives from each connection after the remote side drops ... If you were to use the socket module, then it would look something like this: ...
    (comp.lang.python)