| 订阅 | 在线投稿
分享
 
 
 

在php上使用fork以及socket的sample

2008-12-19 08:07:38 编辑來源:互联网 国际版 评论
 
 
本文为【在php上使用fork以及socket的sample】的汉字拼音对照版显示拼音
  zuijinganghaoyudaoyigetoudadewendixieleyangecoderangdajiacankaoyixiaba

  jialidemoxianAPgongnengbutaihaotazhitigongbawaibuzhenshiIP map dao NatlimiandemougeIP

  bunengzhidingmougeport mapdaomougenabuIPdePort

  keshiwoyijingbawaibudeIP MapdaonabudeLinux Servershang

  danshiwoyouxiangcongwaibu使shiyongVNCliandaonabudeyitaiWindowsdiannao

  suoyijiuxieleyangechengshi

  yuanlishiyanyang

  yangechengshihuizaiLinux ServershangkaiyigePortzuoListendedongzuo

  dangwaibuliandaoyangePortshichengshihuizaikaiqilingyigelianxianliandaonabuWindowsdeVNCshang

  bawaibudefengbaoyuanfengbudongdediudaoVNCdelianxianshangranhoubaVNClianxianchuanhuideziliaoyuanfengbudongdezaidiuhuiwaibudePort

  chengshima:

  #!/usr/bin/php -q

  <?php

  $IP='192.168.1.1';//WindowsdiannaodeIP

  $Port='5900'; //VNC使shiyongdePort

  $ServerPort='9999';//Linux Serverduiwai使shiyongdePort

  $RemoteSocket=false;//lianxiandaoVNCdeSocket

  functionSignalFunction($Signal)

  {

   //yanshizhuProcessdexunxichulihanshu

   global$PID;//Child ProcessdePID

   switch ($Signal)

   {

   caseSIGTRAP:

   caseSIGTERM:

   //shoudaojishuchengshideSignal

   if($PID)

   {

   //songyigeSIGTERMdexunhaojiChildgaosutagankuaijishudiaolou

   posix_kill($PID,SIGTERM);

   //dengdaiChild Processjishubimianzombie

   pcntl_wait($Status);

   }

   //guanbizhuProcesskaiqideSocket

   DestroySocket();

   exit(0); //jishuzhuProcess

  

  break;

   caseSIGCHLD:

   /*

  dangChild ProcessjishudiaoshiChildhuisongyigeSIGCHLDxunhaojiParrent

  dangParrentshoudaoSIGCHLDjiuzhidaoChild Processyijingjishulou gaizuoyixie

  jishudedongzuo*/

   unset($PID); //jiang$PIDqingkongbiaoshiChild Processyijingjishu

   pcntl_wait($Status); //bimianZombie

  

  break;

   default:

   }

  }

  functionChildSignalFunction($Signal)

  {

  //yanshiChild Processdexunxichulihanshu

   switch ($Signal)

   {

   caseSIGTRAP:

   caseSIGTERM:

  //Child Processshoudaojishudexunxi

   DestroySocket(); //guanbiSocket

   exit(0); //jishuChild Process

   default:

   }

  }

  functionProcessSocket($ConnectedServerSocket)

  {

   //Child Process Socketchulihanshu

   //$ConnectedServerSocket -> waibulianjinlaideSocket

   global$ServerSocket,$RemoteSocket,$IP,$Port;

   $ServerSocket=$ConnectedServerSocket;

   declare(ticks = 1); //yanyixingyidingyaojiaburanmeibanfashedingxunxichulihanshu

  //shedingxunxichulihanshu

   if(!pcntl_signal(SIGTERM, "ChildSignalFunction")) return;

   if(!pcntl_signal(SIGTRAP, "ChildSignalFunction")) return;

  //jianliyigelianxiandaoVNCdeSocket

   $RemoteSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);

  //lianxiandaonabudeVNC

   @$RemoteConnected=socket_connect($RemoteSocket,$IP,$Port);

   if(!$RemoteConnected) return; //mofalianxiandaoVNC jishu

  //jiangSocketdechulishehaNonblockbimianchengshibeiBlockzhu

   if(!socket_set_nonblock($RemoteSocket)) return;

   if(!socket_set_nonblock($ServerSocket)) return;

   while(true)

   {

  //yanbianwomencaiyongpoolingdefangshiququdeziliao

   $NoRecvData=false; //yangebianshuyonglaipanbiewaibudelianxianshifouyoudoudaoziliao

   $NoRemoteRecvData=false;//yangebianshuyonglaipanbieVNClianxianshifouyoudoudaoziliao

   @$RecvData=socket_read($ServerSocket,4096,PHP_BINARY_READ);

  //congwaibulianxiandouqu4096 bytesdeziliao

   @$RemoteRecvData=socket_read($RemoteSocket,4096,PHP_BINARY_READ);

  //congvnclianxianlianxiandouqu4096 bytesdeziliao

   if($RemoteRecvData==='')

   {

  //VNClianxianzhongduangaijishulou

   echo"Remote Connection Close\n";

   return;

   }

   if($RemoteRecvData===false)

   {

  /*

  youyuwomenshicaiyongnonblobkmoshi

  yanlideqingkuangjiushivnclianxianmeiyoukegongdouqudeziliao

  */

   $NoRemoteRecvData=true;

  //qingchudiaoLast Errror

   socket_clear_error($RemoteSocket);

   }

   if($RecvData==='')

   {

  //waibulianxianzhongduangaijishulou

   echo"Client Connection Close\n";

   return;

   }

   if($RecvData===false)

   {

  /*

  youyuwomenshicaiyongnonblobkmoshi

  yanlideqingkuangjiushiwaibulianxianmeiyoukegongdouqudeziliao

  */

   $NoRecvData=true;

  //qingchudiaoLast Errror

   socket_clear_error($ServerSocket);

   }

   if($NoRecvData&&$NoRemoteRecvData)

   {

  //ruguowaibulianxianyijiVNClianxiandoumeiyouziliaokeyidouqushi

  //jiurangchengshishuige0.1miaobimianchangqichanyongCPUziyuan

   usleep(100000);

  //shuixinghoujixuzuopoolingdedongzuodouqusocket

   continue;

   }

   //Recv Data

   if(!$NoRecvData)

   {

  //waibulianxiandouqudaoziliao

   while(true)

   {

  //bawaibulianxiandoudaodeziliaozhuansongdaoVNClianxianshang

   @$WriteLen=socket_write($RemoteSocket,$RecvData);

   if($WriteLen===false)

   {

  //youyuwangluchuanshudewendimuqianzanshimofaxieruziliao

  //xianshuige0.1miaozaijixuchangshi

   usleep(100000);

   continue;

   }

   if($WriteLen===0)

   {

  //yuanduanlianxianzhongduanchengshigaijishule

   echo"Remote Write Connection Close\n";

   return;

   }

  //congwaibulianxiandouqudeziliaoyijingwanquansongjiVNClianxianshizhongduanyangehuiquan

   if($WriteLen==strlen($RecvData)) break;

  //ruguoziliaoyicisongbuwanjiudechaichenghaojicichuansongzhidaosuoyoudeziliaoquanbusongchuhazhi

   $RecvData=substr($RecvData,$WriteLen);

   }

   }

   if(!$NoRemoteRecvData)

   {

  //yanbianshicongVNClianxiandouqudaodeziliaozaizhuansonghuiwaibudelianxian

  //yuanligenshangmianchabuduobuzaizhuishu

   while(true)

   {

   @$WriteLen=socket_write($ServerSocket,$RemoteRecvData);

   if($WriteLen===false)

   {

   usleep(100000);

   continue;

   }

   if($WriteLen===0)

   {

   echo"Remote Write Connection Close\n";

   return;

   }

   if($WriteLen==strlen($RemoteRecvData)) break;

   $RemoteRecvData=substr($RemoteRecvData,$WriteLen);

   }

   }

   }

  }

  functionDestroySocket()

  {

  //yonglaiguanbiyijingkaiqideSocket

   global$ServerSocket,$RemoteSocket;

   if($RemoteSocket)

   {

  //ruguoyijingkaiqiVNClianxian

  //zaiClose SocketqianbixujiangSocket shutdownburanduifangbuzhidaoniyijingguanbilianxianle

   @socket_shutdown($RemoteSocket,2);

   socket_clear_error($RemoteSocket);

  //guanbiSocket

   socket_close($RemoteSocket);

   }

  //guanbiwaibudelianxian

   @socket_shutdown($ServerSocket,2);

   socket_clear_error($ServerSocket);

   socket_close($ServerSocket);

  }

  //yanlishizhenggechengshidekaitouchengshicongyanbiankaishizhixing

  //yanlishouxianzhixingyicifork

  $PID=pcntl_fork();

  if($PID==-1) die("could not fork");

  //ruguo$PIDbuha0biaoshiyanshiParrent Process

  //$PIDjiushiChild Process

  //yanshiParrent Process zijijishudiaorangChildchenghayigeDaemon

  if($PID) die("Daemon PID:$PID\n");

  //congyanbiankaishijiushiDaemonmoshizaizhixingle

  //jiangmuqiandeProcessgenzhongduanjituichichenghadaemonmoshi

  if(!posix_setsid()) die("could not detach from terminal\n");

  //shedingdaemon dexunxichulihanshu

  declare(ticks = 1);

  if(!pcntl_signal(SIGTERM, "SignalFunction")) die("Error!!!\n");

  if(!pcntl_signal(SIGTRAP, "SignalFunction")) die("Error!!!\n");

  if(!pcntl_signal(SIGCHLD, "SignalFunction")) die("Error!!!\n");

  //jianliwaibulianxiandeSocket

  $ServerSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);

  //shedingwaibulianxianjiantingdeIPyijiPortIPlanweishe0biaoshijingtingsuoyoujiemiandeIP

  if(!socket_bind($ServerSocket,0,$ServerPort)) die("Cannot Bind Socket!\n");

  //kaishijiantingPort

  if(!socket_listen($ServerSocket)) die("Cannot Listen!\n");

  //jiangSocketshehanonblockmoshi

  if(!socket_set_nonblock($ServerSocket)) die("Cannot Set Server Socket to Block!\n");

  //qingkong$PIDbianshubiaoshimuqianmeiyourenhedeChild Process

  unset($PID);

  while(true)

  {

  //jinrupoolingmoshimeige1miaozhongjiuqujianchayoumeiyoulianxianjinlai

   sleep(1);

  //jianchayoumeiyoulianxianjinlai

   @$ConnectedServerSocket=socket_accept($ServerSocket);

   if($ConnectedServerSocket!==false)

   {

  //yourenlianjinlailou

  //qishiyigeChild Processyonglaichulilianxian

   $PID=pcntl_fork();

   if($PID==-1) die("could not fork");

   if($PID) continue;//yanshidaemon processjixuhuiqujianting

   //yanlishiChild Processkaishi

   //zhixingSocketlihanshu

   ProcessSocket($ConnectedServerSocket);

   //chuliwanSockethoujishudiaoSocket

   DestroySocket();

   //jishuChild Process

   exit(0);

   }

  }

  ?>原文
 
 
最近剛好遇到一個頭大的問題寫了這個code讓大家參考一下吧 家裏的無線AP功能不太好,他只提供把外部真實IP map 到 Nat裡面的某個IP 不能指定某個port map到某個內部IP的Port 可是我已經把外部的IP Map到內部的[url=http://www.knowsky.com/javascript:;]Linux[/url] Server上, 但是我又想從外部使用VNC連到內部的一台[url=http://www.knowsky.com/javascript:;]Windows[/url]電腦。 所以就寫了這個程式 原理是這樣 這個程式會在Linux Server上開一個Port作Listen的動作 當外部連到這個Port時,程式會再開啟另一個連線連到內部Windows的VNC上 把外部的封包原封不動的丟到VNC的連線上,然後把VNC連線傳回的資料原封不動的再丟回外部的Port 程式碼: #!/usr/bin/php -q <?php $IP='192.168.1.1';//Windows電腦的IP $Port='5900'; //VNC使用的Port $ServerPort='9999';//Linux Server對外使用的Port $RemoteSocket=false;//連線到VNC的Socket functionSignalFunction&#40;$Signal&#41; &#123; //這是主Process的訊息處理函數 global$PID;//Child Process的PID switch &#40;$Signal&#41; &#123; caseSIGTRAP&#58; caseSIGTERM&#58; //收到結束程式的Signal if&#40;$PID&#41; &#123; //送一個SIGTERM的訊號給Child告訴他趕快結束掉嘍 posix_kill&#40;$PID,SIGTERM&#41;; //等待Child Process結束,避免zombie pcntl_wait&#40;$Status&#41;; &#125; //關閉主Process開啟的Socket DestroySocket&#40;&#41;; exit&#40;0&#41;; //結束主Process break; caseSIGCHLD&#58; /* 當Child Process結束掉時,Child會送一個SIGCHLD訊號給Parrent 當Parrent收到SIGCHLD,就知道Child Process已經結束嘍 ,該做一些 結束的動作*/ unset&#40;$PID&#41;; //將$PID清空,表示Child Process已經結束 pcntl_wait&#40;$Status&#41;; //避免Zombie break; default&#58; &#125; &#125; functionChildSignalFunction&#40;$Signal&#41; &#123; //這是Child Process的訊息處理函數 switch &#40;$Signal&#41; &#123; caseSIGTRAP&#58; caseSIGTERM&#58; //Child Process收到結束的訊息 DestroySocket&#40;&#41;; //關閉Socket exit&#40;0&#41;; //結束Child Process default&#58; &#125; &#125; functionProcessSocket&#40;$ConnectedServerSocket&#41; &#123; //Child Process Socket處理函數 //$ConnectedServerSocket -> 外部連進來的Socket global$ServerSocket,$RemoteSocket,$IP,$Port; $ServerSocket=$ConnectedServerSocket; declare&#40;ticks = 1&#41;; //這一行一定要加,不然沒辦法設定訊息處理函數。 //設定訊息處理函數 if&#40;!pcntl_signal&#40;SIGTERM, "ChildSignalFunction"&#41;&#41; return; if&#40;!pcntl_signal&#40;SIGTRAP, "ChildSignalFunction"&#41;&#41; return; //建立一個連線到VNC的Socket $RemoteSocket=socket_create&#40;AF_INET, SOCK_STREAM,SOL_TCP&#41;; //連線到內部的VNC @$RemoteConnected=socket_connect&#40;$RemoteSocket,$IP,$Port&#41;; if&#40;!$RemoteConnected&#41; return; //無法連線到VNC 結束 //將Socket的處理設為Nonblock,避免程式被Block住 if&#40;!socket_set_nonblock&#40;$RemoteSocket&#41;&#41; return; if&#40;!socket_set_nonblock&#40;$ServerSocket&#41;&#41; return; while&#40;true&#41; &#123; //這邊我們採用pooling的方式去取得資料 $NoRecvData=false; //這個變數用來判別外部的連線是否有讀到資料 $NoRemoteRecvData=false;//這個變數用來判別VNC連線是否有讀到資料 @$RecvData=socket_read&#40;$ServerSocket,4096,PHP_BINARY_READ&#41;; //從外部連線讀取4096 bytes的資料 @$RemoteRecvData=socket_read&#40;$RemoteSocket,4096,PHP_BINARY_READ&#41;; //從vnc連線連線讀取4096 bytes的資料 if&#40;$RemoteRecvData===''&#41; &#123; //VNC連線中斷,該結束嘍 echo"Remote Connection Close\n"; return; &#125; if&#40;$RemoteRecvData===false&#41; &#123; /* 由於我們是採用nonblobk模式 這裡的情況就是vnc連線沒有可供讀取的資料 */ $NoRemoteRecvData=true; //清除掉Last Errror socket_clear_error&#40;$RemoteSocket&#41;; &#125; if&#40;$RecvData===''&#41; &#123; //外部連線中斷,該結束嘍 echo"Client Connection Close\n"; return; &#125; if&#40;$RecvData===false&#41; &#123; /* 由於我們是採用nonblobk模式 這裡的情況就是外部連線沒有可供讀取的資料 */ $NoRecvData=true; //清除掉Last Errror socket_clear_error&#40;$ServerSocket&#41;; &#125; if&#40;$NoRecvData&&$NoRemoteRecvData&#41; &#123; //如果外部連線以及VNC連線都沒有資料可以讀取時, //就讓程式睡個0.1秒,避免長期佔用CPU資源 usleep&#40;100000&#41;; //睡醒後,繼續作pooling的動作讀取socket continue; &#125; //Recv Data if&#40;!$NoRecvData&#41; &#123; //外部連線讀取到資料 while&#40;true&#41; &#123; //把外部連線讀到的資料,轉送到VNC連線上 @$WriteLen=socket_write&#40;$RemoteSocket,$RecvData&#41;; if&#40;$WriteLen===false&#41; &#123; //由於網路傳輸的問題,目前暫時無法寫入資料 //先睡個0.1秒再繼續嘗試。 usleep&#40;100000&#41;; continue; &#125; if&#40;$WriteLen===0&#41; &#123; //遠端連線中斷,程式該結束了 echo"Remote Write Connection Close\n"; return; &#125; //從外部連線讀取的資料,已經完全送給VNC連線時,中斷這個迴圈。 if&#40;$WriteLen==strlen&#40;$RecvData&#41;&#41; break; //如果資料一次送不完就得拆成好幾次傳送,直到所有的資料全部送出為止 $RecvData=substr&#40;$RecvData,$WriteLen&#41;; &#125; &#125; if&#40;!$NoRemoteRecvData&#41; &#123; //這邊是從VNC連線讀取到的資料,再轉送回外部的連線 //原理跟上面差不多不再贅述 while&#40;true&#41; &#123; @$WriteLen=socket_write&#40;$ServerSocket,$RemoteRecvData&#41;; if&#40;$WriteLen===false&#41; &#123; usleep&#40;100000&#41;; continue; &#125; if&#40;$WriteLen===0&#41; &#123; echo"Remote Write Connection Close\n"; return; &#125; if&#40;$WriteLen==strlen&#40;$RemoteRecvData&#41;&#41; break; $RemoteRecvData=substr&#40;$RemoteRecvData,$WriteLen&#41;; &#125; &#125; &#125; &#125; functionDestroySocket&#40;&#41; &#123; //用來關閉已經開啟的Socket global$ServerSocket,$RemoteSocket; if&#40;$RemoteSocket&#41; &#123; //如果已經開啟VNC連線 //在Close Socket前必須將Socket shutdown不然對方不知到你已經關閉連線了 @socket_shutdown&#40;$RemoteSocket,2&#41;; socket_clear_error&#40;$RemoteSocket&#41;; //關閉Socket socket_close&#40;$RemoteSocket&#41;; &#125; //關閉外部的連線 @socket_shutdown&#40;$ServerSocket,2&#41;; socket_clear_error&#40;$ServerSocket&#41;; socket_close&#40;$ServerSocket&#41;; &#125; //這裡是整個程式的開頭,程式從這邊開始執行 //這裡首先執行一次fork $PID=pcntl_fork&#40;&#41;; if&#40;$PID==-1&#41; die&#40;"could not fork"&#41;; //如果$PID不為0表示這是Parrent Process //$PID就是Child Process //這是Parrent Process 自己結束掉,讓Child成為一個Daemon。 if&#40;$PID&#41; die&#40;"Daemon PID&#58;$PID\n"&#41;; //從這邊開始,就是Daemon模式在執行了 //將目前的Process跟終端機脫離成為daemon模式 if&#40;!posix_setsid&#40;&#41;&#41; die&#40;"could not detach from terminal\n"&#41;; //設定daemon 的訊息處理函數 declare&#40;ticks = 1&#41;; if&#40;!pcntl_signal&#40;SIGTERM, "SignalFunction"&#41;&#41; die&#40;"Error!!!\n"&#41;; if&#40;!pcntl_signal&#40;SIGTRAP, "SignalFunction"&#41;&#41; die&#40;"Error!!!\n"&#41;; if&#40;!pcntl_signal&#40;SIGCHLD, "SignalFunction"&#41;&#41; die&#40;"Error!!!\n"&#41;; //建立外部連線的Socket $ServerSocket=socket_create&#40;AF_INET, SOCK_STREAM,SOL_TCP&#41;; //設定外部連線監聽的IP以及Port,IP欄位設0,表示經聽所有介面的IP if&#40;!socket_bind&#40;$ServerSocket,0,$ServerPort&#41;&#41; die&#40;"Cannot Bind Socket!\n"&#41;; //開始監聽Port if&#40;!socket_listen&#40;$ServerSocket&#41;&#41; die&#40;"Cannot Listen!\n"&#41;; //將Socket設為nonblock模式 if&#40;!socket_set_nonblock&#40;$ServerSocket&#41;&#41; die&#40;"Cannot Set Server Socket to Block!\n"&#41;; //清空$PID變數,表示目前沒有任何的Child Process unset&#40;$PID&#41;; while&#40;true&#41; &#123; //進入pooling模式,每隔1秒鐘就去檢查有沒有連線進來。 sleep&#40;1&#41;; //檢查有沒有連線進來 @$ConnectedServerSocket=socket_accept&#40;$ServerSocket&#41;; if&#40;$ConnectedServerSocket!==false&#41; &#123; //有人連進來嘍 //起始一個Child Process用來處理連線 $PID=pcntl_fork&#40;&#41;; if&#40;$PID==-1&#41; die&#40;"could not fork"&#41;; if&#40;$PID&#41; continue;//這是daemon process,繼續回去監聽。 //這裡是Child Process開始 //執行Socket裡函數 ProcessSocket&#40;$ConnectedServerSocket&#41;; //處理完Socket後,結束掉Socket DestroySocket&#40;&#41;; //結束Child Process exit&#40;0&#41;; &#125; &#125; ?>
󰈣󰈤
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

网络安全治理:国家安全保障的主要方向是打击犯罪,而不是处置和惩罚受害者

来源:中国青年报 新的攻击方法不断涌现,黑客几乎永远占据网络攻击的上风,我们不可能通过技术手段杜绝网络攻击。国家安全保障的主要方向是打击犯罪,而不是处置和惩罚...

 
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
清淡自然的清纯模特
倩女幽魂_美丽女生
[i]黄山
雪棉
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有