| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> php >> PHP Socket bian cheng qi bu

PHP Socket bian cheng qi bu

2008-12-19 08:07:46 编辑來源:互联网 评论
 
 
 
本文为【PHP Socket编程起步】的拼音翻译版
  rang wo men yi yi ge jian dan de li zi kai shi --- yi ge jie shou shu ru zi fu chuan , chu li bing fan hui zhe ge zi fu chuan dao ke hu duan de TCP fu wu . xia mian shi xiang ying de dai ma :
  PHP dai ma :
  <?
  // she zhi yi xie ji ben de bian liang
  $host="192.168.1.99";
  $port=1234;
  // she zhi chao shi shi jian
  set_time_limit(0);
  // chuang jian yi ge Socket
  $socket=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create
  socket\n");
  // bang ding Socket dao duan kou
  $result=socket_bind($socket,$host,$port) or die("Could not bind to
  socket\n");
  // kai shi jian ting lian jie
  $result=socket_listen($socket,3) or die("Could not set up socket
  listener\n");
  // accept incoming connections
  // ling yi ge Socket lai chu li tong xin
  $spawn=socket_accept($socket) or die("Could not accept incoming
  connection\n");
  // huo de ke hu duan de shu ru
  $input=socket_read($spawn,1024) or die("Could not read input\n");
  // qing kong shu ru zi fu chuan
  $input=trim($input);
  // chu li ke hu duan shu ru bing fan hui jie guo
  $output=strrev($input) ."\n";
  socket_write($spawn,$output,strlen($output)) or die("Could not write
  output\n");
  // guan bi sockets
  socket_close($spawn);
  socket_close($socket);
  ?>
  xia mian shi qi mei yi bu zhou de xiang xi shuo ming :
  1. di yi bu shi jian li liang ge bian liang lai bao cun Socket yun xing de fu wu qi de IP di zhi he duan kou . ni ke yi she zhi wei ni zi ji de fu wu qi he duan kou ( zhe ge duan kou ke yi shi 1 dao 65535 zhi jian de shu zi ), qian ti shi zhe ge duan kou wei bei shi yong .
  PHP dai ma :
  <?
  // she zhi liang ge bian liang
  $host="192.168.1.99";
  $port=1234;
  ?>
  2. zai fu wu qi duan ke yi shi yong set_time_out() han shu lai que bao PHP zai deng dai ke hu duan lian jie shi bu hui chao shi .
  PHP dai ma :
  <?
  // chao shi shi jian
  set_time_limit(0);
  ?>
  3. zai qian mian de ji chu shang , xian zai gai shi yong socket_creat() han shu chuang jian yi ge Socket le --- zhe ge han shu fan hui yi ge Socket ju bing , zhe ge ju bing jiang yong zai yi hou suo you de han shu zhong .
  PHP dai ma :
  <?
  // chuang jian Socket
  $socket=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket\n");
  ?>
  di yi ge shen shu "AF_INET" yong lai zhi ding yu ming ;
  di er ge shen shu "SOCK_STREM" gao su han shu jiang chuang jian yi ge shen me lei xing de Socket( zai zhe ge li zi zhong shi TCP lei xing )
  yin ci , ru guo ni xiang chuang jian yi ge UDP Socket de hua , ni ke yi shi yong ru xia de dai ma :
  PHP dai ma :
  <?
  // chuang jian socket
  $socket=socket_create(AF_INET,SOCK_DGRAM,0) or die("Could not create socket\n");
  ?>
  4. yi dan chuang jian le yi ge Socket ju bing , xia yi bu jiu shi zhi ding huo zhe bang ding ta dao zhi ding de di zhi he duan kou . zhe ke yi tong guo socket_bind() han shu lai wan cheng .
  PHP dai ma :
  <?
  // bang ding socket to zhi ding di zhi he duan kou
  $result=socket_bind($socket,$host,$port) or die("Could not bind to socket\n");
  ?>
  5. dang Socket bei chuang jian hao bing bang ding dao yi ge duan kou hou , jiu ke yi kai shi jian ting wai bu de lian jie le .PHP yuan xu ni you socket_listen() han shu lai kai shi yi ge jian ting , tong shi ni ke yi zhi ding yi ge shu zi ( zai zhe ge li zi zhong jiu shi di er ge shen shu :3)
  PHP dai ma :
  <?
  // kai shi jian ting lian jie
  $result=socket_listen($socket,3) or die("Could not set up socket listener\n");
  ?>
  6. dao xian zai , ni de fu wu qi chu le deng dai lai zi ke hu duan de lian jie qing qiu wai ji ben shang shen me ye mei you zuo . yi dan yi ge ke hu duan de lian jie bei shou dao ,socket_assept() han shu bian kai shi qi zuo yong le , ta jie shou lian jie qing qiu bing tiao yong ling yi ge zi Socket lai chu li ke hu duan -- fu wu qi jian de xin xi .
  PHP dai ma :
  <?
  // jie shou qing qiu lian jie
  // tiao yong zi socket chu li xin xi
  $spawn=socket_accept($socket) or die("Could not accept incoming connection\n");
  ?>
  zhe ge zi socket xian zai jiu ke yi bei sui hou de ke hu duan -- fu wu qi tong xin suo yong le .
  7. dang yi ge lian jie bei jian li hou , fu wu qi jiu hui deng dai ke hu duan fa song yi xie shu ru xin xi , zhe xie xin xi ke yi you socket_read() han shu lai huo de , bing ba ta fu zhi gei PHP de $input bian liang .
  PHP dai ma :
  <?
  // du qu ke hu duan shu ru
  $input=socket_read($spawn,1024) or die("Could not read input\n");
  ?>
  socker_read de di er ge shen shu yong yi zhi ding du ru de zi jie shu , ni ke yi tong guo ta lai xian zhi cong ke hu duan huo qu shu ju de da xiao .
  zhu yi :socket_read han shu hui yi zhi du qu ke hu duan shu ju , zhi dao yu jian \n,\t huo zhe \0 zi fu .PHP jiao ben ba zhe xie zi fu kan zuo shi shu ru de jie shu fu .
  8. xian zai fu wu qi bi xu chu li zhe xie you ke hu duan fa lai shi shu ju ( zai zhe ge li zi zhong de chu li jin jin bao han shu ju de shu ru he hui chuan dao ke hu duan ). zhe bu fen ke yi you socket_write() han shu lai wan cheng ( shi de you tong xin socket fa hui yi ge shu ju liu dao ke hu duan cheng wei ke neng )
  PHP dai ma :
  <?
  // chu li ke hu duan shu ru bing fan hui shu ju
  $output=strrev($input) ."\n";
  socket_write($spawn,$output,strlen($output)) or die("Could not write output\n");
  ?>
  9. yi dan shu chu bei fan hui dao ke hu duan , fu / zi socket dou ying tong guo socket_close() han shu lai zhong zhi
  PHP dai ma :
  <?
  // guan bi sockets
  socket_close($spawn);
  socket_close($socket);
  ?>【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

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

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

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

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
rang wo men yi yi ge jian dan de li zi kai shi --- yi ge jie shou shu ru zi fu chuan , chu li bing fan hui zhe ge zi fu chuan dao ke hu duan de TCP fu wu . xia mian shi xiang ying de dai ma : PHP dai ma : <? // she zhi yi xie ji ben de bian liang $host="192.168.1.99"; $port=1234; // she zhi chao shi shi jian set_time_limit(0); // chuang jian yi ge Socket $socket=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket\n"); // bang ding Socket dao duan kou $result=socket_bind($socket,$host,$port) or die("Could not bind to socket\n"); // kai shi jian ting lian jie $result=socket_listen($socket,3) or die("Could not set up socket listener\n"); // accept incoming connections // ling yi ge Socket lai chu li tong xin $spawn=socket_accept($socket) or die("Could not accept incoming connection\n"); // huo de ke hu duan de shu ru $input=socket_read($spawn,1024) or die("Could not read input\n"); // qing kong shu ru zi fu chuan $input=trim($input); // chu li ke hu duan shu ru bing fan hui jie guo $output=strrev($input) ."\n"; socket_write($spawn,$output,strlen($output)) or die("Could not write output\n"); // guan bi sockets socket_close($spawn); socket_close($socket); ?> xia mian shi qi mei yi bu zhou de xiang xi shuo ming : 1. di yi bu shi jian li liang ge bian liang lai bao cun Socket yun xing de fu wu qi de IP di zhi he duan kou . ni ke yi she zhi wei ni zi ji de fu wu qi he duan kou ( zhe ge duan kou ke yi shi 1 dao 65535 zhi jian de shu zi ), qian ti shi zhe ge duan kou wei bei shi yong . PHP dai ma : <? // she zhi liang ge bian liang $host="192.168.1.99"; $port=1234; ?> 2. zai fu wu qi duan ke yi shi yong set_time_out() han shu lai que bao PHP zai deng dai ke hu duan lian jie shi bu hui chao shi . PHP dai ma : <? // chao shi shi jian set_time_limit(0); ?> 3. zai qian mian de ji chu shang , xian zai gai shi yong socket_creat() han shu chuang jian yi ge Socket le --- zhe ge han shu fan hui yi ge Socket ju bing , zhe ge ju bing jiang yong zai yi hou suo you de han shu zhong . PHP dai ma : <? // chuang jian Socket $socket=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket\n"); ?> di yi ge shen shu "AF_INET" yong lai zhi ding yu ming ; di er ge shen shu "SOCK_STREM" gao su han shu jiang chuang jian yi ge shen me lei xing de Socket( zai zhe ge li zi zhong shi TCP lei xing ) yin ci , ru guo ni xiang chuang jian yi ge UDP Socket de hua , ni ke yi shi yong ru xia de dai ma : PHP dai ma : <? // chuang jian socket $socket=socket_create(AF_INET,SOCK_DGRAM,0) or die("Could not create socket\n"); ?> 4. yi dan chuang jian le yi ge Socket ju bing , xia yi bu jiu shi zhi ding huo zhe bang ding ta dao zhi ding de di zhi he duan kou . zhe ke yi tong guo socket_bind() han shu lai wan cheng . PHP dai ma : <? // bang ding socket to zhi ding di zhi he duan kou $result=socket_bind($socket,$host,$port) or die("Could not bind to socket\n"); ?> 5. dang Socket bei chuang jian hao bing bang ding dao yi ge duan kou hou , jiu ke yi kai shi jian ting wai bu de lian jie le .PHP yuan xu ni you socket_listen() han shu lai kai shi yi ge jian ting , tong shi ni ke yi zhi ding yi ge shu zi ( zai zhe ge li zi zhong jiu shi di er ge shen shu :3) PHP dai ma : <? // kai shi jian ting lian jie $result=socket_listen($socket,3) or die("Could not set up socket listener\n"); ?> 6. dao xian zai , ni de fu wu qi chu le deng dai lai zi ke hu duan de lian jie qing qiu wai ji ben shang shen me ye mei you zuo . yi dan yi ge ke hu duan de lian jie bei shou dao ,socket_assept() han shu bian kai shi qi zuo yong le , ta jie shou lian jie qing qiu bing tiao yong ling yi ge zi Socket lai chu li ke hu duan -- fu wu qi jian de xin xi . PHP dai ma : <? // jie shou qing qiu lian jie // tiao yong zi socket chu li xin xi $spawn=socket_accept($socket) or die("Could not accept incoming connection\n"); ?> zhe ge zi socket xian zai jiu ke yi bei sui hou de ke hu duan -- fu wu qi tong xin suo yong le . 7. dang yi ge lian jie bei jian li hou , fu wu qi jiu hui deng dai ke hu duan fa song yi xie shu ru xin xi , zhe xie xin xi ke yi you socket_read() han shu lai huo de , bing ba ta fu zhi gei PHP de $input bian liang . PHP dai ma : <? // du qu ke hu duan shu ru $input=socket_read($spawn,1024) or die("Could not read input\n"); ?> socker_read de di er ge shen shu yong yi zhi ding du ru de zi jie shu , ni ke yi tong guo ta lai xian zhi cong ke hu duan huo qu shu ju de da xiao . zhu yi :socket_read han shu hui yi zhi du qu ke hu duan shu ju , zhi dao yu jian \n,\t huo zhe \0 zi fu .PHP jiao ben ba zhe xie zi fu kan zuo shi shu ru de jie shu fu . 8. xian zai fu wu qi bi xu chu li zhe xie you ke hu duan fa lai shi shu ju ( zai zhe ge li zi zhong de chu li jin jin bao han shu ju de shu ru he hui chuan dao ke hu duan ). zhe bu fen ke yi you socket_write() han shu lai wan cheng ( shi de you tong xin socket fa hui yi ge shu ju liu dao ke hu duan cheng wei ke neng ) PHP dai ma : <? // chu li ke hu duan shu ru bing fan hui shu ju $output=strrev($input) ."\n"; socket_write($spawn,$output,strlen($output)) or die("Could not write output\n"); ?> 9. yi dan shu chu bei fan hui dao ke hu duan , fu / zi socket dou ying tong guo socket_close() han shu lai zhong zhi PHP dai ma : <? // guan bi sockets socket_close($spawn); socket_close($socket); ?>
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚秀气模特裴裴(8)
时尚秀气模特裴裴(7)
时尚秀气模特裴裴(6)
时尚秀气模特裴裴(5)
白墙黑瓦
广州东郊[原创]
八美至丹巴的东谷天然盆景
不一样的
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有