Home  >  Article  >  Backend Development  >  Sample_PHP tutorial on using fork and socket on php

Sample_PHP tutorial on using fork and socket on php

WBOY
WBOYOriginal
2016-07-13 17:36:03903browse

最近剛好遇到一個頭大的問題寫了這個code讓大家參考一下吧
家裏的無線AP功能不太好,他只提供把外部真實IP map 到 Nat裡面的某個IP
不能指定某個port map到某個內部IP的Port
可是我已經把外部的IP Map到內部的Linux);" href="javascript:;" target=_self>Linux Server上,
但是我又想從外部使用VNC連到內部的一台Windows電腦。
所以就寫了這個程式
原理是這樣

這個程式會在Linux Server上開一個Port作Listen的動作
當外部連到這個Port時,程式會再開啟另一個連線連到內部Windows的VNC上
把外部的封包原封不動的丟到VNC的連線上,然後把VNC連線傳回的資料原封不動的再丟回外部的Port

程式碼:
<br><font face="NSimsun">#!/usr/bin/php -q<br></font><font color="#000000"><font face="NSimsun"><font color="#0000bb"><?php<BR> $IP</FONT><FONT color=#007700>=</FONT><FONT color=#dd0000>192.168.1.1</FONT><FONT color=#007700>;</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>//Windows電腦的IP<BR> </FONT><FONT color=#0000bb>$Port</FONT><FONT color=#007700>=</FONT><FONT color=#dd0000>5900</FONT><FONT color=#007700>;        </FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>//VNC使用的Port<BR> </FONT><FONT color=#0000bb>$ServerPort</FONT><FONT color=#007700>=</FONT><FONT color=#dd0000>9999</FONT><FONT color=#007700>;</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>//Linux Server對外使用的Port<BR> </FONT><FONT color=#0000bb>$RemoteSocket</FONT><FONT color=#007700>=</FONT><FONT color=#0000bb>false</FONT><FONT color=#007700>;</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>//連線到VNC的Socket<BR> </FONT><FONT color=#007700>function</FONT><FONT color=#0000bb>SignalFunction</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;$Signal&#41;<BR> </FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#123;<BR>   //這是主Process的訊息處理函數<BR>  </FONT><FONT color=#007700>global</FONT><FONT color=#0000bb>$PID</FONT><FONT color=#007700>;</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>//Child Process的PID<BR>  </FONT><FONT color=#007700>switch &</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;$Signal&#41;<BR>  </FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#123;<BR>   </FONT><FONT color=#007700>case</FONT><FONT color=#0000bb>SIGTRAP</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#58;<BR>   </FONT><FONT color=#007700>case</FONT><FONT color=#0000bb>SIGTERM</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#58;<BR>    //收到結束程式的Signal<BR>    </FONT><FONT color=#007700>if&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;$PID&#41;<BR>    </FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#123;<BR>     //送一個SIGTERM的訊號給Child告訴他趕快結束掉嘍<BR>     </FONT><FONT color=#0000bb>posix_kill</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;$PID,SIGTERM&#41;;<BR>     //等待Child Process結束,避免zombie<BR>     </FONT><FONT color=#0000bb>pcntl_wait</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;$Status&#41;;<BR>    </FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#125;<BR>    //關閉主Process開啟的Socket<BR>    </FONT><FONT color=#0000bb>DestroySocket</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;&#41;;<BR>    </FONT><FONT color=#007700>exit&</FONT></FONT><FONT face=NSimsun color=#ff8000>#40;0&#41;; //結束主Process<BR>    </FONT><FONT face=NSimsun><FONT color=#007700>break;<BR>   case</FONT><FONT color=#0000bb>SIGCHLD</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#58;<BR>    /*<BR>當Child Process結束掉時,Child會送一個SIGCHLD訊號給Parrent<BR>當Parrent收到SIGCHLD,就知道Child Process已經結束嘍 ,該做一些<BR>結束的動作*/<BR>    </FONT><FONT color=#007700>unset&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;$PID&#41;; //將$PID清空,表示Child Process已經結束<BR>    </FONT><FONT color=#0000bb>pcntl_wait</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun color=#ff8000>#40;$Status&#41;; //避免Zombie<BR>    </FONT><FONT face=NSimsun color=#007700>break;<BR>   default&</FONT><FONT face=NSimsun><FONT color=#ff8000>#58;<BR>  </FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#125;<BR> </FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#125;<BR> </FONT><FONT color=#007700>function</FONT><FONT color=#0000bb>ChildSignalFunction</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;$Signal&#41;<BR> </FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#123;<BR>//This is the message processing function of Child Process <BR> </FONT><FONT color=#007700>switch &</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;$Signal&#41;<BR> </FONT><FONT color=#007700> &</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#123;<BR> </FONT><FONT color=#007700>case</FONT><FONT color=#0000bb>SIGTRAP</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#58;<BR> </FONT><FONT color=#007700>case</FONT><FONT color=#0000bb>SIGTERM</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#58;<BR>//Child Process received the end message<BR> </FONT><FONT color=#0000bb>DestroySocket</FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;&#41;; //Close Socket<BR> </FONT><FONT color=#007700>exit&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#40;0&#41;; //End Child Process<BR> </FONT><FONT color=#007700>default&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#58;<BR> </FONT><FONT color=#007700>&</FONT></FONT><FONT face=NSimsun><FONT color=#ff8000>#125;<BR> ></FONT>&#125;<FONT color=#007700> ;</FONT> </FONT><FONT face=NSimsun>&<FONT color=#ff8000><BR></FONT><FONT color=#007700>#123;</FONT> //Child Process Socket processing function<FONT color=#0000bb> //$ConnectedServerSocket -> Externally connected Socket</font> <font color="#007700"></font>global</font><font face="NSimsun">$ServerSocket<font color="#ff8000"><br>,</font><font color="#007700">$RemoteSocket</font></font>,<font face="NSimsun"><font color="#ff8000">$IP<br><br>,<br></font>$Port<font color="#007700"></font><font color="#0000bb"></font>;<font color="#007700"> >$ConnectedServerSocket</font><font color="#0000bb"></font>;<font color="#007700"> declare </font>
<font color="#0000bb"></font><font color="#007700">http://www.bkjia.com/PHPjc/508267.html</font><font color="#0000bb">www.bkjia.com</font></font>true<font face="NSimsun"><font color="#007700">http: //www.bkjia.com/PHPjc/508267.html<br></font>TechArticle<font color="#0000bb"></font>I just encountered a big problem recently and wrote this code for your reference. Does the wireless AP function at home work? Great, it only provides mapping of the external real IP to a certain IP in Nat. It cannot refer to...<font color="#007700"></font><font color="#0000bb"></font>
</font><font face="NSimsun" color="#007700"></font></font>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn