1

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');
9
  • 2
    show us the definition of readInFood() Commented Oct 25, 2012 at 13:20
  • Please indent you code when you post... White space is your friend Commented Oct 25, 2012 at 13:28
  • Looks indented to me. +1 for using perror(). Commented Oct 25, 2012 at 13:29
  • You're sending 100 bytes to the server, even if the user only types 5 or 10 characters. Is the server expecting to get lots of extraneous bytes that it should ignore? Commented Oct 25, 2012 at 13:33
  • 1
    Just make sure that the server's reading matches the way the client sends. If the client sends blocks of 100 bytes, the server should read that way, and vice versa. But it would be better if you sent just the amount of data you needed, either with a length prefix or a unique delimiter. Commented Oct 25, 2012 at 19:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.