10

The Tcp server takes only one command. Need to Empty the Recovery Buffer?

 3 years ago
source link: https://www.codesd.com/item/the-tcp-server-takes-only-one-command-need-to-empty-the-recovery-buffer.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The Tcp server takes only one command. Need to Empty the Recovery Buffer?

advertisements

I am able to send the command "insert data" to the tcp server and it will do what is suppose to. I would like the server to take multiple commands one after the other. At the moment if i send "insert data" and then hit enter and then send "bob" which should not do anything the server responds as if i sent "insert data" again. If you think i should post full source code up let me know in comments.. Screenshot: http://imgur.com/UNRFb5n

#define buf 2000
void *connection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    ssize_t read_size;
    char *message , client_message[buf];
    //char *contents;
    //contents = "hello";
    //strcpy(mess,contents);
    //Send some messages to the client
    message = "Greetings! I am your connection handler\n";
    write(sock , message , strlen(message));

    message = "Now type something and i shall repeat what you type \n";
    write(sock , message , strlen(message));

    //Receive a message from client
    while( (read_size = recv(sock , client_message , buf , 0 )) > 0 )
    {
        //write(sock , client_message , strlen(client_message));
        char start_char[] = "start";
        char insert_demo_char[] = "insert_demo";
        char *inserting = "Inserting Data\n";
        char *complete = "Task Complete\n";
        if(strcmp(message, start_char))
        {
            printf("Starting...\n");
            //start();
            //printf("it works");
            //fflush( stdout );
        }
        if(strcmp(message, insert_demo_char))
        {
            write(sock , inserting , strlen(inserting));
            printf("Inserting data\n");
            insert_demo();
            write(sock, complete, strlen(complete));
            printf("Finished Inserting Data\n");
        }
    }
    if(read_size == 0)
    {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if(read_size == -1)
    {
        perror("recv failed");
    }

    //Free the socket pointer
    free(socket_desc);

    return 0;
}


while( (read_size = recv(sock , client_message , buf , 0 )) > 0 )
{
    [...]
    if(strcmp(message, start_char))

After you receive data into client_message, you are checking the buffer named message instead. Since you didn't recv() into that buffer, of course it has not changed.

Also note that strcmp() returns 0 if the two strings are equal, and non-zero if the two strings are different; you may have that backwards in your if(strcmp()) tests (I'm not sure what behavior you intended).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK