⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chk_sockfile.cpp

📁 一个不错
💻 CPP
📖 第 1 页 / 共 3 页
字号:
 size_t n_rd; char rbuf[64]; if(sizeof(rbuf)<CliMsg.size()+1) {   WY_THROW( WyRet() );   // rbuf not long enough } if((r=psock.read(rbuf,sizeof(rbuf),n_rd))!=Ok) {   WY_THROW(r); } if(n_rd!=CliMsg.size()) {   WY_THROW( WyRet() );  } if(Wy::_strcmp(CliMsg,WyCSeg(rbuf,n_rd))!=0) {   WY_THROW( WyRet() );  }  if(::wait(&child_status)!=child_pid) {   WY_THROW( WyRet(WyReply(errno)) ); } if(WIFEXITED(child_status)==0) {   WY_THROW( WyRet() );  // child process was not terminated voluntarily } if(WEXITSTATUS(child_status)!=0) {   WY_THROW( WyRet() );  // child process not exist(0) } // EOF test (Ok/zero byte) n_rd=99; if((r=psock.read(rbuf,sizeof(rbuf),n_rd))!=Ok) {   WY_THROW(r); } if(n_rd!=0) {   WY_THROW( WyRet() );  } n_rd=99; if((r=psock.read(rbuf,sizeof(rbuf),n_rd))!=Ok) {   WY_THROW(r); } if(n_rd!=0) {   WY_THROW( WyRet() );  } ::unlink(LocalSockFileName.c_str());}catch(...) { ::unlink(LocalSockFileName.c_str()); throw;};#ifdef WY__TEST_SOCK_INET__// Test WySockFile communication (AF_INET)//// Connect to TestINetIP4 and receieve SvrMsg, then send CliMsg // and exit(0).//static int process_client_inet4(void) throw()try { WyRet r; WySockFile cli; WySockAddrINet4 saddr(TestINetIP4,TestINetPort); // Open socket (PF_INET) and connect to the server // if((r=cli.reset(PF_INET,SOCK_STREAM))!=Ok) {   WY_THROW(r); } if((r=cli.connect(saddr))!=Ok) {   WY_THROW(r); } // stat {   WyFileStat stt;   if((r=cli.stat(stt))!=Ok) {     WY_THROW(r);   }   if(stt.is_sock()==false) {     WY_THROW( WyRet() );   } } // Dump addr {  WyStr str;  WyRet r=saddr.ip().notation(str);  if(r!=Ok) {    WY_THROW(r);  }  if((r=saddr.ip().notation(str))!=Ok) {    WY_THROW( WyRet() );  }  std::cout << "process_client_inet4 connected to: " << str.c_str() << std::endl; } // Receive message from the server (SvrMsg expected) // size_t n_rd; char rbuf[64]; if(sizeof(rbuf)<SvrMsg.size()+1) {   WY_THROW( WyRet() );   // rbuf not long enough, reedit rbuf size. } if((r=cli.read(rbuf,sizeof(rbuf),n_rd))!=Ok) {   WY_THROW(r); } if(n_rd!=SvrMsg.size()) {   WY_THROW( WyRet() );  } if(Wy::_strcmp(SvrMsg,WyCSeg(rbuf,n_rd))!=0) {   WY_THROW( WyRet() );  }  // Send message CliMsg to the server // size_t n_wr; if((r=cli.write(CliMsg.begin(),CliMsg.size(),n_wr))!=Ok) {   WY_THROW(r); } if(n_wr!=CliMsg.size()) {   WY_THROW( WyRet() );  } return(0);}catch(const WyRet& e) { std::cerr << "process_client_inet4: "  << Wy::wrd(e).c_str() << std::endl; return(-1);}catch(...) { std::cerr << "process_client_inet4 : caught throw" << std::endl; return(-1);};// Test WySockFile communication (AF_INET)// t_comm_inet4 fork process_client_inet4 and runs as the server// process_client_inet4 will run as the client//static void t_comm_inet4(void){ WyRet r; pid_t child_pid; int child_status; // Create server socket // WySockFile svr; WySockAddrINet4 saddr(TestINetIP4,TestINetPort); if((r=svr.reset(PF_INET,SOCK_STREAM))!=Ok) {   WY_THROW(r); } if((r=svr.bind(saddr))!=Ok) {   WY_THROW(r); } if((r=svr.listen(1))!=Ok) {   WY_THROW(r); } // stat {   WyFileStat stt;   if((r=svr.stat(stt))!=Ok) {     WY_THROW(r);   }   if(stt.is_sock()==false) {     WY_THROW( WyRet() );   }   std::cout << "stream socket(ipv4), _blksize= " << stt.sf_mode() << '\n'; } // Fork child process // child_pid=::fork(); if(child_pid<0) {   WY_THROW( WyRet(WyReply(errno)) ); } if(child_pid==0) {   ::exit(process_client_inet4()); } // Server accepts connection // WySockFile psock; WySockAddrINet4 paddr; if((r=svr.accept(psock,paddr))!=Ok) {   WY_THROW(r); } if(paddr.is_default()) {   WY_THROW( WyRet() ); } // Dump the accept addr {   WyStr str;   if((r=paddr.ip().notation(str))!=Ok) {     WY_THROW(r);   }  if((r=paddr.ip().notation(str))!=Ok) {    WY_THROW(r);  }   std::cout << "server(PF_INET) accept() address: " << str.c_str() << std::endl;   if((r=psock.sockname(paddr))!=Ok) {     WY_THROW(r);   }   if((r=paddr.ip().notation(str))!=Ok) {     WY_THROW(r);   }  if( Wy::wrd(paddr.ip())!=str) {    WY_THROW( WyRet() );  }   std::cout << " accepted..sockname()= " << str.c_str() << std::endl;   if((r=psock.peername(paddr))!=Ok) {     WY_THROW(r);   }   if((r=paddr.ip().notation(str))!=Ok) {     WY_THROW(r);   }  if((r=paddr.ip().notation(str))!=Ok) {    WY_THROW(r);  }   std::cout << " accepted..peername()= " << str.c_str() << std::endl; } // Send SvrMsg to the client // size_t n_wr; if((r=psock.write(SvrMsg,n_wr))!=Ok) {   WY_THROW(r); } if(n_wr!=SvrMsg.size()) {   WY_THROW( WyRet() );  } // Receive message from the client (CliMsg expected) // size_t n_rd; char rbuf[64]; if(sizeof(rbuf)<CliMsg.size()+1) {   WY_THROW( WyRet() );   // rbuf not long enough } if((r=psock.read(rbuf,sizeof(rbuf),n_rd))!=Ok) {   WY_THROW(r); } if(n_rd!=CliMsg.size()) {   WY_THROW( WyRet() );  } if(Wy::_strcmp(CliMsg,WyCSeg(rbuf,n_rd))!=0) {   WY_THROW( WyRet() );  }  if(::wait(&child_status)!=child_pid) {   WY_THROW( WyRet(WyReply(errno)) ); } if(WIFEXITED(child_status)==0) {   WY_THROW( WyRet() );  // child process was not terminated voluntarily } if(WEXITSTATUS(child_status)!=0) {   WY_THROW( WyRet() );  // child process not exist(0) } // EOF test (Ok/zero byte) n_rd=99; if((r=psock.read(rbuf,sizeof(rbuf),n_rd))!=Ok) {   WY_THROW(r); } if(n_rd!=0) {   WY_THROW( WyRet() );  } n_rd=99; if((r=psock.read(rbuf,sizeof(rbuf),n_rd))!=Ok) {   WY_THROW(r); } if(n_rd!=0) {   WY_THROW( WyRet() );  }};//-----------//// Test WySockFile communication (AF_INET6)//// Connect to TestINetIP6 and receieve SvrMsg, then send CliMsg // and exit(0).//static int process_client_inet6(void) throw()try { WyRet r; WySockFile cli; WySockAddrINet6 saddr(TestINetIP6,TestINetPort); // Open socket (PF_INET6) and connect to the server // if((r=cli.reset(PF_INET6,SOCK_STREAM))!=Ok) {   WY_THROW(r); } if((r=cli.connect(saddr))!=Ok) {   WY_THROW(r); } // stat {   WyFileStat stt;   if((r=cli.stat(stt))!=Ok) {     WY_THROW(r);   }   if(stt.is_sock()==false) {     WY_THROW( WyRet() );   } } // Dump addr {  WyStr str;  WyRet r=saddr.ip().notation(str);  if(r!=Ok) {    WY_THROW(r);  }  if( Wy::wrd(saddr.ip())!=str) {    WY_THROW( WyRet() );  }  std::cout << "process_client_inet6 connected to: " << str.c_str() << std::endl; } // Receive message from the server (SvrMsg expected) // size_t n_rd; char rbuf[64]; if(sizeof(rbuf)<SvrMsg.size()+1) {   WY_THROW( WyRet() );   // rbuf not long enough, reedit rbuf size. } if((r=cli.read(rbuf,sizeof(rbuf),n_rd))!=Ok) {   WY_THROW(r); } if(n_rd!=SvrMsg.size()) {   WY_THROW( WyRet() );  } if(Wy::_strcmp(SvrMsg,WyCSeg(rbuf,n_rd))!=0) {   WY_THROW( WyRet() );  }  // Send message CliMsg to the server // size_t n_wr; if((r=cli.write(CliMsg.begin(),CliMsg.size(),n_wr))!=Ok) {   WY_THROW(r); } if(n_wr!=CliMsg.size()) {   WY_THROW( WyRet() );  } return(0);}catch(const WyRet& e) { std::cerr << "process_client_inet6: "  << Wy::wrd(e).c_str() << std::endl; return(-1);}catch(...) { std::cerr << "process_client_inet6 : caught throw" << std::endl; return(-1);};// Test WySockFile communication (AF_INET6)// t_comm_inet6 fork process_client_inet6 and runs as the server// process_client_inet6 will run as the client//static void t_comm_inet6(void){ WyRet r; pid_t child_pid; int child_status; // Create server socket // WySockFile svr; WySockAddrINet6 saddr(TestINetIP6,TestINetPort); if((r=svr.reset(PF_INET6,SOCK_STREAM))!=Ok) {   WY_THROW(r); } if((r=svr.bind(saddr))!=Ok) {   WY_THROW(r); } if((r=svr.listen(1))!=Ok) {   WY_THROW(r); } // stat {   WyFileStat stt;   if((r=svr.stat(stt))!=Ok) {     WY_THROW(r);   }   if(stt.is_sock()==false) {     WY_THROW( WyRet() );   }   std::cout << "stream socket(ipv6), _blksize= " << stt.sf_mode() << '\n'; } // Fork child process // child_pid=::fork(); if(child_pid<0) {   WY_THROW( WyRet(WyReply(errno)) ); } if(child_pid==0) {   ::exit(process_client_inet6()); } // Server accepts connection // WySockFile psock; WySockAddrINet6 paddr; if((r=svr.accept(psock,paddr))!=Ok) {   WY_THROW(r); } if(paddr.is_default()) {   WY_THROW( WyRet() ); } // Dump the accept addr {   WyStr str;   if((r=paddr.ip().notation(str))!=Ok) {     WY_THROW(r);   }  if( Wy::wrd(paddr.ip())!=str) {    WY_THROW( WyRet() );  }   std::cout << "server(PF_INET6) accept() address: " << str.c_str() << std::endl;   if((r=psock.sockname(paddr))!=Ok) {     WY_THROW(r);   }   if((r=paddr.ip().notation(str))!=Ok) {     WY_THROW(r);   }  if( Wy::wrd(paddr.ip())!=str) {    WY_THROW( WyRet() );  }   std::cout << " accepted..sockname()= " << str.c_str() << std::endl;   if((r=psock.peername(paddr))!=Ok) {     WY_THROW(r);   }   if((r=paddr.ip().notation(str))!=Ok) {     WY_THROW(r);   }  if(Wy::wrd(paddr.ip())!=str) {    WY_THROW( WyRet() );  }   std::cout << " accepted..peername()= " << str.c_str() << std::endl; } // Send SvrMsg to the client // size_t n_wr; if((r=psock.write(SvrMsg,n_wr))!=Ok) {   WY_THROW(r); } if(n_wr!=SvrMsg.size()) {   WY_THROW( WyRet() );  } // Receive message from the client (CliMsg expected) // size_t n_rd; char rbuf[64]; if(sizeof(rbuf)<CliMsg.size()+1) {   WY_THROW( WyRet() );   // rbuf not long enough } if((r=psock.read(rbuf,sizeof(rbuf),n_rd))!=Ok) {   WY_THROW(r); } if(n_rd!=CliMsg.size()) {   WY_THROW( WyRet() );  } if(Wy::_strcmp(CliMsg,WyCSeg(rbuf,n_rd))!=0) {   WY_THROW( WyRet() );  }  if(::wait(&child_status)!=child_pid) {   WY_THROW( WyRet(WyReply(errno)) ); } if(WIFEXITED(child_status)==0) {   WY_THROW( WyRet() );  // child process was not terminated voluntarily } if(WEXITSTATUS(child_status)!=0) {   WY_THROW( WyRet() );  // child process not exist(0) }}#endifstatic WyStr chdr("+---------------+\n"                  "| main() caught:|\n"                  "+---------------+\n");int main(void) throw()try { std::cout << "Checking wysockfile.h ...\n"; if(Wy::_strcmp(WySockFile::class_name,"WySockFile")!=0) {   WY_THROW( WyRet() ); } try {   setup_test();   t1();   t2();   t_exception();   t_error();   t_local_mis();   t_comm_local();#ifdef WY__TEST_SOCK_INET__   try {     t_comm_inet4();    }   catch(const WyRet& e) {     std::cerr << "\at_comm_inet4() failed: (Beep: IPv4 may have not configured)\n"               << Wy::wrd(e).c_str() << std::endl;   };   try {     t_comm_inet6();    }   catch(const WyRet& e) {     std::cerr << "\at_comm_inet6() failed: (Beep: IPv6 may have not configured)\n"               << Wy::wrd(e).c_str() << std::endl;   };#endif   close_test(); } catch(...) {   close_test();   throw; } std::cout << "Checked Ok\n"; return(0);}catch(const WyRet& e) { std::cerr << chdr.c_str() << Wy::wrd(e).c_str() << std::endl; return(-1);}catch(const std::exception& e) { std::cerr << chdr.c_str() << "std::exception" << std::endl; return(-1);}catch(...) { std::cerr << chdr.c_str() << "unknown unwind" << std::endl; return(-1);};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -