bobbybouwmann
bobbybouwmann

Reputation: 1013

scanf() not working after ENTER

I'm making this simple example from server to client where the server functions as a echo server! So the client gets input from the user and sends it to the server, the server then sends it back.

I just got stuck on the input from the user. I used stuff like getchar, scanf and fgets. But when I start typing and press ENTER, then it should continue to the next thing in the program... Sadly that ain't happening.

Here is my code for the message part:

while(1)
{
    char message[1000], servermessage[1000];

    printf("You: ");
    scanf("%s", message);
    printf("%s\n", message);

    if (send(sock, message, strlen(message), 0) < 0) 
    {
        printf("sending failed\n");
        return (EXIT_FAILURE);
    }

    int received = recv(sock, servermessage, sizeof(servermessage), 0);
    if (received < 0) 
    {
        printf("received failed\n");
        return (EXIT_FAILURE);
    }

    servermessage[received] = '\0';
    printf("Server: %s\n", servermessage);
}

close(sock);

Upvotes: 0

Views: 588

Answers (1)

jfly
jfly

Reputation: 8020

After you type the first time, you call recv(), it will block until there is some data to read, you client didn't read any data, so nothing happen. You need check your echo server to find what's wrong. If you need a echo server to test your client, you can use the code for server here.

Upvotes: 1

Related Questions