2013-11-09 66 views
0

我的項目是:客戶端接收來自以太網的原始數據包,將其保存在名爲「sniff_data.bin」的文件中,並將其發送到服務器。服務器接收內容處理數據包(區分tcp,icmp,udp等)並保存在名爲'info_agent_ report'的文本文件中。 txt'文件。我認爲我的代碼存在一些錯誤。任何人都可以請指導我,幫助我。如何使用c socket程序顯示客戶端地址?

int main() 
{ 
int sockfd, new_sockfd,log,n,x1,x2; 
int server_len, client_len,len; 
int cont,fh,cont2,x; 
int result1; 
struct sockaddr_in serveraddress; 
struct sockaddr_in address; 
struct sockaddr_in client_address; 

FILE *ia_address; 
char *fname = "/home/shishira/Desktop/packet_capture/info_agent_report.txt"; 
int buffsize=1024; 
char buffer1[1024]; 
char buffer[1024]; 
char clntName[INET_ADDRSTRLEN]; 

if((sockfd = socket(AF_INET,SOCK_STREAM,0))>0) 
printf("\n ---------------------------Task Agent---------------------------\n"); 
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 = htons(9734); 
server_len = sizeof(address); 
bind(sockfd, (struct sockaddr *)&address, server_len); 

/* Create a connection queue and wait INFO_AGENT_REPORTS */ 

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

     char ch; 

     printf("\n\n Task agent waiting...\n"); 

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

    client_len = sizeof(client_address); 
new_sockfd = accept(sockfd,(struct sockaddr *)&client_address, &client_len); 
if (new_sockfd==-1) { perror("Connection Not Accepted!!"); return(1);} 
else 
{ 
// x=fork(); 
// if (x==0) // child process starts 
    // { 
     printf("\n Information agent is connected\n"); 

    //for displaying the client address  
if(inet_ntop(AF_INET,&client_address.sin_addr.s_addr,clntName,sizeof(clntName))!=NULL) 
     { 
      ia_address = fopen("info_agent_report.txt","a+"); 
      fprintf(ia_address,"\nFrom InformationAgent:%s\n",clntName); 
      fclose(ia_address); 
     } 
printf("\n Task agent processed the contents and saved it in 'info_agent_report' 
file\n\n"); 
     log=open("info_agent_report.txt",O_CREAT|O_RDWR|O_APPEND,0777); 
     if(log==-1) 
       { 
        perror("cannot open info_agent_report file\n"); 
        return(1); 
       } 

     do 
     { 
      x1=read(new_sockfd, buffer1, 1024); 
      x2=write(log,buffer1,x1); 
     } 
    while (x1>0); 
    data_process();//for processing the packet 
    close(log); 
// } child process ends 
    close(new_sockfd); 

    } 

} 

我已經編寫了在info_agent_report.txt中顯示客戶端地址的代碼。但沒有得到dispalyed :(

+0

'log'和'ia_address'是相同的描述符文件,可以覆蓋地址,或者文件被「關閉(日誌)」截斷? – halfbit

+0

我現在編輯了代碼,我已經編寫了用於顯示客戶端地址但不顯示的代碼!? – Beginner

回答

0

要顯示的客戶名稱:(把這種接受之後)

char clntName[INET_ADDRSTRLEN]; // String to contain client address 

if (inet_ntop(AF_INET, &clntAddr.sin_addr.s_addr, clntName,sizeof(clntName)) != NULL){ 
    printf("Handling client %s/%d\n", clntName, ntohs(clntAddr.sin_port)); 
} 

或者這樣:

char clntName[INET6_ADDRSTRLEN]; 
char portName[6]; 

if(getnameinfo(&client_address,sizeof client_address,clntName,sizeof(clntName),NULL,0,NI_NUMERICHOST|NI_NUMERICSERV|NI_NUMERICSCOPE)==0){ 
    printf("Client = %s/%s\n",clntName,portName); 
} else { 
    printf("Unable to get address\n"); 
} 
+0

我已經使用它sir ,但它沒有顯示:( – Beginner

+0

@ user2 971609我不知道爲什麼這不工作..你是積極的客戶端連接? – sukhvir

+0

是客戶端連接併發送內容到服務器 – Beginner

相關問題