Simple Network Socket Guessing Game

🚫
Exiled
Joined
Feb 21, 2012
Messages
281
Reaction score
27
Location
Hell
Hello, here is a very simple network socket game that lets client guess the random number generated in the server. No error checking or advance features. You can however learn from it or improvise it yourself.

_server.cpp
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cerrno>
#include <fcntl.h>

int main()
{
    srand(time(NULL));
    int number = rand() % 100 + 1;
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        std::cerr << "Error creating socket: " << strerror(errno) << std::endl;
        return 1;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(369);
    addr.sin_addr.s_addr = INADDR_ANY;
    if (bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        std::cerr << "Error binding socket: " << strerror(errno) << std::endl;
        return 1;
    }

    if (listen(sockfd, 5) < 0) {
        std::cerr << "Error listening on socket: " << strerror(errno) << std::endl;
        return 1;
    }

    int clientfd = accept(sockfd, NULL, NULL);
    if (clientfd < 0) {
        std::cerr << "Error accepting client: " << strerror(errno) << std::endl;
        return 1;
    }

    while (true)
    {
        char buffer[32] = {0};
        int n = read(clientfd, buffer, sizeof(buffer));
        int guess = atoi(buffer);

        const char* response;

        if (guess == number)
        {
            response = "You won!\n";
            write(clientfd, response, strlen(response));
            close(clientfd);
            break;
        }
        else if (guess < number)
            response = "Your guess is too low.\n";
        else
            response = "Your guess is too high.\n";

        write(clientfd, response, strlen(response));
    }

    close(sockfd);
    return 0;
}

_client.cpp
Code:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cerrno>
#include <fcntl.h>

int main()
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        std::cerr << "Error creating socket: " << strerror(errno) << std::endl;
        return 1;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(369);
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        std::cerr << "Error connecting to server: " << strerror(errno) << std::endl;
        return 1;
    }

    while (true)
    {
        std::cout << "Enter a guess: ";
        int guess;
        std::cin >> guess;

        char buffer[32];
        sprintf(buffer, "%d", guess);
        write(sockfd, buffer, strlen(buffer));

        memset(buffer, 0, sizeof(buffer));
        int n = read(sockfd, buffer, sizeof(buffer));
        std::cout << buffer;

        if (strstr(buffer, "You won") != NULL)
        {
            close(sockfd);
            break;
        }
    }

    return 0;
}
 
Last edited:
Back