How to get the sock data after receive it

Posted on 2005-04-26
Last Modified: 2010-04-01
Hi Experts,

I am using the code below to get the sock data after receive it.

    bytesRecv = recv( m_socket, recvbuf, 32, 0 );

But how to get the data from recvbuf?

Thanks and Regards,

Question by:turbot_yu
    LVL 86

    Accepted Solution

    That depends on how 'recvbuf' is set up and what data you are receiving. If it is plain ASCII text, you could

    char recvbuf[33];


        bytesRecv = recv( m_socket, recvbuf, 32, 0 );


       printf("Received: %s\n", recvbuf);
    LVL 8

    Assisted Solution

    You can treat it as byte stream, and it is upto us to interpret that stream.

    For instance if some one is sending a struct like

    struct Foo
        int a;

    Foo a;

    LVL 39

    Assisted Solution

    >>>> But how to get the data from recvbuf?

    You now have

            char recbuf[32];
            int bytesRecv;
            bytesRecv = recv( m_socket, recvbuf, 32, 0 );

    If the message is larger than 32 bytes recv would return SOCKET_ERROR and GetLastMessage is WSAEMSGSIZE. However recvbuf contains the first 32 bytes of the message.

    If you control both clients and server you should increase the buffer size to the maximum message length or to a maximum size that is enough to hold any TCP/IP message, e. g. char recbuf[4096];

    If not you would have to check for WSAEMSGSIZE error and read the message in a while loop like that:

            char recbuf[32];
            int bytesRecv = 0;
            int rc;
            string message;
            while ((rc = recv( m_socket, recvbuf, 32, 0 )) == SOCKET_ERROR)
                   if (GetLastError() == WSAEMSGSIZE)
                         message += string(recvbuf, 32);
                   // break if error
                   return GetLastError();
            message += string(recvbuf, rc);   // add the buffer of the last (or only) read

    Note, you would need

    #include <string>
    using namespace std;

    to use std::string as message buffer. If using MFC you could use CString instead of std::string.

    Regards, Alex

    LVL 4

    Assisted Solution


    >>    bytesRecv = recv( m_socket, recvbuf, 32, 0 );
    >>But how to get the data from recvbuf?
    If above your code is in client. Then server you have to send data to client like send(cli_sock,sendbuff,32,0);
    Then recvbuf automatically have the data what server sent.

    LVL 55

    Assisted Solution

    by:Jaime Olivares
    I notice that everybody uses recv() return to test for error condition but doesn't use it to retrieve length.
    This will be useful if message length is variable (not fixed 32 bytes). Combining above answers:

            char recbuf[4096];  // a big buffer
            int bytesRecv;
            bytesRecv = recv( m_socket, recvbuf, 4096, 0 );

            // put trailing zero at proper position
            if (bytesRecv == SOCKET_ERROR)
                   recbuf[0] = 0;
                  recbuf[bytesRecv] = 0;

    LVL 4

    Assisted Solution

        printf( "Waiting for a client to connect...\n" );
        while (1) {
            AcceptSocket = accept( m_socket, NULL, NULL );
            recv(AcceptSocket, recvbuf, 32, 0);
         printf( "Message Recd: %s\n", recvbuf );
       // Send and receive data.
        int bytesSent;
        char sendbuf[32] = "Client: Sending data.";

        bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );

    See the following link

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now