【Linux網路程式設計-3】多程序

neon233發表於2024-07-04
#include "TcpServer.h"
#include "CLogFile.h"

CLogFile logfile;
TcpServer tcpServer;

void FatherEXIT(int sig)
{
    if(sig>0)	
    {
        signal(sig,SIG_IGN);
        signal(SIGINT,SIG_IGN);
        signal(SIGTERM,SIG_IGN);
        
        kill(0,15);	
        printf("mpserver exit..(father process:%d)\n",getpid());
        
        tcpServer.CloseListen();
        exit(0);
    }
}

void ChildEXIT(int sig)
{
    if(sig>0)
    {
        signal(sig,SIG_IGN);
        signal(SIGINT,SIG_IGN);
        signal(SIGTERM,SIG_IGN);
        
        printf("mpserver exit..(child process:%d\n),getpid()");
       
        tcpServer.CloseClient();
        exit(0);
    }
}

int main(int argc, char** argv)
{
 	if(argc!=3)	
    {
        printf("Using: ./mpserver port logfile\nExample:./mpserver 5000 /tmp/mpserver.log\n\n");
        return -1;
    }
   
    for(int i=0;i<70;i++)
    {
        signal(i, SIG_IGN);
    }
	
    logfile.m_bBackup=false; //多程序服務程式關閉日誌切換

    if(logfile.Open(argv[2], "a+") == false)	//a+新增,不存在則建立
    {
        printf("logfile open failed..(%s)\n", argv[2]);
    }
    
    signal(SIGINT,FatherEXIT);	
    signal(SIGTERM,FatherEXIT);
    if(tcpServer.InitServer(atoi(argv[1]))==false)
    {
        //printf("server init failed..\n");
        logfile.Write("server init failed..\n");
        FatherEXIT(-1);
    }

    while(true)
    {
        if(tcpServer.Accept()==false)
        {
            logfile.Write("client(%s) connected failed..\n", tcpServer.GetIP());
            continue;	
        }
        
       //父程序處理
        if(fork()>0)
        {
            tcpServer.CloseClient();	
            continue;
        }
		
         //子程序處理
        signal(SIGINT,ChildEXIT);	
        signal(SIGTERM,ChildEXIT);
        
        tcpServer.CloseListen();	
        break;        
             
    }

    logfile.Write("client(%s) connected..\n", tcpServer.GetIP());
    char strbuffer[1024];
    while(true)
    {
        memset(strbuffer,0,sizeof(strbuffer));
        if(tcpServer.Read(strbuffer, 300)==false)  
            break;
        logfile.Write("receive msg: %s\n", strbuffer);

        strcat(strbuffer, ":ok\n");
        if(tcpServer.Write(strbuffer)==false)   break;
    }
    
    logfile.Write("client(%s) disconnected..\n", tcpServer.GetIP());
    FatherEXIT(-1);
}
#include "TcpClient.h"

int main(int argc, char** argv)
{
    TcpClient tcpClient;

    if(tcpClient.ConnectServer("127.0.0.1",5000)==false)
    {
        printf("connect server failed..\n");
        return -1;
    }

    char strBuffer[1024];
    for(int i=0;i<100;i++)
    {
        memset(strBuffer,0,sizeof(strBuffer));
        snprintf(strBuffer,50,"this is msg:(%d)\n",i+1);
        if(tcpClient.Write(strBuffer)==false)   break;
        printf("send msg: %s\n", strBuffer);

        memset(strBuffer,0,sizeof(strBuffer));
        if(tcpClient.Read(strBuffer, 300)==false)    break;
        printf("receive msg: %s\n", strBuffer);
        sleep(1);
    }

}

相關文章