c++ - Sending "string" and "int" var using send() method in socket -


in below code want send hit->first , *vit via socket:

for (std::map < int, std::vector < std::string > >::iterator hit = three_highest.begin(); hit != three_highest.end(); ++hit) {     //std::cout << hit->first << ":";      (std::vector < std::string >::iterator vit = (*hit).second.begin(); vit != (*hit).second.end(); vit++) {         std::cout << hit->first << ":";         std::cout << *vit << "\n"; 

results:

2:one 3:ff 3:rr 6:fg 

i.e. occurrence:word

i want send using send() method in socket. if both have been int or char can store 2 dimentional array , send array. here not case.

can 1 figure out way sent using single send() in socket?

you can pack message string via stringstream, , send message (prefixed string length). other end read string length, read many bytes string. string fed stringstream extract data.

in pseudo-ish code, sending like:

std::ostringstream oss; (...) {     (...) {         oss << first << " " << second << "\n";     } } std::vector<char> v(oss.str().c_str(), oss.str().c_str() + oss.str().size()); uint32_t len = v.size(); iovec iv[2] = { { &len, sizeof(len) }, { &v[0], len } }; writev(sock, iv, 2); 

the receive code like:

uint32_t len; recv(sock, &len, sizeof(len), 0); std::vector<char> v(len + 1); recv(sock, &v[0], len, 0); v[len] = '\0'; std::string s(&v[0]); std::istringstream iss(s); (;;) {     (;;) {         iss >> first >> second;     } } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -