CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2003
    Posts
    82

    accepting a socket connection request

    in my main dialog.cpp//dialog-based app

    if (serversock.create(8000,SOCK_STREAM,myIP) && serversock.listen())
    display OK;

    if (serversock.Accept(receivesock,0,0))
    display OK;


    Is this the method to wait for a connection request?

    or should I do it in the OnAccept() of the socket class?
    What is the usual method of waiting for connection request and where to accept the request?

  2. #2
    Join Date
    Mar 2003
    Location
    Poland
    Posts
    73
    I thing you should use OnAccept() function. If you use Accept() and there will not be any waiting connections this will block your app.

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158
    Actually, this is depending on your socket. If it is a blocking socket, calling Accept() will then wait until there is an incoming connection. If it is nonblocking, Accept will return immediately and any incoming connections will be signalled in OnAccept.

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556
    Originally posted by Richard.J
    Actually, this is depending on your socket. If it is a blocking socket, calling Accept() will then wait until there is an incoming connection. If it is nonblocking, Accept will return immediately and any incoming connections will be signalled in OnAccept.
    I think I agree on blocking, but don't entirely agree on non-blocking.

    For non-blocking, set the socket up to signal your program that there's a request that needs accepting, with a call to WSAAsyncSelect(...FD_ACCEPT...). Then, in your OnAccept() handler, call accept() and test the result against WSAEWOULDBLOCK. If there was not a block, then everthing is fine and the program can continue with communication on the newly-accepted socket. But if there's a block, then the program needs to sit back and wait for OnAccept to be called again.

    -Mike

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