Mark Wigg
Mark Wigg

Reputation: 11

C programming socket buffer

Re: this client<->server (foodrequest-foodinfo scenario) I am trying to send receive to a successful client-server connection sock_fd. In this loop, I receive the first information back but the next iteration stops at the keyboard input ie readInFood(). Is there anything wrong with the way I am handling the buffer? or otherwise.

RESPONSE_BUFFER = 2200;
INPUT_BUFFER = 100;
int numbytes;
char foodType[INPUT_BUFFER];
char foodResponse[RESPONSE_BUFFER];

do {

    //send a message to server
    if (send(sock_fd, readInFood(foodType), INPUT_BUFFER, 0) == -1)
        perror("send");

    //receive the message
    if ((numbytes = read(sock_fd, foodResponse, RESPONSE_BUFFER)) == -1) {
        perror("receive");
        exit(EXIT_FAILURE);
    }
    //end the buffer string
    foodResponse[numbytes] = '\0';

    //print the buffer
    printf("\nThis is the information you require: %s", foodResponse);

} while (foodType[0] != 'q' || foodType[0] != 'Q');

Upvotes: 1

Views: 584

Answers (1)

Dr.Knowitall
Dr.Knowitall

Reputation: 10468

My guess is that your socket is blocking because it expects more information or its not getting anything else. In that case perror() will not fire, but your program will continue to wait for info.

Upvotes: 1

Related Questions