tags:

views:

102

answers:

4

From the below piece of code, why I am getting Reading Socket for response

int Read(int sock, char *p, int size)
{
    int remain, read=0;

    remain = size;
    while (remain > 0 ) {

        if ((read = recv(sock, p, remain, 0)) < 0) {

            /* Error */
            return(read);
         } else if (read == 0 || *p == 0x0a) {

            /* EOF */
            break;
        }
        remain -= read;
        p += read;
    }

    return(size - remain);
}



while (!done)
{
    printf("***Reading  Socket for response***");
    rsplen= Read(myVsHandle.sock,(char *)encXMLResponse,MAX_RSP_LEN);
    if (rsplen < 0 )
    {
        printf("Internal Communication Error");
        return -1;
    }
    else if (rsplen >0)
        printf("Revieved response");
        done++;
        return 0;
    else if (rsplen == 0)
    {
        printf("Reading socket");
    }
A: 

This:

if ((read = recv(sock, p, remain, 0)) < 0) {

Should be

if ((read = recv(sock, p, remain, 0)) > 0) { // Greater then 0, because recv returns the number of bytes received if successful, if it fails -1.  
Tony
Umm, your comment is correct, but the condition is checking for error, so it should be `< 0`.
Alok
come on, read the code !! he is looping until there is an error or nothing to receive. his test is fine: when `recv()` returns a value < 0, it is an error, as the comment in the code is telling you !
Adrien Plisson
A: 

Your problem could be that you are not ending your output with a newline. Try ending your outputs with a newline (\n). stdout is line buffered, so you may not see anything for a long time if you don't output a newline.

Another possibility is that you don't return from Read() unless you read the specified number of bytes. Depending upon the value of MAX_RSP_LEN, and the amount of data available, Read() may wait forever.

Also, your test: *p == 0x0a looks suspicious. What are you testing here?

Edit: There is another "bug":

else if (rsplen >0)
    printf("Revieved response");
    done++;
    return 0;
else...

You are missing curly braces. In the current form, the code shouldn't compile. Please post actual code.

Alok
This code is compiled and is working properly for many messages. I wonder why it is keep on waiting for infinite period. If read can read even a zero byte message then the corresponding printf should get executed
Sachin Chourasiya
As I said, you are not ending your output with a newline. So even if the `printf()` call gets called, you may not see any output.
Alok
@Sachin: also, is this the actual code? You are missing curly braces, but that makes it not compile. Please post real code.
Alok
+1  A: 

You are waiting for MAX_RSP_LEN bytes to be read - is there that many bytes to be read? Maybe your process is stuck in a blocking read().

Also depending on the sort of socket you are recv()ing from, there is no guarantee on the amount of data you will read, so specifically looking for a value 0x0a may not work.

Beano
I can see the response messages of length less than the MAX_RSP_LEN could be read successfully
Sachin Chourasiya
A: 

You're missing curly braces around the:

else if(rsplen > 0)
    ... statements

It should be:

...
}else if (rsplen >0){
    printf("Revieved response");
    done++;
    return 0;
} ...
Autopulated
That's most likely an error due to typing the code here on SO, instead of posting actual code. Otherwise the next `else` is syntax error.
Alok
I have already stated this is working fine for most of the cases in an online system
Sachin Chourasiya
Oh, thanks for the downvote, I really appreciate the feedback: I mean, no-one had spotted this error and there were like four answers asking for more information.
Autopulated