"Socket" uses PHP socket to implement client to server communication ____php

Source: Internet
Author: User
Tags echo command

One, server.php service end:

<?php error_reporting (E_all);
Set_time_limit (0);
Ob_implicit_flush ();
Local IP $address = ' localhost ';
Set up 111 ports for communication $port = 111; Create a socket if (($sock = Socket_create (Af_inet, Sock_stream, sol_tcp)) < 0) {echo "Socket creation failure Reason". Socket_strerror ($sock).
"\ n"; } if ($ret = Socket_bind ($sock, $address, $port)) < 0) {echo creates socket failure reason. Socket_strerror ($ret).
"\ n"; //Listen for if ($ret = Socket_listen ($sock, 5)) < 0) {echo "Monitor failure Reason". Socket_strerror ($ret).
"\ n"; Do {//Receive command if ($msgsock = @socket_accept ($sock)) < 0) {echo command receives failure reason: ". Socket_strerror ($msgsock).
		"\ n";
	Break $msg = "\nphp Test Server. \ n "." Use Quit,shutdown,sun ...

	such as command test. \ n ";

	@socket_write ($msgsock, $msg, strlen ($msg));  do {if ($buf = @socket_read ($msgsock, 2048, Php_normal_read))) {echo "Socket_read () Failed:reason:". Socket_strerror ($ret).
				"\ n";
		Break 2;
		} if (! $buf = Trim ($buf)) {continue;
		} if ($buf = = ' quit ') {break; } if ($buf= = ' shutdown ') {socket_close ($msgsock);
		Break 2;
		if ($buf = = ' Sun ') {echo ' What are you doing? ';
		$talkback = "backinformation: ' $buf '. \ n";
		Socket_write ($msgsock, $talkback, strlen ($talkback));
	echo "$BUF \ n";

	} while (true);

Socket_close ($msgsock);

} while (true); Socket_close ($sock);?>

Second, client.php client:

<?php
error_reporting (e_all);
Port
$service _port =;
Local
$address = ' localhost ';
Create a TCP/IP socket
$socket = socket_create (Af_inet, Sock_stream, sol_tcp);
if ($socket < 0) {
		echo Socket creation failure Reason:. Socket_strerror ($socket). "\ n";
} else {
		echo "Ok,he he.\n";
}
$result = Socket_connect ($socket, $address, $service _port);
if ($result < 0) {
		echo Socket connection failure Reason: ($result). Socket_strerror ($result). "\ n";
} else {
		echo "ok.\n";
}
Send command
$in = "head/http/1.1\r\n";
$in. = "connection:close\r\n\r\n";
$out = ';
echo "Send Command ... ...";
$in = "sun\n";
Socket_write ($socket, $in, strlen ($in));
echo "ok.\n";
echo "Reading backinformatin:\n\n";
while ($out = Socket_read ($socket, 2048)) {
		echo $out;
}
echo "close socket ...";
Socket_close ($socket);
echo "Ok,he he.\n\n";
? >

These two pieces of code is a simple introduction to use PHP to implement client and server communication
client.php
server.php
The specific operation, in DOS under the PHP command to open the server, in the listening state.
Then open a DOS window, with the PHP command to open the client, will get the service side of the response ....

#Php



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.