Operate tcp and SOCKET function instances in php-PHP source code

Source: Internet
Author: User
Ec (2); create the socket service $ cstream_socket_server (& quot; tcp: 127.0.0.1: 1100 & quot;); while (1) {& nbsp; if (is_resource ($ astream_socket_accept ($ c) & nbsp; {& nbsp; dosomethingtime-consum script ec (2); script

// Create a socket Service
$ C = stream_socket_server ("tcp: // FIG: 1100 ");
While (1)
{
If (is_resource ($ a = stream_socket_accept ($ c )))
{
// Do something time-consuming
// Obtain the network socket name
If (stream_socket_get_name ($ a, true )! = False)
{
Fwrite ($ a, "regards form berlin ");
}
}
}

//

// Create two non-differentiated socket data stream connections
$ Sockets = stream_socket_pair (stream_pf_unix, stream_sock_stream, stream_ipproto_ip );
// Create a sub-process and return its pid
$ Pid = pcntl_fork ();
If ($ pid =-1)
{
// If no sub-process exists
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
{
/* Sub-process */
Fclose ($ sockets [1]);
Fwrite ($ sockets [0], "message from childn ");
Echo fgets ($ sockets [0]);
Fclose ($ sockets [0]);
}

//

/* Open a socket server */
$ 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 count */
Echo "data: '". stream_socket_recvfrom ($ socket, 1500). "'n ";
/* Disable 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 );
/* Disable socket */
Fclose ($ socket );

//

// Create a server
$ Socket = stream_socket_server ("tcp: // 0.0.0.0.0: 8000", $ errno, $ errstr );
// If creation fails
If (! $ Socket)
{
Echo "$ errstr ($ errno)
N ";
}
// If creation is successful
Else
{
// Accept the connection
While ($ conn = stream_socket_accept ($ socket ))
{
// Write data
Fwrite ($ conn, 'the local time is '. date ('n'/j/y g: I A'). "n ");
// Close the connection
Fclose ($ conn );
}
// Close the 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.