Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <queue>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- void printWall(const queue<string> messages, int clientSocket)
- {
- string printclient;
- printclient = "Wall Contents\n-------------\n";
- send(clientSocket, printclient.c_str(), printclient.size(), 0);
- if (messages.empty())
- {
- printclient = "[NO MESSAGES - WALL EMPTY]\n";
- send(clientSocket, printclient.c_str(), printclient.size(), 0);
- }
- else
- {
- queue<string> temp = messages;
- while (!temp.empty())
- {
- printclient = temp.front() + "\n";
- send(clientSocket, printclient.c_str(), printclient.size(), 0);
- temp.pop();
- }
- }
- printclient = "\n";
- send(clientSocket, printclient.c_str(), printclient.size(), 0);
- }
- void post(int size, queue<string>& messages, int clientSocket)
- {
- string name;
- string message;
- string printclient;
- printclient = "Enter name: ";
- send(clientSocket, printclient.c_str(), printclient.size(), 0);
- char buff[4096];
- memset(buff, 0, 4096);
- int bytesReceived = recv(clientSocket, buff, 4096, 0);
- name = string(buff, bytesReceived);
- name.erase(remove(name.begin(), name.end(), '\n'), name.end());
- if (name.length() >= 77)
- {
- printclient = "Error: message is too long!\n";
- send(clientSocket, printclient.c_str(), printclient.size() + 1, 0);
- }
- else
- {
- printclient = "Post [Max length " + to_string(80 - name.length() - 2) + "]:";
- send(clientSocket, printclient.c_str(), printclient.size(), 0);
- char buff2[4096];
- memset(buff2, 0, 4096);
- int bytesReceived2 = recv(clientSocket, buff2, 4096, 0);
- message = string(buff2, bytesReceived2);
- message.erase(remove(message.begin(), message.end(), '\n'), message.end());
- name = name + ": " + message;
- if (name.length() > 80)
- {
- printclient = "Error: message is too long!\n";
- send(clientSocket, printclient.c_str(), printclient.size(), 0);
- }
- else
- {
- if (messages.size() < size)
- messages.push(name);
- else
- {
- messages.pop();
- messages.push(name);
- }
- printclient = "Successfully tagged the wall.\n";
- send(clientSocket, printclient.c_str(), printclient.size(), 0);
- }
- printclient = "\n";
- send(clientSocket, printclient.c_str(), printclient.size(), 0);
- }
- }
- int main(int argc, char *argv[])
- {
- int size = 20;
- int port = 5514;
- queue<string> messages;
- string initmsg;
- if (argv[1])
- {
- size = strtol(argv[1], nullptr, 0);
- if (argv[2])
- port = strtol(argv[2], nullptr, 0);
- }
- initmsg = "Wall server running on port " + to_string(port) + " with queue size " + to_string(size) + ".\n";
- cout << initmsg << "\n";
- start:
- int listening = socket(AF_INET, SOCK_STREAM, 0);
- if (listening == -1)
- {
- cerr << "Cannot create the socket.\n";
- return -1;
- }
- sockaddr_in hint;
- hint.sin_family = AF_INET;
- hint.sin_port = htons(port);
- inet_pton(AF_INET, "0.0.0.0", &hint.sin_addr);
- if (bind(listening, (sockaddr*)&hint, sizeof(hint)) == -1)
- {
- cerr << "Cannot bind to IP or port.\n";
- return -1;
- }
- if (listen(listening, SOMAXCONN) == -1)
- {
- cerr << "Cannot listen via listener.\n";
- return -1;
- }
- sockaddr_in client;
- socklen_t clientSize = sizeof(client);
- char host[NI_MAXHOST];
- char svc[NI_MAXSERV];
- int clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
- if (clientSocket == -1)
- {
- cerr << "Client cannot connect.\n";
- return -1;
- }
- close(listening);
- memset(host, 0, NI_MAXHOST);
- memset(svc, 0, NI_MAXSERV);
- int result = getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, svc, NI_MAXSERV, 0);
- printWall(messages, clientSocket);
- string command;
- char buff[4096];
- while (command != "kill")
- { memset(buff, 0, 4096);
- initmsg = "Enter command: ";
- send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
- if (command != "quit") {
- int bytesRecv = recv(clientSocket, buff, 4096, 0);
- command = string(buff, bytesRecv);
- command.erase(remove(command.begin(), command.end(), '\n'), command.end());
- }
- if (command == "clear")
- {
- while (!messages.empty())
- messages.pop();
- initmsg = "Wall cleared.\n\n";
- send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
- printWall(messages, clientSocket);
- }
- else if (command == "post")
- {
- post(size, messages, clientSocket);
- printWall(messages, clientSocket);
- }
- else if (command == "kill")
- {
- initmsg = "Closing socket and terminating server. Bye!\n";
- send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
- close(clientSocket);
- }
- else if (command == "quit")
- {
- initmsg = "Come back soon. Bye!\n";
- send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
- close(clientSocket);
- goto start;
- }
- else
- {
- initmsg = "Invalid command.\n";
- send(clientSocket, initmsg.c_str(), initmsg.size(), 0);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement