PROGRAM
TO IMPLEMENT WEB CHAT APPLICATION
AIM:
To implement web
chatting application
Procedure
Step1 – Run XAMPP
Step2 – Start Apache Web Server
Step3 – copy the server.php and client.php file into
this location (C:\xampp\htdocs)
Step4 – type localhost/server.php in address bar in
any Web Browser( Eg. Firefox)
Step5 – type
localhost/client.php in address bar in another Web Browser ( Eg. Chrome)
Step6 – type any message in the text box of client and send
to server
Step7 –Server will reply a default message “Hi, Client !!!”
to client.
Step8 –Reload/Refresh the server page.
Step9– Client page: Type another message. Send to Server.
Step10 –Server will reply a default message “Hi, Client !!!”
to client.
Step11 – Repeat the step 8 if you want to send more messages.
Otherwise close the browser. Exit.
Source Code: server.php
<?php
// set some variables
$host =
"127.0.0.1";
$port = 25003;
// don't timeout!
set_time_limit(0);
// create socket
$socket =
socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n");
// bind socket to port
$result =
socket_bind($socket, $host, $port) or die("Could not bind to
socket\n");
// start listening for
connections
$result =
socket_listen($socket, 3) or die("Could not set up socket
listener\n");
// accept incoming
connections
// spawn another socket to
handle communication
$spawn =
socket_accept($socket) or die("Could not accept incoming
connection\n");
// read client input
$input = socket_read($spawn,
1024) or die("Could not read input\n");
echo ' Received Message :
<input type = "text" value="' . $input . '" />';
$output="Hi, Client!!!
";
echo '
<br><br>Sent Message : <input type = "text"
value="' . $output . '" />';
socket_write($spawn,
$output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Source Code: client.php
<form
action="#" method="post">
<p>Type Message :
<input type="text" name="message"></p>
<p><button
type="submit" name="submit">Send To
Server</button></p>
</form>
<?php
if(isset($_POST['message'])
&& !empty($_POST['message']))
{
sendMessage(htmlentities($_POST['message']));
}
function
sendMessage($message)
{
$host = "127.0.0.1";
$port = 25003;
// create socket
$socket =
socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n");
// connect to server
$result =
socket_connect($socket, $host, $port) or die("Could not connect to
server\n");
socket_write($socket,
$message, strlen($message)) or die("Could not send data to
server\n");
echo
'<br><br>Sent Message: <input type = "text" value="' . $message . '" />';
// get server response
$result = socket_read
($socket, 1024) or die("Could not read server response\n");
echo
'<br><br>Received Message : <input type = "text" value="' . $result . '" />';
// close socket
socket_close($socket);
}
?>
Output
Step1: Run
Server.php then run client.php
Step2: Type a
message in client side and press “Send To Server” button
Step3: Reload
server if you want to send more messages, then type a message in client side
and send to server.
RESULT:
Thus
the Web
Chat is implemented using PHP and
the output is verified successfully.