1

私のミニ プロジェクトは、複数のクライアントが 2 つまたは 3 つのサーバーにファイルを送信する AC ソケット プログラムの実装に関するものです。私はこれらを実装しました。しかし、クライアントのリクエストを処理するには、子プロセスを作成する必要がありますか? どうやってやるの 。リクエストを個別に処理する必要があるようです。誰かが私を案内してください。

サーバ:

int main()
{
int sockfd, new_sockfd,x1,x2,log,n;
int server_len, client_len,len;
int cont,fh,cont2;
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 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 = ntohs(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
{
  printf("\n Information agent is connected\n");
                                                                                             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 received the contents and saved it in 'info_agent_report' file");
  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);
 close(log);
 close(new_sockfd); 

 }

 /*this is to connect to other   server */
/*  connect socket to the interface server's socket.  */

int interface_sockfd = socket(AF_INET,SOCK_STREAM,0);

serveraddress.sin_family = AF_INET;
serveraddress.sin_addr.s_addr = inet_addr("127.0.0.1");
serveraddress.sin_port = 9735;
len = sizeof(serveraddress);
//len=sizeof(address);


if((result1 = connect(interface_sockfd, (struct sockaddr *)&serveraddress, len))==0)
printf("\n Connecting to the Interface server\n");

if(result1 == -1)
{
perror(" Not able to connect to Interface Server!!!!\n");
}

fh = open(fname , O_RDONLY);  
if(fh==-1)
{
perror(" INFO_AGENT_REPORT File is Not Opened!!\n");
return(1);
} 


do
 {
   cont=read(fh, buffer, buffsize);
   cont2=write(interface_sockfd,buffer,cont);
 }
while (cont==1024);
close(fh);    
printf("\n Task agent has sent 'info_agent_report' file to the Interface Server\n\n");
close(interface_sockfd); 
}
}
4

2 に答える 2

2

方法は次のとおりです:(これを後に入れますaccept):

char clntName[INET_ADDRSTRLEN];
FILE *output;

if(inet_ntop(AF_INET,&client_address.sin_addr.s_addr,clntName,sizeof(clntName))!=NULL){
   output = fopen("output.txt","a+");  
   fprintf(output,"%s%c%d",clntName,'/',ntohs(client_address.sin_port));  
   fclose(output);
} else {
   printf("Unable to get address\n"); // i just fixed this to printf .. i had it as print before
}
于 2013-10-22T05:15:30.203 に答える