Thread: Query related to recv()?

  1. #1
    Registered User
    Join Date
    May 2005

    Question Query related to recv()?

    Hi experts
    I have recently joined this forum.

    This is my first query here:

    I have developing a client server application where the server is an MFC dialog based application & the client is an MFC SDI application.

    Winsock2 is being used for communication.

    Both the client & the server is set to asynchronous mode by using the WSAAsyncselect().

    I have 2 questions to ask:


    Is a single recv() in response to a single FD_READ message appropriate OR the
    recv() must be called in a loop untill all data is received.


    Lets say the server needs to send 1000 bytes of data to the client but at a time
    only 100 bytes.
    Now there are two ways:
    1----> The server calls send() in a loop & donot cares whether data reaches the client or not.
    2----> The server calls send() in a loop & after every send() it waits for the client's
    notification that the data is received, now send next lot of 100 bytes.

    Waiting for suggestions

    Best Regards

  2. #2
    Registered User
    Join Date
    Jan 2005
    FD_READ indicates that there is data available to be received but not how much. If you want to receive a certain amount of data then a single recv can't guarantee that. Looping on recv is a possibility but since your using nonblocking (asyncronous) sockets you could just get as much data as is available, store it somewhere and get remaining chunks of the amount you expect on subsequent FD_READ messages.
    If you want your server to send a reply after each 100 bytes and the client to wait for that reply before sending the next 100 bytes then you would send the first 100 with send() then you would receive the servers reply with recv() and send the next 100 bytes and so on.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    I'm sure the answer to both your questions are in
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    A less readable version of Q1024's answer is buried in the documentation page for WSAAsyncSelect:
    Quote Originally Posted by MSDN
    For FD_READ, FD_OOB, and FD_ACCEPT events, message posting is level-triggered. This means that if the reenabling routine is called and the relevant condition is still met after the call, a WSAAsyncSelect message is posted to the application. This allows an application to be event-driven and not be concerned with the amount of data that arrives at any one time. Consider the following sequence:
    1. Network transport stack receives 100 bytes of data on socket s and causes Windows Sockets 2 to post an FD_READ message.
    2. The application issues recv( s, buffptr, 50, 0) to read 50 bytes.
    3. Another FD_READ message is posted since there is still data to be read.

    With these semantics, an application need not read all available data in response to an FD_READ message—a single recv in response to each FD_READ message is appropriate. If an application issues multiple recv calls in response to a single FD_READ, it can receive multiple FD_READ messages. Such an application can need to disable FD_READ messages before starting the recv calls by calling WSAAsyncSelect with the FD_READ event not set.

  5. #5
    Registered User
    Join Date
    May 2005
    Thank u all
    I really appreciate the suggestions provided.

    Best Regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about recv
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-26-2009, 02:10 PM
  2. Problem in when I run insert query
    By Bargi in forum Windows Programming
    Replies: 1
    Last Post: 05-09-2007, 10:46 PM
  3. Replies: 1
    Last Post: 09-18-2005, 09:06 PM
  4. Query related to DisconnectEx?
    By dp_76 in forum Networking/Device Communication
    Replies: 5
    Last Post: 05-25-2005, 02:33 AM
  5. recv()
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-24-2004, 05:32 PM