简体   繁体   中英

Turn based chat program in c

I'm currently writing a chat program in C. It operates in two terminals, and uses one program. A sample input of session 1 would look like:

./a.out a.txt b.txt Phil

Where a.txt is the file to be used for input/reading, and b.txt is the file to be used for output/sending a message. Phil is the chat handle. It follows that the input of session 2 looks like:

./a.out b.txt a.txt Jill

So that the input file is the output file of the first session, enabling the chat to work, and for the two terminals to talk to each other.

A sample run of this program would look something like:

Send:    Are you there?
Received [Jill]: Yup!
Send:    Okay see ya!
Received [Jill]: Bye!
^C

And vice versa in the other terminal. However, I'm having trouble getting my program to send the files and receive them automatically. My output looks correct, but I can only receive a message if I send one, which sort of defeats the purpose of the chat. My question is, where in my while loop am I going wrong, to where I have to send a message before I can read the one the other session has sent? Here is the code I have so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[]) {

    // Initialize file variables
    FILE *in;
    FILE *out;

    // Initialize incoming, incoming check, and outgoing variables
    char inc[300] = " ";
    char incCheck[300] = " ";
    char send[300];
    char handle[300];
    int size, counter;
    counter=1;
    // This checks if a user has already entered a message
    // i.e. entered the chat
    if (in = fopen(argv[1], "r")) {
        fclose(in);
    }
    else {
        // create an empty in.txt to bypass segfault if the file doesn't already exist
        in = fopen(argv[1], "w");
        fclose(in);

    // To check if anything has been received yet.
        if (strcmp(inc, incCheck) == 0) {
         printf("Nothing has been received yet.\n");
        }
    }   
    // The while loop that reads and writes the files
    while(1) {

        // Read the incoming file and put it to a string
        // It will read the incoming file over and over until a message is seen
        in = fopen(argv[1], "r");
        fgets(inc, size, in);
        fclose(in);

        // This counter is only for the first message, since it is not possible 
        // that one has already been received.
    if (counter > 0) {
        size=sizeof(send);
        printf("Send: \t");
        fgets(send, size, stdin);
        out = fopen(argv[2], "w");
        fprintf(out, "%s",send);
        fclose(out);
        counter=0;
    }

        // If the incoming file is different, print it out
    if (strcmp(inc, incCheck) != 0) {
        printf("Received [%s]: %s", argv[3], inc);

        in = fopen(argv[1], "r");
        fgets(inc, size, in);
        strcpy(incCheck, inc);
        fclose(in);
        // And prompt to send another message.
        size=sizeof(send);
        printf("Send: \t");
        fgets(send, size, stdin);
        out = fopen(argv[2], "w");
        fprintf(out, "%s",send);
        fclose(out);

        in = fopen(argv[1], "r");
        fgets(inc, size, in);
        fclose(in);

        }
    }

Setting one side of the chat as initiator solves the problem. Is that sufficient ? In code below this is solved by setting one of the handles as "s".

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[]) {

    // Initialize file variables
    FILE *in;
    FILE *out;

    // Initialize incoming, incoming check, and outgoing variables
     char inc[300] = " ";
    char incCheck[300] = " ";
    char send[300];
    char oldsend[300];
    char handle[300];
    int size, counter;
    counter=1;
    // This checks if a user has already entered a message
    // i.e. entered the chat
    if (in = fopen(argv[1], "r")) {
            fclose(in);
    }
    else {
            // create an empty in.txt to bypass segfault if the file    doesn't already exist
            in = fopen(argv[1], "w");
            fclose(in);

    // To check if anything has been received yet.
            if (strcmp(inc, incCheck) == 0) {
             printf("Nothing has been received yet.\n");
            }
    }   
        in = fopen(argv[1], "r");
            fgets(incCheck, size, in);
            fclose(in);
    in = fopen(argv[2], "r");
            fgets(oldsend, size, in);
            fclose(in);

    // The while loop that reads and writes the files
    while(1) {

            // Read the incoming file and put it to a string
            // It will read the incoming file over and over until a message is seen

            // This counter is only for the first message, since it is not possible 
            // that one has already been received.
    if ((counter > 0) && ( strcmp(argv[3],"s")==0)) {
        size=sizeof(send);
        do {
    printf("Send: \t");
    fgets(send, size, stdin);
        }while (strcmp(send, oldsend)==0);
        strcpy (oldsend,send);
        out = fopen(argv[2], "w");
        fprintf(out, "%s",send);
        fclose(out);
        counter=0;
 }
 in = fopen(argv[1], "r");
            fgets(inc, size, in);
            fclose(in);
 }

            // If the incoming file is different, print it out
    if (strcmp(inc, incCheck) != 0) {
 printf("Received [%s]: %s", argv[3], inc);

            in = fopen(argv[1], "r");
            fgets(inc, size, in);
            strcpy(incCheck, inc);
            fclose(in);
            // And prompt to send another message.
            size=sizeof(send);
 do {
            printf("Answer: \t");
            fgets(send, size, stdin);
 } while (strcmp(send, oldsend)==0);
            strcpy (oldsend,send);
            out = fopen(argv[2], "w");
            fprintf(out, "%s",send);
            fclose(out);
            }
    }
    return 0;
}

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