【问题标题】:Listen to port continuously and dump data to a text file [closed]连续监听端口并将数据转储到文本文件[关闭]
【发布时间】:2017-02-28 07:01:45
【问题描述】:

我有以下代码通过端口中的套接字侦听调用,但我缺少创建文本文件然后将数据转储到其中的函数,因为稍后我需要解析它并在其他地方显示它.

/*
    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;
}

【问题讨论】:

  • 你有什么尝试吗? google - 如何在 c 中编写文件

标签: c linux sockets ubuntu


【解决方案1】:

您快到了,但只需对如何写入文件进行一点研究就足够了。

在您的 while(1) 循环中,只需将以下内容添加到您的代码中(请参阅代码 cmets):

...

//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() 中的a 选项而附加

【讨论】:

    【解决方案2】:

    查看函数fopen()fwrite() 和(最后但并非最不重要的)fclose() 来解决这个问题。它们应该为您提供将数据写入文件所需的基础知识。

    【讨论】:

      猜你喜欢
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 2021-07-15
      相关资源
      最近更新 更多