How can I solve the problem of sending PHPsocket to the specified address?

Source: Internet
Author: User
PHPsocket is sent to the specified address. Three users log on to the server through socket, and the IP addresses and ports of the three users are recorded through mysql: A: 192.168.1.100: 13564B: 192.168.1.100: 13565C: 192.168.1.100: 135 PHP socket sent to the specified address
Three users log on to the server through socket, and use mysql to record the IP addresses and ports of the three users:
A: 192.168.1.100: 13564 B: 192.168.1.100: 13565 C: 192.168.1.100: 13566

Now I want to send A finger (including the port of Customer B) to customer A. after the server receives the analysis, it extracts the address corresponding to Customer B from the database and sends a test character to Customer B.

I added the following code to the server and encountered an error,
PHP Warning: socket_connect (): unable to connect [0]: an invalid parameter is provided.

PHP code
  $conn = socket_connect ($socket, '192.168.1.100', 13565);  $buffer = "ok";$command = socket_sendto($socket,$buffer,strlen($buffer),0,'192.168.1.100','192.168.1.100');


Can I use socket_sendto on the server? How can this be achieved?



Server:
PHP code
  $addr = "192.168.1.121";$port = 1338;$remoteIP = "";$remotePort = "";$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);if($socket < 0) {        echo "Socket create:".$socket_strerror($socket)."\n";        exit;}if ( ($ret = socket_bind($socket, $addr, $port)) < 0 ) {    echo "socket bind:".socket_strerror()."\n";    exit;}if ( ($ret = socket_listen($socket, 5)) < 0 ) {    echo "socket listen:".socket_strerror()."\n";    exit;}socket_set_nonblock($socket);echo "Wainting for a connection:\n";$clients = array($socket);while(true) {    $read = $clients;    if(socket_select($read, $writes=NULL, $execs=NULL, 3) < 1){        //echo ".";        continue;    }    if(in_array($socket,$read)) {        $clients[] = $newsock = socket_accept($socket);        socket_set_nonblock($newsock);        socket_getpeername($newsock,&$remoteIP,&$remotePort);                $key = array_search($socket,$read);        unset($read[$key]);        $msg = "Server: $addr\r\n".                "Notice:Welcome the user from ".$remoteIP.":".$remotePort.".\r\n";        echo $remoteIP.":".$remotePort." conected in.\r\n";        socket_write($newsock,$msg);    }    foreach($read as $read_sock) {        $data = @socket_read($read_sock,1024,PHP_BINARY_READ);        if($data == false) {            $key = array_search($read_sock,$clients);            print_r($clients[$key]);            unset($clients[$key]);            socket_close($read_sock);            echo "Client disconnected.\n";            continue;        }                $command = strtoupper ($data);                switch ($command)        {            case "LIST":                $writer = print_r($clients,1);                break;            case "HELLO":                $writer = "Hello Everybody!";                break;            case "QUIT":                $writer = "Bye-Bye";                break;            case "SLEEP":                sleep(30);                $writer = "sleeped 10 seconds!";                break;            case "HELP":                $writer = "HELLO\tQUIT\tHELP";                break;            default:                $writer = "Error Command!\r\nType help for help message!";        }        socket_write($read_sock, $writer);        //exit;    }}socket_close($socket);


Client:
PHP code
  // Client // sets error handling error_reporting (E_ALL); // sets the processing time set_time_limit (0); $ ip = "192.168.1.121"; // IP address $ port = 1338; // port number $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // Create a SOCKET if ($ socket) echo "socket_create () successed! \ N "; else echo" socket_create () failed :". socket_strerror ($ socket ). "\ n"; $ conn = socket_connect ($ socket, $ ip, $ port); // Create a SOCKET connection if ($ conn) echo "Success to connection! [". $ Ip. ":". $ port. "] \ n"; else echo "socket_connect () failed :". socket_strerror ($ conn ). "\ n"; echo socket_read ($ socket, 1024); $ stdin = fopen ('php: // stdin ', 'r'); while (true) {$ command = trim (fgets ($ stdin, 1024); socket_write ($ socket, $ command, strlen ($ command); $ msg = trim (socket_read ($ socket, 1024); echo $ msg. "\ n"; if ($ msg = "Bye-Bye") break;} fclose ($ stdin); socket_close ($ socket );
Related Article

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.