Problem with my client and connect()

This is a discussion on Problem with my client and connect() within the Networking/Device Communication forums, part of the General Programming Boards category; The problem is this: with no server listening on the port the client will process connect and respond with a ...

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Problem with my client and connect()

    The problem is this: with no server listening on the port the client will process connect and respond with a success. Futhermore, I can go on to send() data and it will come back saying that it sent the requested number of bytes.

    When a server is listening it works fine.
    Note: POSTBUFF; is just a #define that prints szBuffer to an edit box.

    Code:
             if ( iError = WSAStartup (MAKEWORD (2,0), &WSAData) )
                   {
                   wsprintf(szBuffer, TEXT("WSAStartup() error #%i.\r\n"), WSAGetLastError() );
                   POSTBUFF;
                   return 0;
                   }
             
             wsprintf(szBuffer, TEXT("Started up %hs\r\n"), WSAData.szDescription);
             POSTBUFF;
             
             if ( (lpHostEntry = gethostbyname("Michael")) == NULL)
                {
                wsprintf(szBuffer, TEXT("gethost error %i"), WSAGetLastError() );
                POSTBUFF;
                }
                
             if ( (sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP) ) == INVALID_SOCKET)
                   {
                   wsprintf(szBuffer, TEXT("sock error #%i\r\n"), WSAGetLastError() );
                   POSTBUFF;
                   }
             
             if (SOCKET_ERROR == WSAAsyncSelect (sock, hwnd, WM_SOCKET_NOTIFY, FD_CONNECT | FD_READ | FD_ACCEPT | FD_WRITE | FD_CLOSE | FD_OOB) )
                   {
                   wsprintf(szBuffer, TEXT("WSAAsyncSelect sock error #%i.\r\n"), WSAGetLastError() );
                   POSTBUFF;
                   closesocket (sock);
                   }
              sa.sin_family = AF_INET;
              sa.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
              sa.sin_port = htons(9143);
             
             temp = connect (sock, (LPSOCKADDR)&sa, sizeof(struct sockaddr) );
                if ( temp == SOCKET_ERROR && (iError = WSAGetLastError()) != WSAEWOULDBLOCK)
                    {
                    wsprintf(szBuffer, TEXT("\r\nConnect() error.  %i"), iError );
                    POSTBUFF;
                    closesocket (sock);
                    return 0;
                    }
                else
                    PostText (hMessages, "\r\nConnect() successful");

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I believe your problem is that connect will almost always need to block, but since it's non-blocking it returns with an error and WSAEWOULDBLOCK. Since you say a WSAEWOULDBLOCK is not a problem, it tells you that you got success, when you haven't. You should either handle the FD_CONNECT message and check its error code for success/failure, or wait until after you've connected before switching it to non-blocking.

    **EDIT**
    Just a note, I think async sockets will always give you WSAEWOULDBLOCK if the host isn't there, unless you wait long enough for it to time out the connect attempt and it sets the error to reflect it, and THEN you check WSAGetLastError().
    Last edited by Hunter2; 09-02-2003 at 06:38 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yea I realized what was going on yesterday, purely by accident. While I'm building the programs I"m having the messages displayed along with the wparam and lparam values, and just happened to notice that fd_connect had a different hiword lparam, and I looked it up and realized what was going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  2. server client problem
    By rudeboy in forum Networking/Device Communication
    Replies: 5
    Last Post: 05-17-2008, 01:41 AM
  3. Client: Failed to connect
    By Galvatron in forum Networking/Device Communication
    Replies: 7
    Last Post: 05-12-2008, 04:40 PM
  4. Client works on a LAN but don't send all the data
    By Niara in forum Networking/Device Communication
    Replies: 9
    Last Post: 01-04-2007, 04:44 PM
  5. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21