serverFork.cpp

00001 /*
00002   サーバ起動後に fork したとき、子プロセス側で accept できるかテスト
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include <unistd.h>
00008 #include "tcpipServer.h"
00009 
00010 
00011 int main(int argc, char *argv[]) {
00012 
00013   enum { Port = 57577 };
00014   TcpipServer* server = new TcpipServer();
00015   server->activate(Port);
00016 
00017   enum { ChildId = 0 };
00018   pid_t pid = fork();
00019   if (pid < 0) {
00020     perror("fork");
00021     exit(1);
00022   }
00023 
00024   if (pid == ChildId) {
00025     printf("...wait connection\n");
00026     TcpipDevice* new_con = server->accept(1000);
00027     if (!new_con) {
00028       printf("accept fail\n");
00029     }
00030 
00031     delete new_con;
00032     delete server;
00033     printf("child end.\n");
00034     exit(0);
00035   }
00036   delete server;
00037 
00038   usleep(1000);
00039   TcpipDevice to_server;
00040   int con_success = to_server.connect("localhost", Port);
00041   printf("server connection: %s\n", (con_success < 0) ? "false" : "true");
00042 
00043   return 0;
00044 }
00045 

Generated on Mon Apr 13 22:52:03 2009 by  doxygen 1.5.7.1