Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <queue>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <sys/socket.h>
  7. #include <netdb.h>
  8. #include <arpa/inet.h>
  9. #include <string.h>
  10. #include <algorithm>
  11.  
  12. using namespace std;
  13.  
  14. void printWall(const queue<string> messages, int clientSocket)
  15. {
  16. string printclient;
  17. printclient = "Wall Contents\n-------------\n";
  18. send(clientSocket, printclient.c_str(), printclient.size(), 0);
  19. if (messages.empty())
  20. {
  21. printclient = "[NO MESSAGES - WALL EMPTY]\n";
  22. send(clientSocket, printclient.c_str(), printclient.size(), 0);
  23. }
  24. else
  25. {
  26. queue<string> temp = messages;
  27. while (!temp.empty())
  28. {
  29. printclient = temp.front() + "\n";
  30. send(clientSocket, printclient.c_str(), printclient.size(), 0);
  31. temp.pop();
  32. }
  33. }
  34. printclient = "\n";
  35. send(clientSocket, printclient.c_str(), printclient.size(), 0);
  36. }
  37.  
  38. void post(int size, queue<string>& messages, int clientSocket)
  39. {
  40. string name;
  41. string message;
  42. string printclient;
  43. printclient = "Enter name: ";
  44. send(clientSocket, printclient.c_str(), printclient.size(), 0);
  45. char buff[4096];
  46. memset(buff, 0, 4096);
  47. int bytesReceived = recv(clientSocket, buff, 4096, 0);
  48. name = string(buff, bytesReceived);
  49. name.erase(remove(name.begin(), name.end(), '\n'), name.end());
  50. if (name.length() >= 77)
  51. {
  52. printclient = "Error: message is too long!\n";
  53. send(clientSocket, printclient.c_str(), printclient.size() + 1, 0);
  54. }
  55. else
  56. {
  57. printclient = "Post [Max length " + to_string(80 - name.length() - 2) + "]:";
  58. send(clientSocket, printclient.c_str(), printclient.size(), 0);
  59. char buff2[4096];
  60. memset(buff2, 0, 4096);
  61. int bytesReceived2 = recv(clientSocket, buff2, 4096, 0);
  62. message = string(buff2, bytesReceived2);
  63. message.erase(remove(message.begin(), message.end(), '\n'), message.end());
  64. name = name + ": " + message;
  65. if (name.length() > 80)
  66. {
  67. printclient = "Error: message is too long!\n";
  68. send(clientSocket, printclient.c_str(), printclient.size(), 0);
  69. }
  70. else
  71. {
  72. if (messages.size() < size)
  73. messages.push(name);
  74. else
  75. {
  76. messages.pop();
  77. messages.push(name);
  78. }
  79. printclient = "Successfully tagged the wall.\n";
  80. send(clientSocket, printclient.c_str(), printclient.size(), 0);
  81. }
  82. printclient = "\n";
  83. send(clientSocket, printclient.c_str(), printclient.size(), 0);
  84. }
  85. }
  86.  
  87. int main(int argc, char *argv[])
  88. {
  89. int size = 20;
  90. int port = 5514;
  91. queue<string> messages;
  92. string initmsg;
  93. if (argv[1])
  94. {
  95. size = strtol(argv[1], nullptr, 0);
  96. if (argv[2])
  97. port = strtol(argv[2], nullptr, 0);
  98. }
  99. initmsg = "Wall server running on port " + to_string(port) + " with queue size " + to_string(size) + ".\n";
  100. cout << initmsg << "\n";
  101. start:
  102. int listening = socket(AF_INET, SOCK_STREAM, 0);
  103. if (listening == -1)
  104. {
  105. cerr << "Cannot create the socket.\n";
  106. return -1;
  107. }
  108. sockaddr_in hint;
  109. hint.sin_family = AF_INET;
  110. hint.sin_port = htons(port);
  111. inet_pton(AF_INET, "0.0.0.0", &hint.sin_addr);
  112. if (bind(listening, (sockaddr*)&hint, sizeof(hint)) == -1)
  113. {
  114. cerr << "Cannot bind to IP or port.\n";
  115. return -1;
  116. }
  117. if (listen(listening, SOMAXCONN) == -1)
  118. {
  119. cerr << "Cannot listen via listener.\n";
  120. return -1;
  121. }
  122. sockaddr_in client;
  123. socklen_t clientSize = sizeof(client);
  124. char host[NI_MAXHOST];
  125. char svc[NI_MAXSERV];
  126. int clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
  127. if (clientSocket == -1)
  128. {
  129. cerr << "Client cannot connect.\n";
  130. return -1;
  131. }
  132. close(listening);
  133. memset(host, 0, NI_MAXHOST);
  134. memset(svc, 0, NI_MAXSERV);
  135. int result = getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, svc, NI_MAXSERV, 0);
  136. printWall(messages, clientSocket);
  137. string command;
  138. char buff[4096];
  139. while (command != "kill")
  140. { memset(buff, 0, 4096);
  141. initmsg = "Enter command: ";
  142. send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
  143. if (command != "quit") {
  144. int bytesRecv = recv(clientSocket, buff, 4096, 0);
  145. command = string(buff, bytesRecv);
  146. command.erase(remove(command.begin(), command.end(), '\n'), command.end());
  147. }
  148. if (command == "clear")
  149. {
  150. while (!messages.empty())
  151. messages.pop();
  152. initmsg = "Wall cleared.\n\n";
  153. send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
  154. printWall(messages, clientSocket);
  155. }
  156. else if (command == "post")
  157. {
  158. post(size, messages, clientSocket);
  159. printWall(messages, clientSocket);
  160. }
  161. else if (command == "kill")
  162. {
  163. initmsg = "Closing socket and terminating server. Bye!\n";
  164. send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
  165. close(clientSocket);
  166. }
  167. else if (command == "quit")
  168. {
  169. initmsg = "Come back soon. Bye!\n";
  170. send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
  171. close(clientSocket);
  172. goto start;
  173. }
  174. else
  175. {
  176. initmsg = "Invalid command.\n";
  177. send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
  178. }
  179. }
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement