Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Please help me to find solution

SERVER SIDE CODE

XML
<form action="UDPsrvr.php" method="post">
            Name :<input type="text" name="data"><br>
            <input type="submit" value="Send" name="subt"/>
        </form>
    <?php


error_reporting(E_ALL | E_STRICT);

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, '127.0.0.1', 1223);

$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 12, 0, $from, $port);

//echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;

echo "<textarea name='n'>".$buf."</textarea>";

if($_POST['subt']){
  $data=  $_POST['data'];
   socket_sendto($sock, $data, $len, 0, '127.0.0.1', 1223);
}
   ?>





CLIENT SIDE CODE

XML
  <form action="UDPclint.php" method="post">
        Msg  :) <input type="text" name="nm"><br>
        <input  type="submit" value="Send" name="sub"/>
    </form>
<?php
  if(isset($_POST['sub'])) {
     $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);


$msg = $_POST['nm'];
$len = strlen($msg);

socket_sendto($sock, $msg, $len, 0, '127.0.0.1', 1223);
socket_close($sock);
  }

?>


Both server and client can pass messages.
How to convert this code to a real time chat application model
Posted
Updated 22-Sep-15 18:19pm
v2
Comments
Mohibur Rashid 23-Sep-15 20:03pm    
First of all, this is not a chat application model.
I guess, you still need to learn how PHP and client/server model works. You also need to understand the term UDP and TCP. Text chat in UDP model is not ideal.
May be you should take a look at websocket.

1 solution

Kindly go through the following article

Chat Application in PHP[^]

Hope the above link helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900