Multiple communication of the socket client-php the socket client

Source: Internet
Author: User
Keywords Socket client Socket JS PHP
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);
}
}
?>

  • 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.