开发者

buffer's socket to queue issue

开发者 https://www.devze.com 2023-01-28 21:48 出处:网络
I have somecode: a declaration of a queue: typedef deque<char*, allocator<char*> > CHARDEQUE;

I have some code:

a declaration of a queue:

typedef deque<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 I can't copy data to my queue which is what i want, I could just point to it...can any one help in this?

for more information, my program is receiving raw data, that's why i use char array.. any ideas how to fix this?


The problem is that you are pushing a "char*" which is a pointer into your queue, not the actual data!

Use a std::vector<char>, as below (no error checking etc - which you should do btw.!):

std::deque<std::vector<char> > p;


std::vector<char> read_buff;

// per loop iteration
read_buff.resize(1024);
// read
bytes_read = recvfrom(sock,&read_buff[0], 1024,0, (struct sockaddr *)&client_addr, &addr_len);
// now resize to contents
read_buff.resize(bytes_read);

// push this
p.push_back(read_buff);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号