client.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * 分段发送数据
  4. *
  5. * @param swoole_client $client
  6. * @param string $data
  7. * @param int $chunk_size
  8. */
  9. function send_chunk(swoole_client $client, $data, $chunk_size = 1024)
  10. {
  11. $len = strlen($data);
  12. $chunk_num = intval($len / $chunk_size) + 1;
  13. for ($i = 0; $i < $chunk_num; $i++)
  14. {
  15. if ($len < ($i + 1) * $chunk_size)
  16. {
  17. $sendn = $len - ($i * $chunk_size);
  18. }
  19. else
  20. {
  21. $sendn = $chunk_size;
  22. }
  23. $client->send(substr($data, $i * $chunk_size, $sendn));
  24. }
  25. }
  26. $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
  27. if(!$client->connect('127.0.0.1', 9501, 0.5, 0))
  28. {
  29. echo "Over flow. errno=".$client->errCode;
  30. die("\n");
  31. }
  32. //for ($i = 0; $i < 10; $i++)
  33. //{
  34. // $client->send("hello world\r\n\r\n");
  35. // echo "send\n";
  36. //}
  37. //exit;
  38. $data = array(
  39. 'name' => __FILE__,
  40. 'content' => str_repeat('A', 8192 * rand(1, 3)), //800K
  41. );
  42. $_serialize_data = serialize($data);
  43. $_send = $_serialize_data."__doit__";
  44. echo "serialize_data length=".strlen($_serialize_data)."send length=".strlen($_send)."\n";
  45. //send_chunk($client, $_send);
  46. //
  47. if(!$client->send($_send))
  48. {
  49. die("send failed.\n");
  50. }
  51. //$client->send("\r\n".substr($_serialize_data, 0, 8000));
  52. echo $client->recv();
  53. exit;
  54. $client->send(substr($_serialize_data, 8000));
  55. //usleep(500000);
  56. if (!$client->send("\r\n\r\n"))
  57. {
  58. die("send failed.\n");
  59. }
  60. echo $client->recv();
  61. //sleep(1);