简体   繁体   中英

I can't understand the result of this chatting program

So I made a simple server-client program that can communicate with each other using fifo file (msgfifo).

My question is: When I type a message which includes a space, the receiver process runs several times with number of words.

That's not I expected, as I expected to print it out as a whole sentence, but it doesn't, and I want to know why.

When I type a something to send, process send SIGUSR1 signal to another.

/* receive msg part */
/* added this using sigset(SIGUSR1, receiveSIGUSR1) */

void receiveSIGUSR1()
{
    char* msg = "\nIncoming Message from client...";
    char* msg2 = "\nClient : ";
    char buf[max_of_msg];
    int fd;
    write(1, msg, strlen(msg)+1);
    fflush(stdin);
    if( (fd = open("./msgpipe", O_RDONLY)) < 0)
    {   perror("open"); exit(1);    }
    read(fd, buf, max_of_msg);
    close(fd);
    write(1, msg2, strlen(msg2)+1);
    write(1, buf, strlen(buf)+1);
    flag = 0;
}

/*send msg part*/

while(1)
{
    flag = -1;
    printf("\nType what u want to send : ");
    scanf(" %s", msg);
    if(flag == 0)   continue;
    printf("msgtaken\n");
    fflush(stdin);
    if( (fd = open("./msgpipe", O_RDWR)) < 0)
    {   perror("exit"); exit(1);    }
    kill(clpid, 30);
    sleep(2);
    printf("Send message to Client..\n");
    write(fd, msg, max_of_msg);
    printf("Message Sent...\n");
}

Expected:

Client : Hello Server this is client

Actual: /* server */


Incoming Message from client...
Hello
Incoming Message from client...
this
Incoming Message from client...
is
Incoming Message from client...
client

Type what u want to send :

/ client /


Type what u want to send : Hello Server This is client
msgtaken
Send message to server..
Message sent

Type what u want to send : msgtaken
Send message to server..
Message sent

Type what u want to send : msgtaken
Send message to server..
Message sent

Type what u want to send : msgtaken
Send message to server..
Message sent

Type what u want to send : msgtaken
Send message to server..
Message sent


Type what u want to send :

That's because this is how it takes input:

scanf(" %s", msg);

Let's have a look at the documentation of scanf (emphasis mine):

s: Any number of non-whitespace characters, stopping at the first whitespace character found . A terminating null character is automatically added at the end of the stored sequence.

That's why it stops after Hello when you send Hello Server this is client . Also note the space in " %s" , this means that it will also ignore any whitespace at the start of the input. So when it reads the Server on the next run through the loop, this makes it ignore the space between Hello and Server . As a result, it goes through the loop five times, and the messages each time are Hello , Server , this , is and client .

Instead, you could use getline :

char *message = NULL;
size_t length;
getline(&message, &length, stdin);
// use message
free(message);

A little side note: to make your scanf call more safe, you can specify a maximum size for the string input:

scanf(" %99s", msg);

Per example, this would mean that only 99 char can be read in, plus a null terminator, for a buffer size of 100. This way you can avoid undefined behavior that would occur if the user would enter a string that is too big for your buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM