#include #include #include #include #include #include #include #include #include #include using namespace std; /** Server */ struct sockaddr_in server; int server_fd; int sockopt; /** Client */ struct sockaddr_in client; int client_fd; socklen_t clientlen;// = sizeof(client); /** Port the server will listen on, alternately read from config or cmdline. */ int port = 7613; /** Number of bytes read by the socket at a time. */ int nread; /** This is the token that signifies a request is finished. */ const string REQUEST_END_TOKEN = "\r\n\r\n"; /** * Change this to search for your protocol's "message end" token. */ bool isRequestDone(string & req) { return (req.find(REQUEST_END_TOKEN) != string::npos); } /** * Contains the loop that reads the request. Contains sections indicating * where the request is read, where you might do something with the request, * and where you respond to the request. */ void process() { // Read the request //------------------ string request = ""; while(! isRequestDone(request) ) { // Read from the client //----------------------------- char buf; bzero(&buf, 1); nread = recv(client_fd, &buf, 1, 0); request += buf; if (nread < 0) { if (errno == EINTR) continue;// Continue reading else break; // No more } else if (nread == 0) { break; // Nothing left to read } } // Do something with the request //------------------------------ // Send the response: //------------------------------ string response = request; int num_b = response.length(); for (int i = 0; i < num_b; i++) { char cur = response[i]; send(client_fd, &cur, 1, 0); } // Close the connection here close(client_fd); } /** * Initializes the socket that will listen for client connections. */ void initSocket() { // Setup the socket address structure memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(port); server.sin_addr.s_addr = INADDR_ANY; // Create the socket server_fd = socket(PF_INET, SOCK_STREAM, 0); if (!server_fd) { perror("socket error"); exit(-1); } // Set the socket options sockopt = 1; if ( setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)) < 0) { perror("set socket options error"); exit(-1); } } /** * Binds the socket to the port (contained in the 'server' socket address structure). */ void bindSocket() { if (bind(server_fd, (const struct sockaddr *)&server, sizeof(server)) < 0) { perror("bind error"); exit(-1); } } /** * Starts the server listening on the defined port (makes it ready to accept connections) */ void listenSocket() { if (listen(server_fd, SOMAXCONN) < 0) { perror("listen error"); exit(-1); } } /** * The "infinite loop" that accepts clients connections. When a connection is received, * it is processed. */ void acceptLoop() { cout << "Server started." << endl; while ( (client_fd = accept(server_fd, (struct sockaddr *)&client, &clientlen)) > 0) { cout << "Client connected." << endl; process(); cout << "Client connection completed." << endl; } } /** * Initializes and starts the server. */ int main(int argc, char** args) { initSocket(); bindSocket(); listenSocket(); acceptLoop(); return 0; }