简体   繁体   English

C中基于回合的聊天程序

[英]Turn based chat program in c

I'm currently writing a chat program in C. It operates in two terminals, and uses one program. 我目前正在用C编写聊天程序。它在两个终端中运行,并使用一个程序。 A sample input of session 1 would look like: 会话1的样本输入如下所示:

./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. 其中a.txt是用于输入/阅读的文件,b.txt是用于输出/发送消息的文件。 Phil is the chat handle. 菲尔是聊天室。 It follows that the input of session 2 looks like: 因此,会话2的输入如下所示:

./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? 我的问题是,在我的while循环中哪里出问题了,在我能阅读另一会话发送的消息之前必须发送消息到哪里? 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". 在下面的代码中,可以通过将句柄之一设置为“ 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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