<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[PHP socket]完整的示例，在Windows下调试通过]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Fri, 30 May 2008 06:50:17 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	server.php:(UTF8编码)<br/>&lt;?php <br/>// server <br/>// 设置错误处理 <br/>error_reporting (e_all); <br/>// 设置运行时间 <br/>set_time_limit (0); <br/>// 起用缓冲 <br/>ob_implicit_flush (); <br/>$ip = &quot;127.0.0.1&quot;; // ip地址 <br/>$port = 8848;&nbsp;&nbsp;// 端口号 <br/>$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);&nbsp;&nbsp;// 创建一个socket <br/>if ($socket) <br/>echo &quot;socket_create() successed!&#92;n&quot;; <br/>else {<br/>echo &quot;socket_create() failed:&quot;.socket_strerror ($socket).&quot;&#92;n&quot;; <br/>exit(0);<br/>}<br/><br/>$bind = socket_bind ($socket, $ip, $port); // 绑定一个socket <br/>if ($bind) {<br/>&nbsp;&nbsp;echo &quot;socket_bind() successed!&#92;n&quot;; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else{ <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;socket_bind() failed:&quot;.socket_strerror ($bind).&quot;&#92;n&quot;; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;}<br/><br/>$listen = socket_listen ($socket);&nbsp;&nbsp;// 间听socket <br/>if ($listen) <br/>echo &quot;socket_listen() successed!&#92;n&quot;; <br/>else <br/>echo &quot;socket_listen() failed:&quot;.socket_strerror ($listen).&quot;&#92;n&quot;; <br/><br/>while (true) <br/>{ <br/>$msg = socket_accept ($socket);&nbsp;&nbsp;// 接受一个socket <br/>if (!$msg) <br/>{ <br/>echo &quot;socket_accept() failed:&quot;.socket_strerror ($msg).&quot;&#92;n&quot;; <br/>break; <br/>} <br/>$welcome0 = &quot;welcome to php server!向东&#92;n&quot;; <br/>$welcome = iconv(&quot;utf-8&quot;,&quot;gbk&quot;,$welcome0);//显示gb的中文,否则在windows cmd 终端显示乱码<br/>socket_write ($msg, $welcome, strlen ($welcome)); <br/>while (true) <br/>{ <br/>$command = strtoupper (trim (socket_read ($msg, 1024))); <br/>if (!$command) <br/>break; <br/>echo $command.&quot;&#92;n&quot;;<br/>switch ($command) <br/>{ <br/>case &quot;HELLO&quot;: <br/>$writer = &quot;hello everybody!&quot;; <br/>break; <br/>case &quot;BYE&quot;: <br/>$writer = &quot;bye-bye&quot;; <br/>break; <br/>case &quot;HELP&quot;: <br/>$writer = &quot;hello&#92;tquit&#92;thelp&quot;; <br/>break; <br/>default: <br/>$writer = &quot;error command!&#92;n&quot;; <br/>} <br/>socket_write ($msg, $writer, strlen ($writer)); <br/>if ($command == &quot;QUIT&quot;) <br/>break; <br/>} <br/>socket_close ($msg); <br/>} <br/>socket_close ($socket);&nbsp;&nbsp;// 关闭socket <br/>?&gt; <br/>client.php(UTF8编码)<br/>&lt;?php<br/>// Client <br/>// 设置错误处理<br/>error_reporting (E_ALL);<br/>// 设置处理时间<br/>set_time_limit (0);<br/><br/>$ip = &quot;127.0.0.1&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // IP 地址<br/>$port = 8848;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 端口号<br/><br/>$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);&nbsp;&nbsp; // 创建一个SOCKET<br/>if ($socket)<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;socket_create() successed!/n&quot;;<br/>else<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;socket_create() failed:&quot;.socket_strerror ($socket).&quot;/n&quot;;<br/><br/>$conn = socket_connect ($socket, $ip, $port);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 建立SOCKET的连接<br/>if ($conn)<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;Success to connection![&quot;.$ip.&quot;:&quot;.$port.&quot;]/n&quot;;<br/>else<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;socket_connect() failed:&quot;.socket_strerror ($conn).&quot;/n&quot;;<br/><br/>echo socket_read ($socket, 1024);&nbsp;&nbsp; <br/><br/>$stdin = fopen (&#039;php://stdin&#039;, &#039;r&#039;);<br/>while (true)<br/>{<br/>&nbsp;&nbsp;&nbsp;&nbsp;$command = trim (fgets ($stdin, 1024));<br/>&nbsp;&nbsp;&nbsp;&nbsp;socket_write ($socket, $command, strlen ($command));<br/>&nbsp;&nbsp;&nbsp;&nbsp;$msg = trim (socket_read ($socket, 1024));<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo $msg.&quot;&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ($msg == &quot;Bye-Bye&quot;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br/>}<br/>fclose ($stdin);<br/>socket_close ($socket);<br/>?&gt; <br/><br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [PHP socket]完整的示例，在Windows下调试通过]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>