| #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) |
| { |
| printf("logfile open failed..(%s)\n", argv[2]); |
| } |
| |
| signal(SIGINT,FatherEXIT); |
| signal(SIGTERM,FatherEXIT); |
| if(tcpServer.InitServer(atoi(argv[1]))==false) |
| { |
| |
| 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); |
| } |
| |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
2023-07-04 【C#】反射基础