server-client : sending uint32_t data using write() function

i am making a server-client program in which they are exchanging keys of a hash chain. the keys being generated are in uint32_t, however when i exchange these keys between server-client, i have to send them using write(). here problem is that write() transfer data in string format only, therefore before sending i have to convert uint32_t to string using sprintf and then transmit. at reciever side i have to convert back into uint32_t. but i am unable to do so; as per me there are 2 solutions for this.
- either send data using write() in uint32_t format only. i dont how to do it.
- or use a function which can convert uint32_t to char and vice versa. this also i am not aware of.
if any one can provide solution to my problem, i will be grateful to him.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <sstream>

int32_t i;
std::string s;

//convert int32_t to string
std::ostringstream oss;
oss << i;
s = oss.str();

// convert string to int32_i
std::istringstream iss(s);
iss >> i;


Why can you not just do this?

1
2
3
4
uint32_t hash;
...
uint32_t buffer = htonl(hash);
write(fd, &buffer, sizeof(buffer));


Topic archived. No new replies allowed.