Socket Client socketjsphp
Want to use PHP to do a socket client, you can implement a connection, and then send a message to the server multiple times. The concrete idea is this:
To make a page, this page has a button and the Message bar, click the button to send the content of the message bar to the server, then click again send, and do not need to reconnect, but the page opened with the server connection.
How is this going to come true? Please Daniel to help, the younger sister thanked first.
Enclose the code for the Socket class.
/** define IP, and port */
Define (' Php_socket_port ', ' 6000 ');
Define (' Php_socket_host ', ' 127.0.0.1 ');
Define (' Php_socket_start ', ');
Define (' Php_socket_end ', ');
Socket class
Class socket{
var $socket; Socket handle
var $sendflag = ">>>";
var $recvflag = "<<<";
var $response;
var $debug = 1;
function socket ($hostname, $port) {
$address = gethostbyname ($hostname);
$this->socket = socket_create (AF_INET,SOCK_STREAM,SOL_TCP);
$result = Socket_connect ($this->socket, $address, $port);
if ($this->debug = = 1) {
if ($result < 0) {
$result = "Socket_connect () Failed.\nreason: ($result)". Socket_strerror ($result). "
";
} else{
$result = "Connect OK.
";
}
}
}
function sendmsg ($msg) {
Socket_write ($this->socket, $msg, strlen ($msg));
$result = Socket_read ($this->socket,100);
$this->response = $result;
if ($this->debug = = 1) {
printf ("%s $msg
", $this->sendflag);
printf ("%s $result
", $this->recvflag);
}
return $result;
}
function Close () {
Socket_close ($this->socket);
}
}
?>