Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

recv function - winsock

Status
Not open for further replies.

fmaurer

Technical User
May 15, 2007
11
PL
Hi,
is there any option to check how big message size is in order to invoke recv() with the proper buflen parameter?
I tried the option to put recv in while loop with fixed buflen, tmp buffer and reallocated the proper buffer, however, it didn't work as I intended.
Any help will be appreciated.
 
> is there any option to check how big message size is in order to invoke recv() with the proper buflen parameter?
Nope.
Well there might be, but it would be pretty specific to your implementation if there was a way.

It wouldn't do a lot of good, since it could change between you asking and you reading. It's a stream of data arriving at irregular intervals.
Either
- your buffer is partially filled with what's available at that moment.
- your buffer is full, and anything over is kept for the next recv call.

You'd have to post your code for more comment.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Hi,
thank you for your interest. My code is just rudimentary implementation of echo server. The part of code responsible for receiving and sending the echo is here:
Code:
    do
    {
        bytesSent = 0;
        sPosition = 0;

        if((bytesReceived = recv(client, buffer, BUFFER_SIZE, 0)) == SOCKET_ERROR)
        {
            reportError(WSAGetLastError());
            throw WinApiError();
        }
        std::cout<<"Received "<<bytesReceived<<" bytes"<<std::endl;
        std::cout<<"Message: "<<buffer<<std::endl;

        bytesToBeSent = bytesReceived;
        while(bytesSent < bytesToBeSent)
        {
            if((bytesSent = send(client, (buffer)+=sPosition, bytesToBeSent, 0)) == SOCKET_ERROR)
            {
                reportError(WSAGetLastError());
                throw WinApiError();
            }
                bytesToBeSent -= bytesSent;
                sPosition += bytesSent;
        }    
        std::cout<<"Sent "<<bytesSent<<" bytes"<<std::endl;
    }
    while (bytesReceived > 0);
What I tried to do is perform sending after the end of do-while loop, not inside of it, however, it doesn't work since my server after invoke of recv is waiting for new data. I could then put the new message in some bigger buffer which I could extend dynamically.
I've read somewhere that there is a way to shorten the time for recv (I mean the server waits some time and if no message comes it goes to the next line of the code). However, I'm looking for more general solution (if any exists).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top