2016-10-18 5 views
-1

ポートのソケット経由でコールを聴くコードがありますが、テキストファイルを作成してからデータをダンプする機能がありません。後で私はそれを解析し、どこか別の場所に見せてくれる。ポートを継続的にリッスンし、データをテキストファイルにダンプします。

/* 
    Simple udp server 
*/ 
#include<stdio.h> //printf 
#include<string.h> //memset 
#include <stdlib.h> //exit(0); 
#include <arpa/inet.h> 
#include <sys/socket.h> 

#define BUFLEN 512 //Max length of buffer 
#define PORT 8888 //The port on which to listen for incoming data 

void die(char *s) 
{ 
    perror(s); 
    exit(1); 
} 

int main(void) 
{ 
    struct sockaddr_in si_me, si_other; 

    int s, i, slen = sizeof(si_other) , recv_len; 
    char buf[BUFLEN]; 

    //create a UDP socket 
    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) 
    { 
     die("socket"); 
    } 

    // zero out the structure 
    memset((char *) &si_me, 0, sizeof(si_me)); 

    si_me.sin_family = AF_INET; 
    si_me.sin_port = htons(PORT); 
    si_me.sin_addr.s_addr = htonl(INADDR_ANY); 

    //bind socket to port 
    if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me)) == -1) 
    { 
     die("bind"); 
    } 

    //keep listening for data 
    while(1) 
    { 
     printf("Waiting for data..."); 
     fflush(stdout); 

     //try to receive some data, this is a blocking call 
     if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1) 
     { 
      die("recvfrom()"); 
     } 

     //print details of the client/peer and the data received 
     printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); 
     printf("Data: %s\n" , buf); 

     //now reply the client with the same data 
     if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1) 
     { 
      die("sendto()"); 
     } 
    } 

    close(s); 
    return 0; 
} 
+3

であるためaオプションの追加? Googleの - cでファイルを書く方法 – pm100

答えて

0

あなたはほぼそこにいますが、ファイルへの書き込み方法に関する研究はちょっとだけで十分でした。あなたのwhile(1)ループでは

、ちょうどあなたのコードに次の行を追加します(コードのコメントを参照してください):

... 

//print details of the client/peer and the data received 
printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); 
printf("Data: %s\n" , buf); 

/* ----- Begin: write to file ------------------------------------- */ 
FILE *fp = fopen("/tmp/received_data", "a"); // open in append mode 
if (NULL == fp) { 
    // handle problem here 
} 

if (fwrite(buf, sizeof(char), recv_len, fp) != recv_len) { // write to file 
    // handle problem here 
} 

if (fclose(fp) != 0) { // flush and close the file descriptor 
    // handle problem here 
} 
/* ----- End: write to file ------------------------------------- */  

//now reply the client with the same data 
if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1) 
{ 
    die("sendto()"); 
} 

... 

今毎回あなたが/tmp/でファイルreceived_dataに(*)は、それが書き込まれます、クライアントからのデータを受信。つまり、プログラムを停止するまで、ファイルの内容は増加します。それがどのように動作し、起こり得るエラーを処理するかを知りたい場合は、Striezelの答えのリンクをチェックしてください。

(*):あなたが何をしようとしなかったfopen()

0

その問題を解決するために(少なくとも最後のではなく)fclose()を機能fopen()fwrite()を見て、。彼らはあなたがファイルにデータを書き込むために必要な基礎をあなたに提供するべきです。

関連する問題