通过C ++套接字发送std :: map <int,std :: map <std :: string,=“” double =“”>>

时间:2019-05-20 11:10:58

标签: c++ sockets dictionary serialization

我正在尝试通过linux中的TCP套接字发送一个包含int和另一个映射的映射。

地图的格式

map<int, map<string, double>>

this SO link之后,我试图做

unsigned char* mybytemap =  reinterpret_cast<unsigned char*>(&my_map);

然后发送缓冲区,我使用了write()函数,如下所示:

int size = sizeof(mybytemap);
char temp[10];
sprintf(temp, "%d", size);
write(sockfd, temp, strlen(temp));     //send the size for the client
write(sockfd, mybytemap, sizeof(mybytemap));

在客户端:

char temp[10];
n  = read(sockfd, temp, 10);
size = stoi(temp);    //Got Size Of buffer

unsigned char * buf;
if(size != 0)
{
    buf = new unsigned char[size];
    int current=0;
    while(current<size)
    {
        n = read(sockfd,(unsigned char*)(buf)+current, min(1024,size-current));
        if (n <= 0)
        {
            cout<<"ERROR reading from socket when receiving"<<endl;
            break;
        }
        current+=n;
    }
}

map<int, map<string, double>> *test = reinterpret_cast< map<int, map<string, double>>* > (buf);

vector<int> ks;
for(map<int, map<string, double>>::iterator it = test->begin(); it != test->end(); ++it)
{
    ks.push_back(it->first);
    cout<<"id: "<<it->first<<endl;
}

,但是未正确接收地图,并且尝试访问数据时代码崩溃。如何解决?

我需要对地图进行XML处理吗?如果可以的话,有人可以指导我该怎么做吗?

2 个答案:

答案 0 :(得分:3)

您问题中链接的答案不适用于地图(地图不是吊舱)

总体思路:如何对地图进行编码\解码:对地图进行编码,然后对每个键/值进行编码,对键的大小进行编码,然后对键进行编码,然后对值进行编码,然后对值进行编码。以下代码假定您将size_t编码为sizeof(size_t)个字节

template<class T>
std::string encode(T value){
   // data to bytes
}

template<class T> 
T decode(std::string bytes){
  // bytes to data
}

template<class K, class V>
std::string encode_map(std::map<K, V> data){
  std::string result;
  result.append(encode<size_t>(data.size()));
  for(auto iter: data){
    std::string first = encode<K>(iter.first);
    result.append(encode<size_t>( first.size() ));
    result.append(first);
    std::string second = encode<V>(iter.second);
    result.append(encode<size_t>( second.size() ));
    result.append(encode<V>(iter.second));
  }
  return result;
}

template<class K, class V>
std::map<K, V> decode_map(std::string bytes){
  size_t index = 0;
  size_t size = decode<size_t>(std::string(bytes.begin()+index, bytes.begin()+index+sizeof(size_t) ) );
  index += sizeof(size_t);
  std::map<K, V> result;
  for(size_t i = 0; i<size; i++){
    size_t next_size = decode<size_t>(std::string(bytes.begin()+index, bytes.begin()+index+sizeof(size_t) ) );
    index += sizeof(size_t);
    K key = decode<K>(std::string(bytes.begin()+index, bytes.begin()+index+next_size ) );
    index += next_size;
    next_size = decode<size_t>(std::string(bytes.begin()+index, bytes.begin()+index+sizeof(size_t) ) );
    index += sizeof(size_t);
    V value = decode<V>(std::string(bytes.begin()+index, bytes.begin()+index+next_size ) );
    index += next_size;
    result[key] = value;
  }
  return result;
}

答案 1 :(得分:2)

根据建议,您需要执行所谓的串行化。您可以使用Boost.Serialization作为序列化库。它可以处理所有STL类型。一个示例:

std::map<int, std::map<std::string, double>> m1;
// ... (fill m1)

// serialize into a buffer (string):
std::string buffer;
boost::iostreams::back_insert_device<std::string> inserter(buffer);
boost::iostreams::stream<boost::iostreams::back_insert_device<std::string>> ostr(inserter);
boost::archive::binary_oarchive oa(ostr);
oa << m1;
ostr.flush();

// ... (here you can send the contents of buffer as plain bytes-chars via socket)

// deserialize into new map:
boost::iostreams::basic_array_source<char> device(buffer.data(), buffer.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char>> istr(device);
boost::archive::binary_iarchive ia(istr);
std::map<int, std::map<std::string, double>> m2;
ia >> m2;

实时演示(带有所有标题):https://wandbox.org/permlink/NyZeVTrFI0p8RcmY