Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int set(string host, int port, uint32_t clientkey, string variableName, string value, uint16_t length)
- {
- rio_t rio;
- int clientfd;
- char padding[2] = {0};
- char success = 11;
- uint16_t request = 0;
- //Preperation to send stuff to server
- clientfd = Open_clientfd(const_cast<char*>(host.c_str()), port);
- Rio_readinitb(&rio, clientfd);
- //Converting key and request number from host to network byte order and sending them to server along with two bytes of padding
- clientkey = htonl(clientkey);
- request = htons(request);
- Rio_writen(clientfd, &clientkey, 4);
- Rio_writen(clientfd, &request, 2);
- Rio_writen(clientfd, padding, 2);
- //Variable name can't be over 15 characters, adds padding so string has a concluding null
- if (variableName.length() > 15)
- {
- variableName = variableName.substr(0, 15);
- }else{
- variableName += "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
- }
- //Sends variable name to server
- Rio_writen(clientfd, const_cast<char*>(variableName.c_str()), 16);
- //Length can't be over 99
- if (length > 99)
- {
- value = value.substr(0, 99);
- length = 100;
- }
- //Converting length to network byte order and sends to server
- length = htons(length);
- Rio_writen(clientfd, &length, 2);
- //Converts length back to host byte order and sends value to server
- length = ntohs(length);
- Rio_writen(clientfd, const_cast<char*>(value.c_str()), length);
- //Checks if process succeeded server-side
- Rio_readnb(&rio, &success, 1);
- Rio_readnb(&rio, padding, PADDING);
- //If it fails, leaves function and declares it as failed
- if (success != 0)
- {
- cout << "Failed" << endl;
- return -1;
- }
- return 0;
- }//set()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement