1

リクエストに応じてキャプチャ画像を作成するサーバーがあるとします。

クライアントは、6 チャット ワードと ID を持つ名前付きパイプを介してサーバーと通信します。

サーバーはイメージを作成し、名前付きパイプを介してクライアントに送信します。

クライアントには、サーバーと通信して結果を取得し、word.png ファイルに保存する関数 create_captcha_files(const char* word) があります。

サーバーには、対応する画像をバッファーに書き込み、最大 16384 バイトの書き込みバイト数を返す関数 size_t captcha(const char* word, char * buffer) が既に実装されています。

したがって、クライアントは次のようになります。

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>


int main()
{
    int fd, fdin, fdpng;
    char * myfifo = "captcha";
    char id = "001";
    char text[9];
    char buf[1024];
    char png[10];
    char palavra[6];


create_captcha_file(const char* palavra) {
    //write to fifo "captcha" the word + id
    mkfifo(myfifo, 0666);
    strcat(strcat(text,palavra),id);
    fd = open(myfifo, O_WRONLY);
    write(fd,text,9):
    close(fd);
    unlink(myfifo);

    //read anwser from server
    mkfifo(id,0666);
    fdin = open(id,O_RDONLY);
    strcat(strcat(png,palavra),".png");
    //create the .png file
    int fdpng = open(id,O_WRONLY | O_CREAT | O_APPEND,S_IRWXU);
        while((read(fdin,buf,1))) 
            {
             write(fdpng,buf,1);
             }
        close(fdpng);
        close(fdin);
     }
    unlink(id);


    return 0;
}

そしてサーバー:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{


    int fd;
    char texto[9];
    char palavra[6];
    char id[3];
    char * myfifo = "captcha";
    buffer[16384];

    //create and read from fifo
    mkfifo(myfifo, 0666);
    fdin = open(myfifo, O_RDONLY);
    read(fdin, texto, 9);
    close(fdin);
    //separate word from id
    for(i=0;i<=9;i++) {
        if(i<7) strcat(palavra,texto[i])
        else strcat(id,texto[i]
    }

size_t captcha(const *char palavra, char * buffer) {
    //create the captcha image and save to buffer
    buffer = create_captcha(palavra);
    return(size_of(buffer));

    }
   captcha(palavra, buffer);

    //write to pipe id the image
    mkfifo(id, 0666);
    fd = open(id, O_WRONLY);
    write(fd,buffer,size_of(buffer)):
    close(fd);
    unlink(fd);

}
4

1 に答える 1