?
Solved

How to get the sock data after receive it

Posted on 2005-04-26
6
Medium Priority
?
262 Views
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,

Turbot
0
Comment
Question by:turbot_yu
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 800 total points
ID: 13872543
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);
0
 
LVL 8

Assisted Solution

by:novitiate
novitiate earned 200 total points
ID: 13873108
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;
send(&a)

_novi_
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 200 total points
ID: 13874972
>>>> 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);
                     continue;
               }    
               // 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


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Assisted Solution

by:smpoojary
smpoojary earned 600 total points
ID: 13875242

>>    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.
recvbuf[32]='\0';
printf("%s",recvbuf);

-Mahesh
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 200 total points
ID: 13876088
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;
        else
              recbuf[bytesRecv] = 0;

0
 
LVL 4

Assisted Solution

by:smpoojary
smpoojary earned 600 total points
ID: 13882905
server.c
---------
    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 );
     closesocket(AcceptSocket);        
    }
client.c
--------
   // Send and receive data.
    int bytesSent;
    char sendbuf[32] = "Client: Sending data.";

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

See the following link
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21403644.html
-Mahesh
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

840 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