0

I am trying to send a char array to a server and I am not able to send the whole thing. I discovered that I am not receiving that it is sending only the first four packets.

This is the data I want to send in one go.

char msg1[]={0xff,0xfd,0x03,0xff,0xfb,0x18,0xff,0xfb,0x1f,0xff,0xfb,0x20,0xff,0xfb,0x21,0xff,0xfb,0x22,0xff,0xfb,0x27,0xff,0xfd,0x05,0xff,0xfb,0x23};

This is the function I am using to check whether all the bytes have been send or not. and if not. send the rest.

int send_all(int s,char *buf,int *len)
{
    int total=0; //total bytes sent
    int byteslft= *len; //bytes left to be sent
    int n; //value returned by send() 

    while(total<*len)
    {
        n=send(s,(buf+total),byteslft,0);
        if(n== -1)
            break;

        total +=n; //total bytes sent will be in total
        byteslft -=n; //total bytes left to be sent is stored here
    }

    *len= total;
    return (n==-1)?-1:0;
}

And this how I am trying to send it

len=sizeof (*(arr+i)) ;
    if((numbytes=send_all(sockfd,(*(arr+i)),&len))==-1)
    {
        perror("send");
        continue;
    }

I have done the connect(),socket(), etc. steps before this just to be clear.

And this is how I've initialized the array of the pointers to the msg array.

char *arr[]={msg1,msg2,msg3,msg4....};

When I debug my program, I see that it is only sending

ff fd 03 ff //message stops sending here

And that's why I'm not getting the required response from the server.

3
  • I have shown how array is initialized. Commented Apr 30, 2013 at 6:44
  • Please show the declaration of arr and how you initialize it. Commented Apr 30, 2013 at 6:44
  • By the way, since you have a variable called byteslft (pointless abbreviation, btw) you should use that in the while loop. Notice how while(bytesleft > 0) reads somewhat better than while(total < *len)? Commented Apr 30, 2013 at 7:00

1 Answer 1

2

The expression *(arr + i) is not an actual array, but a pointer to an array. This means that the sizeof operation will return the size of the pointer (which is 32 bits, i.e. four bytes on a 32-bit operating system) and not what it points to.

You can only use sizeof to get the size of an array if you have the actual array, not on pointers.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to get the size of the array msg1 in order to send the data. I didn't realize that it would return the pointer size. Is there any way I can get the sizeof msg from the pointer?
@ad3 No, there is no way. You might want to keep a second array with the sizes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.