CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2002
    Location
    Germany
    Posts
    487

    TCP-Socket.receive does not return. Why?

    Hi!
    Globally defined:
    CSocket sock;

    In a function:
    TRACE("\r\nTCPRead before sock.Receive");
    temp = sock.Receive(IBuf,64);
    TRACE("\r\nTCPRead after sock.Receive");

    in InitInstance:
    AfxSocketInit(&WsaData);

    Doing this the Receive-function never returns. Any hint?


    Thanks a lot!

    Marc

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: TCP-Socket.receive does not return. Why?

    If you're using blocking sockets the Receive call will wait until 1 or more bytes are read (socket is closed or an error occurs).

    - petter

  3. #3
    Join Date
    May 2006
    Posts
    327

    Re: TCP-Socket.receive does not return. Why?

    Maybe no one sends data to that socket?

  4. #4
    Join Date
    May 2002
    Location
    Germany
    Posts
    487

    Re: TCP-Socket.receive does not return. Why?

    OK: I even expect that no one sends a byte. But in this case there should be a timout?
    How can I make the function waiting only a certain time?

    But already you helped me!

    Thanks!

    Marc

  5. #5
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: TCP-Socket.receive does not return. Why?

    You should take a look at non-blocking or asynchronous sockets. CAsyncSocket is one solution.

    - petter

  6. #6
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: TCP-Socket.receive does not return. Why?

    Indeed you should use CASyncSocket or the WinSock API, because there are some problems with CSocket (though not in your example), because it tries to emulate blocking using a non-blocking socket. I don't know why most network programming beginners use CSocket. Maybe because of bad tutorials?
    Please don't forget to rate users who helped you!

  7. #7
    Join Date
    May 2006
    Posts
    327

    Re: TCP-Socket.receive does not return. Why?

    Quote Originally Posted by Marc from D
    How can I make the function waiting only a certain time?
    I think this can be done with select function:

    Code:
    long ms = ... // timeout in milliseconds
    SOCKET s = sock;
    fd_set set;
    FD_ZERO(&set);
    FD_SET(s, &set);
    timeval timeout;
    timeout.tv_sec = ms / 1000;
    timeout.tv_usec = (ms % 1000) * 1000;
    int const r = select(((int)s) + 1, &set, 000, 000, &timeout);
    
    switch(r)
    {
    case 0:
        // timeout
        break;
    case 1:
        // OK. Data available
        break;
    default:
        // Error
    }
    I hope it helps.

  8. #8
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: TCP-Socket.receive does not return. Why?

    Quote Originally Posted by Marc from D
    OK: I even expect that no one sends a byte. But in this case there should be a timout?
    How can I make the function waiting only a certain time?
    Changing to raw winsock API calls, or to CAsyncSocket, will add to the complexity of your program.

    If all you want is a CSocket with a timeout, then see the CTimeoutScoket class in this MSDN article: "How To Configure a Time-Out on a CSocket Operation" at http://support.microsoft.com/kb/q138692/

    The class is very short. It extends CSocket functionality by adding two public functions ( SetTimeOut and KillTimeOut ) and one protected function ( OnMessagePending ).

    Mike

  9. #9
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: TCP-Socket.receive does not return. Why?

    Quote Originally Posted by Marc from D
    OK: I even expect that no one sends a byte. But in this case there should be a timout?
    How can I make the function waiting only a certain time?

    But already you helped me!

    Thanks!

    Marc
    You can use non-blocking socket.
    There are 2 ways to realize non-blocking socket:
    1. Using setsockopt() function to set timeout of read/write operation.
    Note: According to MSDN, SO_RCVTIMEO and SO_SNDTIMEO are not
    supported in CAsyncSocket.SetSockOpt().
    2. Using timer to stop blocking.

    If you make a program using MFC, you can use CAsyncSocket and
    overwrite event handler OnReceive()/OnSend() to read/write data.

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