Example of operating TCP and socket functions in PHP

Source: Internet
Author: User
Tags local time

Create a Socket service
$c =stream_socket_server ("tcp://127.0.0.1:1100");
while (1)
{
if (Is_resource ($a =stream_socket_accept ($c))
{
Do something time-consuming
Get Network socket name
if (Stream_socket_get_name ($a, true)!==false)
{
Fwrite ($a, "regards form Berlin");
}
}
}

//

Create two no-difference socket data stream connections
$sockets =stream_socket_pair (STREAM_PF_UNIX,STREAM_SOCK_STREAM,STREAM_IPPROTO_IP);
To create a child process and return its PID
$pid =pcntl_fork ();
if ($pid ==-1)
{
If there are no child processes
Die (' could not fork ');
}
else if ($pid)
{
/* Parent Process * *
Fclose ($sockets [0]);
Fwrite ($sockets [1], "Child pid: $pidn");
Echo fgets ($sockets [1]);
Fclose ($sockets [1]);
}
Else
{
/* Child Process * *
Fclose ($sockets [1]);
Fwrite ($sockets [0], "message from CHILDN");
Echo fgets ($sockets [0]);
Fclose ($sockets [0]);
}

//

/* Open a Socket service side * *
$server =stream_socket_server (' tcp://127.0.0.1:1234 ');
/* Accept a connection * *
$socket =stream_socket_accept ($server);
/* GET Data * *
echo "Received Out-of-band:" ". Stream_socket_recvfrom ($socket, 1500,stream_oob)." n ";
* * Use PEEK to get data * *
echo "Data:". Stream_socket_recvfrom ($socket, 1500,stream_peek). "' n ";
/* Normal access number * *
echo "Data:". Stream_socket_recvfrom ($socket, 1500). "' n ";
/* Close socket*/
Fclose ($socket);
Fclose ($server);

//

/* Open a socket client * *
$socket =stream_socket_client (' tcp://127.0.0.1:1234 ');
/* Send normal data * *
Fwrite ($socket, "normal data transmit.");
* * Send more data.
Stream_socket_sendto ($socket, "out of band data.", Stream_oob);
/* Close socket*/
Fclose ($socket);

//

//Create server-side
$socket =stream_socket_server ("tcp://0.0.0.0:8000", $errno, $ERRSTR);
//If creation fails
if (! $socket)
{
  echo "$errstr ($errno) <br/>n";
}
//If creation succeeded
else
{
 //Accept Connection
  while ($conn =stream_socket_accept ($socket))
  {
& nbsp;  ///write Data
    fwrite ($conn, ' The local time '. Date (' n/j/y g:i a '). " n ");
   //close connection
    fclose ($conn);
 }
 //close socket
  Fclose ($socket);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.