Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. int set(string host, int port, uint32_t clientkey, string variableName, string value, uint16_t length)
  2. {
  3. rio_t rio;
  4. int clientfd;
  5. char padding[2] = {0};
  6. char success = 11;
  7. uint16_t request = 0;
  8.  
  9. //Preperation to send stuff to server
  10. clientfd = Open_clientfd(const_cast<char*>(host.c_str()), port);
  11. Rio_readinitb(&rio, clientfd);
  12.  
  13. //Converting key and request number from host to network byte order and sending them to server along with two bytes of padding
  14. clientkey = htonl(clientkey);
  15. request = htons(request);
  16. Rio_writen(clientfd, &clientkey, 4);
  17. Rio_writen(clientfd, &request, 2);
  18. Rio_writen(clientfd, padding, 2);
  19.  
  20. //Variable name can't be over 15 characters, adds padding so string has a concluding null
  21. if (variableName.length() > 15)
  22. {
  23. variableName = variableName.substr(0, 15);
  24. }else{
  25. variableName += "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  26. }
  27.  
  28. //Sends variable name to server
  29. Rio_writen(clientfd, const_cast<char*>(variableName.c_str()), 16);
  30.  
  31. //Length can't be over 99
  32. if (length > 99)
  33. {
  34. value = value.substr(0, 99);
  35. length = 100;
  36. }
  37.  
  38. //Converting length to network byte order and sends to server
  39. length = htons(length);
  40. Rio_writen(clientfd, &length, 2);
  41.  
  42. //Converts length back to host byte order and sends value to server
  43. length = ntohs(length);
  44. Rio_writen(clientfd, const_cast<char*>(value.c_str()), length);
  45.  
  46. //Checks if process succeeded server-side
  47. Rio_readnb(&rio, &success, 1);
  48. Rio_readnb(&rio, padding, PADDING);
  49.  
  50. //If it fails, leaves function and declares it as failed
  51. if (success != 0)
  52. {
  53. cout << "Failed" << endl;
  54. return -1;
  55. }
  56.  
  57. return 0;
  58. }//set()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement