c++ - How to read my socket messages -
i reading variable bytes messages sockets in c. there problem when byte in message \0. split in 2 parts , first half sent. there receiving problem on client side well.
please suggest format, can use.
one solution, have sent size of message first , send rest of message. solve problem processing becomes complex.
i looking other cleaner solutions ?
/** send data connected host */ bool send_data(const char* data) { //send data if (sendto(sock, data, strlen(data), 0, (const struct sockaddr *) &server, length) < 0) { perror("send failed "); return false; } std::cout << "data sent: length=" << strlen(data) << " \n"; char str[inet_addrstrlen]; inet_ntop(af_inet, &(server.sin_addr), str, inet_addrstrlen); std::cerr << "server.sin_addr=" << str << std::endl; return true; } /** receive data connected host */ void receive(char * buffer, size_t size) { //receive reply server if (recvfrom(sock, buffer, size, 0, (struct sockaddr *) &from, (socklen_t*) & length) < 0) { puts("recv failed"); } char str[inet_addrstrlen]; inet_ntop(af_inet, &(from.sin_addr), str, inet_addrstrlen); std::cerr << "from.sin_addr=" << str << std::endl; }
the data sending integers converted casted char *
. integers small, there upper bytes 00000000. hence problem.
i'll assume have access both server , client code.
you said know size of message, prepare message before sending it, looking \0
character , replacing replacing unique byte sequence identify on client side, replacing original character.
Comments
Post a Comment