a socket and queue problem
I have some code:
a declaration of a queue:
typedef d开发者_JAVA技巧eque<char*, allocator<char*> > CHARDEQUE;
typedef queue<char*,CHARDEQUE> CHARQUEUE;
CHARQUEUE p;
size_t size_q;
char recv_data[1024];
I use a udp socket to receive data from a distant machine:
this is the loop:
while (1)
{
bytes_read = recvfrom(sock,recv_data,1024,0, (struct sockaddr *)&client_addr, &addr_len);
p.push(recv_data);
size_q=p.size();
printf("%d\n",size_q);
}
but the problem is that the size of queue doesen't grow it's always the same, this is what I see on the screen
0
40
40
40
40
40
...
for more information, my program is receiving raw data, that's why i use char array.. any ideas how to fix this?
sizeof
is determined in compilation time. You probably mean something like p.size()
.
sizeof(p)
does not return the number of elements in the queue ! It is a compile time construct that returns the number of bytes occupied by the CHARQUEUE
type : sizeof(p)
with always give you the same result.
What you need is :
std::cout << p.size() << std::endl;
Just to illustrate this with another example : const char *s = "Something";
is a pointer to a null terminated string, and sizeof(s)
(which is equivalent to sizeof(const char *)
) has nothing to do with the actual length of the string strlen(s)
(which in this case is 9).
another problem, besides the sizeof/p.size() thing is that p.push(recv_data) is pushing the address of recv_data buffer into the queue, not the data itself
精彩评论