在php中创建基于套接字的聊天应用程序 [英] Creating a socket based chat application in php
问题描述
请帮我找到解决方案
服务器端代码
< 表格 action = UDPsrvr.php 方法 = 发布 >
名称:< input type = text 名称 = data > < br >
< 输入 类型 = 提交 value = 发送 name = sub / >
< span class =code-keyword>< / form >
<? php
< span class =code-summarycomment>
error_reporting(E_ALL | < span class =code-summarycomment> E_STRICT);
$ socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($ socket, '127.0.0.1', < span class =code-summarycomment> 1223);
$ from =' ' ;
$ port = 0;
socket_recvfrom($ socket, $ buf, 12, 0, $ from, $ port); < span class =code-summarycomment>
// echo 已收到 $ buf 来自 remote 地址 $ from 和 远程 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);
}
?>
客户端代码
< 表格 action = UDPclint.php method = post >
Msg :) < input 类型 = text name = nm > < br >
< 输入 type = 提交 value = 发送 name = sub / >
< / form >
<? php
< span class =code-summarycomment> if(isset($ _ POST ['sub'])) {
$袜子 = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); <跨度cl ass =code-summarycomment>
$ msg = $ _ POST [' nm'];
$ len = strlen($ msg);
socket_sendto($ sock, $ msg, $ len, 0, '127.0。 0.1', 1223);
socket_close($ sock);
}
< span class =code-summarycomment>
? >
服务器和客户端都可以传递消息。
如何将此代码转换为实时聊天应用程序模型
socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind(
socket, '127.0.0.1', 1223);
Please help me to find solution 这篇关于在php中创建基于套接字的聊天应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< blockquote> from =' ' ;
SERVER SIDE CODE
<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
<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 modelsocket
= socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind(socket,
'127.0.0.1', 1223);
from
= '';