'Scanf reads data entered while waiting for recv

While the server or client is waiting on recv() the user can input (any) N amount of data that will later be scanned by scanf which will cause the other socket to prompt the user for "Message: " N amount of times.

Scanf basically reads data entered when thread was waiting for recv().

Server Socket

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>

void *send_pscanf(char *);
void clear_buffer ();

int main() {

    // Create Socket
    int server_socket;
    server_socket = socket(AF_INET, SOCK_STREAM, 0);

    // Check what server_socket is before bind
    //printf("server_socket before bind: %d\n", server_socket);

    // Define the server address
    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(9009);
    server_address.sin_addr.s_addr = INADDR_ANY;

    // Bind the socket to our specified IP and port
    bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));

    // Check what server_socket is after bind
    //printf("Server_socket after bind: %d\n", server_socket);

    // Listen for connections
    listen(server_socket, 5);

    // Accept connection
    int client_socket;
    client_socket = accept(server_socket, NULL, NULL);
    
    int count = 0;
    char client_response[50];
    while (count != 3) {

        // Send data
        memset(client_response, 0, 50);
        send_pscanf(client_response);
        send(client_socket, client_response, sizeof(client_response), 0);

        // Recv data from client
        recv(client_socket, &client_response, sizeof(client_response), 0);

        // Print the data recieved from the client
        printf("The client sent: %s\n", client_response);
        count += 1;
    }

    // Print the data recieved from the client
    printf("The client sent: %s\n", client_response);
    // Close the socket
    close(server_socket);

    return 0;
}

void *send_pscanf(char *client_response) {
    printf("Message: ");
    scanf("\n %[^\n]s", client_response);
}

Client Socket

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>

void *send_pscanf(char *);
void clear_buffer ();

int main() {

    // Create Socket
    int network_socket;
    network_socket = socket(AF_INET, SOCK_STREAM, 0);
    //printf("network_socket: %d\n", network_socket);

    // Specify an address for the socket
    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(9009);
    server_address.sin_addr.s_addr = INADDR_ANY;

    // Connect to the socket
    int connect_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
    if (connect_status == -1) {
        printf("There was an error connecting to the socket\n");
    }

    // While loop for chat
    char server_response[50];
    char server_message[50];
    int count = 0;
    while(1) {

        memset(server_message, 0, 50);
        // Recieve data from the server;
        recv(network_socket, &server_response, sizeof(server_response), 0);

        // Print the data recieved from the server
        printf("The server sent the data: %s\n", server_response);

        // Send data to the server
        send_pscanf(server_message);
        send(network_socket, server_message, sizeof(server_message), 0);
        count += 1;
    }

    // Close the socket
    close(network_socket);
    return 0;
}

void *send_pscanf(char *server_message) {
    printf("Message: ");
    scanf("\n %[^\n]s", server_message);
}

I would like the user input to be ignored/blocked while waiting on recv() so the input is not read by scanf and so both server and client one have one message prompt one by one.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source