频道栏目
首页 > 程序开发 > Web开发 > PHP教程 > PHP进阶 > 网络编程 > 正文
PHP socket 网络编程实例
2016-01-08 17:11:42      个评论      
收藏   我要投稿
     如果你对socket理解太模糊,可以做做定位请求的项目,以下是我的经验的结晶,希望对大家有帮助

  Php代码

  //确保在连接客户端时不会超时

  set_time_limit(0);

  //设置IP和端口号

  $address='127.0.0.1';

  $port=2009; //调试的时候,可以多换端口来测试程序!

  //创建一个SOCKET

  if(($sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP))<0)

  {

  echo "socket_create() 失败的原因是:".socket_strerror($sock)."

  ";

  }

  //绑定到socket端口

  if(($ret=socket_bind($sock,$address,$port))<0)

  {

  echo "socket_bind() 失败的原因是:".socket_strerror($ret)."

  ";

  }

  //开始监听

  if(($ret=socket_listen($sock,4))<0)

  {

  echo "socket_listen() 失败的原因是:".socket_strerror($ret)."

  ";

  }

  do {

  if (($msgsock = socket_accept($sock)) < 0)

  {

  echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "/n";

  break;

  }

  //发到客户端

  $msg ="欢迎进入服务器!

  ";

  socket_write($msgsock, $msg, strlen($msg));

  echo "读取客户端发来的信息

  ";

  $buf = socket_read($msgsock,8192);

  $talkback = "收到的信息:$buf

  ";

  echo $talkback;

  /* if(socket_write($msgsock, $talkback, strlen($talkback))<0)

  {

  echo "socket_write() failed: reason: " . socket_strerror($msgsock) . "/n";

  }

  else

  {

  echo "发送成功";

  }*/

  //echo $buf;

  socket_close($msgsock);

  } while (true);

  socket_close($sock);

  ?>

  [php] view plaincopyprint?



  //确保在连接客户端时不会超时

  set_time_limit(0);

  //设置IP和端口号

  $address='127.0.0.1';

  $port=2009; //调试的时候,可以多换端口来测试程序!

  //创建一个SOCKET

  if(($sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP))<0)

  {

  echo "socket_create() 失败的原因是:".socket_strerror($sock)."

  ";

  }

  //绑定到socket端口

  if(($ret=socket_bind($sock,$address,$port))<0)

  {

  echo "socket_bind() 失败的原因是:".socket_strerror($ret)."

  ";

  }

  //开始监听

  if(($ret=socket_listen($sock,4))<0)

  {

  echo "socket_listen() 失败的原因是:".socket_strerror($ret)."

  ";

  }

  do {

  if (($msgsock = socket_accept($sock)) < 0)

  {

  echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "/n";

  break;

  }

  //发到客户端

  $msg ="欢迎进入服务器!

  ";

  socket_write($msgsock, $msg, strlen($msg));

  echo "读取客户端发来的信息

  ";

  $buf = socket_read($msgsock,8192);

  $talkback = "收到的信息:$buf

  ";

  echo $talkback;

  /* if(socket_write($msgsock, $talkback, strlen($talkback))<0)

  {

  echo "socket_write() failed: reason: " . socket_strerror($msgsock) . "/n";

  }

  else

  {

  echo "发送成功";

  }*/

  //echo $buf;

  socket_close($msgsock);

  } while (true);

  socket_close($sock);

  ?>

  CLIENT端

  Php代码

  error_reporting(E_ALL);

  set_time_limit(0);

  echo "

  TCP/IP Connection

  /n";

  $service_port = 2009;

  $address = "127.0.0.1";

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

  if ($socket < 0)

  {

  echo "socket_create() failed: reason: " . socket_strerror($socket) . "/n";

  }

  else

  {

  echo "OK./n";

  }

  echo "试图连接 '$address' 端口 '$service_port'...

  ";

  $result = socket_connect($socket, $address, $service_port);

  if ($result < 0)

  {

  echo "socket_connect() failed./nReason: ($result) " . socket_strerror($result) . "/n";

  }

  else

  {

  echo "连接OK

  ";

  }

  $in = "Hello/r/n";

  $in .= "我是Chester/r/n";

  $out = '';

  $out1='';

  if(!socket_write($socket, $in, strlen($in)))

  {

  echo "socket_write() failed: reason: " . socket_strerror($socket) . "/n";

  }

  else

  {

  echo "发送到服务器信息成功!

  ";

  echo "发送的内容为:$in

  ";

  }

  while($out = socket_read($socket, 8192))

  {

  echo "接收服务器回传信息成功!

  ";

  echo "接受的内容为:",$out;

  }

  echo "关闭SOCKET...

  ";

  socket_close($socket);

  echo "关闭OK

  ";

  ?>

  [php] view plaincopyprint?



  error_reporting(E_ALL);

  set_time_limit(0);

  echo "

  TCP/IP Connection

  /n";

  $service_port = 2009;

  $address = "127.0.0.1";

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

  if ($socket < 0)

  {

  echo "socket_create() failed: reason: " . socket_strerror($socket) . "/n";

  }

  else

  {

  echo "OK./n";

  }

  echo "试图连接 '$address' 端口 '$service_port'...

  ";

  $result = socket_connect($socket, $address, $service_port);

  if ($result < 0)

  {

  echo "socket_connect() failed./nReason: ($result) " . socket_strerror($result) . "/n";

  }

  else

  {

  echo "连接OK

  ";

  }

  $in = "Hello/r/n";

  $in .= "我是Chester/r/n";

  $out = '';

  $out1='';

  if(!socket_write($socket, $in, strlen($in)))

  {

  echo "socket_write() failed: reason: " . socket_strerror($socket) . "/n";

  }

  else

  {

  echo "发送到服务器信息成功!

  ";

  echo "发送的内容为:$in

  ";

  }

  while($out = socket_read($socket, 8192))

  {

  echo "接收服务器回传信息成功!

  ";

  echo "接受的内容为:",$out;

  }

  echo "关闭SOCKET...

  ";

  socket_close($socket);

  echo "关闭OK

  ";

  ?>

先运行server.php
再运行client.php


点击复制链接 与好友分享!回本站首页
相关TAG标签 网络编程 实例
上一篇:简体转繁体内码转换PHP函数
下一篇:最后一页
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站