Tutorial :UDP write to socket and read from socket at the same time



Question:

Server:

<?php  error_reporting(E_ALL | E_STRICT);    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);    socket_bind($socket, '127.0.0.1', 11104);    $from = "";  $port = 0;  socket_recvfrom($socket, $buf, 12, 0, $from, $port);  //$buf=socket_read($socket, 2048);    echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;  $msg="Sikerult";    //socket_write($socket, $msg, strlen($msg));  socket_sendto($socket, $msg, strlen($msg), 0, '127.0.0.1', 11104);  //socket_close($socket);  ?>  

client:

    $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);  $result = socket_connect($sock, '127.0.0.1', 11104);      $msg = "Sikerult";      $len = strlen($msg);          //socket_write($sock, $msg, strlen($msg));      socket_sendto($sock, $msg, $len, 0, '127.0.0.1', 11104);      //$buf=socket_read($sock, 2048);      socket_recvfrom($sock, $buf, 12, 0, $from, $port);      echo $buf;      socket_close($sock);  ?>  

the server receives the data from the client but the client got nothing from the server and not stop running.


Solution:1

The server is sending the packet back to itself, you need to pass it the port of the client, not of the server. So:

socket_recvfrom($socket, $buf, 12, 0, $from, $port);  socket_sendto($socket, $msg, strlen($msg), 0, $from, $port);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »