使用父进程和子进程使用c调用方法

时间:2015-09-07 14:23:15

标签: c linux ipc

我正在使用c程序开发客户端服务器文件传输来操作父/子进程以触发接收文件和发送文件的方法。执行是在ubuntu上完成的,我得到一个非常奇怪的输出

listen to port 2000
waiting for client connect
client connect from "IP address":"port number"
No such file directory
done with this client sent 0 byte in 0 send
close connection
waiting for - client connect

此消息再次打印。客户端发送文件后,最后一条消息再次打印。我注意到我的服务器仍未关闭。

这是我的问题,我认为这是由于服务器中的进程执行,首先我使用fork来创建父进程和子进程,父进程将通过触发函数处理来自客户端的发送文件,子进程将触发其他函数接收文件。我这样做是因为我计划在我的服务器端实现消息队列,我希望我的孩子将它发送到队列中,我的父母将从队列中获取它。我可以知道我的问题是什么,我可以在接收/发送方法或其他任何地方实现消息队列吗?<

我的fork()的for循环

 for( ; ; ) /* run forever*/ 
           {   
            clientAddressLength = sizeof(clientAddress);    
            printf ("Waiting for a client to connect...\n\n");    /* block until some client connects */    
            if ( (fileDescriptorConnection = accept(serverSocketFileDescriptor, (struct sockaddr*) &clientAddress,&clientAddressLength)) < 0 )    
            {      
            perror("accept error");     
             break; /* exit from the for loop */   
                }        
            /* convert numeric IP to readable format for displaying */   
             inet_ntop(AF_INET, &(clientAddress.sin_addr), readableIP, INET_ADDRSTRLEN);    
             printf("Client connected from %s:%d\n",readableIP, ntohs(clientAddress.sin_port) );    
              /* fork a new child process */   
             if ( (childProcessID = fork()) == 0 ) /* fork returns 0 for child */    
             {      
             close (serverSocketFileDescriptor); /* close child's copy of serverSocketFileDescriptor */       
             /* do your work*/     
              obtainFileByName(fileDescriptorConnection, fileName);      

              printf("Closing connection\n");     
               /* done */      
                close(fileDescriptorConnection);  /* close connected socket*/     
                 exit(0); /* exit child process */    
              }   
                         else if ( (childProcessID = fork()) > 0 ) /* fork returns 0 for child */    
             {      
             close (serverSocketFileDescriptor); /* close child's copy of serverSocketFileDescriptor */       
             /* do your work*/     

              fileSent(fileDescriptorConnection, fileName);      
              printf("Closing connection\n");     
               /* done */      
                close(fileDescriptorConnection);  /* close connected socket*/     
                 exit(0); /* exit child process */    
              }   
               close(fileDescriptorConnection); /* close parent's copy of fileDescriptorConnection */      
            } /* end for */      
            close(serverSocketFileDescriptor); /* close listening socket*/ 
             return 0; 
} 


我提到的方法程序

void obtainFileByName(int sock, char* fileName) 
{    
     /* read name of requested file from socket */     
      if ( (bytesReceive = recv(sock,  stringReceive, receivingBuffer, 0)) < 0) 
      {    
       printf("Nothing receive");   
       return; 
      }   
       sscanf ( stringReceive, "%s\n", fileName); /* discard CR/LF */  
} 
int fileSent(int sock, char *fileName) 
{  
  if( (file = open(fileName, O_RDONLY)) < 0)  /* can't open requested file */ 
   {    
   perror(fileName);    
   if( (bytesSent = send(sock, message ,strlen(message), 0)) < 0 )  
     {     
     printf("send error");     
       return -1;    
     }  
    }  
    else /* open file successful */  
    {   
     printf("Sending file: %s\n", fileName);   
      while( (bytesRead = read(file, stringSent, receivingBuffer)) > 0 )    
      {     
       if( (bytesSent = send(sock, stringSent, bytesRead, 0))< bytesRead )     
        {        
        printf("Nothing send");        
        return -1;      
        }     
         counterForSending++;     
         fileSizeSent += bytesSent;    
      }   
       close(file);  
    } /* end else */   
    printf("Done with this client. Sent %d bytes in %d send(s)\n\n", fileSizeSent, counterForSending); 
    return counterForSending; 
} <br><br> the full program will be post if needed

0 个答案:

没有答案