<?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/5726/</link>
<title><![CDATA[PHP与C(或其它语言)通过消息队列进行通讯，完整代码]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Mon, 05 Nov 2012 05:50:41 +0000</pubDate> 
<guid>http://jackxiang.com/post/5726/</guid> 
<description>
<![CDATA[ 
	通过消息队列确实是一个PHP和C进行通讯的除开共享内存外的一个好方法，如下：<br/>msg.php<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
/*
 * class msg
 * Use for communication between php and php;
 * Create at: 12:08 2012/10/31
 * Author: leixun(lein_urg@163.com)
 * version 1 - 14:01 2012/10/31
 */

class msg&#123;
&nbsp;&nbsp;private $id;
&nbsp;&nbsp;private $msg_id;
&nbsp;&nbsp;private $_serialize = true;

&nbsp;&nbsp;/**
&nbsp;&nbsp; * @param $_id ID
&nbsp;&nbsp; */
&nbsp;&nbsp;public function msg($_id, $_serialize = true)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;if(!function_exists(&#039;msg_get_queue&#039;))
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&#039;msg queue function not installed, Reconfigure PHP with --enable-sysvmsg &lt;br/&gt;&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;id = $_id;
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;msg_id = msg_get_queue ( $_id );
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_serialize = $_serialize;
&nbsp;&nbsp;&nbsp;&nbsp;if ($this-&gt;msg_id === false) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(basename(__FILE__).&#039;-&gt;&#039;.__LINE__.&#039;: Unable to create message quee&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;/**
&nbsp;&nbsp; * @data data to send
&nbsp;&nbsp; * @type message type
&nbsp;&nbsp; */
&nbsp;&nbsp;public function send( $data, $type = 1, $blocking = false )
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;if (!msg_send ($this-&gt;msg_id, $type, $data, $this-&gt;_serialize, $blocking, $msg_err))
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &quot;Msg not sent because $msg_err&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;return true;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;/**
&nbsp;&nbsp; * @param $type message type
&nbsp;&nbsp; * @param $maxsize The maximum size of message to be accepted,
&nbsp;&nbsp; */
&nbsp;&nbsp;public function receive($no_wait = true, $type = 1 , $maxsize = 1024 )
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;$rs = msg_receive ( $this-&gt;msg_id , $type ,&nbsp;&nbsp;$type , $maxsize , $message , $this-&gt;_serialize, $no_wait?MSG_IPC_NOWAIT:NULL , $errorcode);
&nbsp;&nbsp;&nbsp;&nbsp;if($rs)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $message;
&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public function remove()
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;msg_remove_queue($this-&gt;msg_id);
&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&#125;
</textarea><br/><br/><br/>msg_write.php<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
define(&#039;base_path&#039; , dirname(__FILE__));//msg_write.php
include(base_path.&#039;/msg.php&#039;);
$msg = new msg(1, false);
$msg1 = new msg(2, false);
if($argv[1]==&#039;del&#039;) $msg-&gt;remove();
 
 
$str = &#039;There are no user contributed notes for this page.&#039;;
while(1)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data = substr($str,0,rand(18,25));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$msg-&gt;send(rand().$data, rand(1,10));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $data.&quot; -&gt; sent&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;Get:&#039;.$msg1-&gt;receive(false, 0).chr(10);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(3);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//usleep(10000);
&#125;
echo &#039;Done&#039;;
</textarea><br/><br/> msg.c <br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;

#include &lt;stdlib.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/ipc.h&gt;
#include &lt;sys/msg.h&gt;
#define MAX_MSG_LEN 512
static int php_msg = -1;
static int php_msg1 = -1;

static int running = 1;

static void *php_msg_handler_thread(void *arg);
static int msg_send(int msg_id, int fd, char *data);

struct msg_st &#123;
&nbsp;&nbsp;long mtype;
&nbsp;&nbsp;char mtext[MAX_MSG_LEN];
&#125;;

int main(int argc,char **argv)
&#123;
&nbsp;&nbsp;printf(&quot;go 1 &#92;n&quot;);

&nbsp;&nbsp;if((php_msg= msgget((key_t)1,0666&#124;IPC_CREAT)) == -1)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;php_msg create&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;
&nbsp;&nbsp;if((php_msg1= msgget((key_t)2,0666&#124;IPC_CREAT)) == -1)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;php_msg create&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;/////////////////////////////////////////////////////////////////////////////////
&nbsp;&nbsp;pthread_t php_msg_pthread;

&nbsp;&nbsp;int rs = pthread_create(&amp;php_msg_pthread, NULL, (void*(*)(void*))php_msg_handler_thread, (void *)NULL);
&nbsp;&nbsp;if(rs!=0)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;php_msg_pthread create&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;pthread_join(php_msg_pthread, NULL);
&nbsp;&nbsp;return 0;

&#125;

static void *php_msg_handler_thread(void *arg)
&#123;
&nbsp;&nbsp;struct msg_st php_data;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;printf(&quot;sizeof(struct msg_st)=%d&#92;n&quot;,sizeof(struct msg_st));

&nbsp;&nbsp;char* data;
&nbsp;&nbsp;data = malloc(MAX_MSG_LEN);
&nbsp;&nbsp;char *pre = &quot;You told me:&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;while(running)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;//ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); 
&nbsp;&nbsp;&nbsp;&nbsp;if(msgrcv(php_msg,(void *) &amp;php_data, MAX_MSG_LEN, 0 , 0) == -1)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;msgrcv&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(errno==E2BIG)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(msgctl(php_msg,IPC_RMID,0) == -1)&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&quot;msgctl(IPC_RMID) failed &#92;n&quot;);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if(errno == EINVAL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;recevier mssage : %s , type= %d&#92;n&quot;, php_data.mtext, php_data.mtype);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(data, &#039;&#92;0&#039;, MAX_MSG_LEN);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(data, pre, strlen(pre));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(data+strlen(pre), php_data.mtext, strlen(php_data.mtext));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg_send(php_msg1, 2, data);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bzero(php_data.mtext, strlen(php_data.mtext));
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;//break;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;free(data);
&#125;


static int msg_send(int msg_id, int fd, char *data)
&#123;
&nbsp;&nbsp;struct msg_st some_data;

&nbsp;&nbsp;//some_data = malloc( sizeof(struct msg_st) );

&nbsp;&nbsp;memcpy(some_data.mtext, data, strlen(data) + 1);
&nbsp;&nbsp;some_data.mtext[strlen(data)] = &#039;&#92;0&#039;;
&nbsp;&nbsp;some_data.mtype= fd;

&nbsp;&nbsp;printf(&quot;will send %s &#92;n&quot;, &amp;some_data.mtext);
&nbsp;&nbsp;//int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); 
&nbsp;&nbsp;if((msgsnd(msg_id,(void *) &amp; some_data, strlen(data), 0)) == -1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;msgsnd&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;return 1;
&#125;
</textarea><br/><br/><br/>转自：http://blog.csdn.net/leinchu/article/details/8141665
]]>
</description>
</item><item>
<link>http://jackxiang.com/post/5726/#blogcomment63817</link>
<title><![CDATA[[评论] PHP与C(或其它语言)通过消息队列进行通讯，完整代码]]></title> 
<author>fg4fdfg7sd &lt;jnxz852@126.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 08 Nov 2012 02:04:16 +0000</pubDate> 
<guid>http://jackxiang.com/post/5726/#blogcomment63817</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;从小培养孩子过硬的心理素质 情商是指非智力因素，就是人们常说的心理素质。如果一个人性格孤僻、怪异、不易合作；自卑、脆弱，不能面对挫折；急燥、自负，情绪不稳定等等，这都是情商不足的表现，即使他的智商再高也很难有所成就，所以情商是一个人获得成功的关键。情商虽有一定的先天遗传因素，但更重要是后天发展的。容颜那么如何从小培养孩子的情商呢？&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;我公司主营建材行业，升降机,升降平台,升降机,升降平......68
]]>
</description>
</item>
</channel>
</rss>