在输出文件中显示客户端的IP地址?

时间:2013-10-22 06:09:38

标签: c linux sockets

我已经实现了一个程序,我的客户端捕获传入的数据包并将其保存在文件“sniffer_all_log.txt”中并将其发送到服务器。然后,服务器在附加模式下创建一个新文件(“output.txt”),保存接收文件的内容。

服务器必须在output.txt文件中打印客户端的IP地址!我该怎么做?以下程序显示终端中的IP地址,但不显示输出文件中的IP地址。我该如何显示它?

服务器程序:

int main()
{
int sockfd, new_sockfd,x1,x2,log,n;
int server_len, client_len;
struct sockaddr_in address;
struct sockaddr_in client_address;
char * fname;
int buffsize=1024;
char buffer1[1024];


if((sockfd = socket(AF_INET,SOCK_STREAM,0))>0)
printf("\n Socket was created\n");

/*  Name the socket.  */

address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = 9734;
server_len = sizeof(address);
bind(sockfd, (struct sockaddr *)&address, server_len);

/*  Create a connection queue and wait for clients.  */

listen(sockfd, 5);
while(1)
    {

         char ch;
         printf(" Server waiting..\n");

/*  Accept a connection to collect report from agent  */

     client_len = sizeof(client_address);
     new_sockfd = accept(sockfd,(struct sockaddr *)&client_address, &client_len);

char clntName[INET_ADDRSTRLEN];

if(inet_ntop(AF_INET,&client_address.sin_addr.s_addr,clntName,sizeof(clntName))!=NULL)
{
printf(" Client = %s\n",clntName);
}  
else
{ 
printf("Unable to get address\n");
}


     if (new_sockfd==-1) { perror("Connection Not Accepted!!"); return(1);}
     else 
         {
           printf(" Client is connected\n");
           printf(" Server received the file and saved it in 'output.txt'\n\n");
           log=open("output.txt",O_CREAT|O_RDWR|O_APPEND,0777);

         }

         do
              { 

               x1=read(new_sockfd, buffer1, 1024); 
               x2=write(log,buffer1,x1);
              }
           while (x1>0);
           close(log); 
         }
      close(new_sockfd);  
    }

我的output.txt文件如下所示:

    ***********************UDP Packet*************************
//here i need to print "report is from client ip :....."

Ethernet Header
|-Destination Address : 01-00-5E-00-00-02 
|-Source Address      : 00-00-0C-07-AC-3B 
|-Protocol            : 8 

IP Header
|-IP Version        : 4
|-IP Header Length  : 5 DWORDS or 20 Bytes
|-Type Of Service   : 192
|-IP Total Length   : 48  Bytes(Size of Packet)
|-Identification    : 0
|-TTL      : 1
|-Protocol : 17
|-Checksum : 61927
|-Source IP        : 172.16.59.3
|-Destination IP   : 224.0.0.2

UDP Header
|-Source Port      : 1985
|-Destination Port : 1985
|-UDP Length       : 28
|-UDP Checksum     : 42701

IP Header
01 00 5E 00 00 02 00 00 0C 07 AC 3B 08 00 45 C0         ..^........;..E.
00 30 00 00                                             .0..
UDP Header
00 00 01 11                                             ....
Data Payload
00 1C A6 CD 00 00 10 03 0A 6E 3B 00 63 69 73 63         .........n;.cisc
6F 00 00 00 AC 10 3B 01                                 o.....;.

1 个答案:

答案 0 :(得分:0)

接受客户后:

FILE* fd = NULL;
char *ip_addr = inet_ntoa(client_address.sin_addr);

fd = fopen("ip.txt","wa");
if(NULL == fd)
{
    printf("\n fopen() Error!!!\n");
    return 1;
}
fwrite(ip_addr,1,strlen(ip_addr),fd);
fclose(fd);

如需进一步阅读:socket address conversionsfile operations