您的位置:首页 > 理论基础 > 计算机网络

linux中c语言和php语言通信代码UDP&TCP

2013-10-31 09:52 501 查看
linux中c语言和php语言通信代码UDP&TCP

http://blog.chinaunix.net/uid-24015214-id-2644174.html

UDP方式通信

服务器端server.c

<?php
$server_ip="127.0.0.1";
$port = 8000;
if($_POST['submit'])
{
$buf=trim($_POST['buf']);
$sockfd=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)or die("创建失败");
socket_connect($sockfd,$server_ip,$port)or die("连接错误");
socket_write($sockfd,$buf,strlen($buf))or die("写入错误");

$a=socket_read($sockfd,80);
echo $a;
$buf="";
socket_close($sockfd);
}
?>
<form action="tcpclient.php" method="post">
请选则发送方式:<br><input type=text name="buf">
<input type=submit name="submit" value="发送">
</form>


View Code

注意:1、tcp方式要设置安全级别及防火墙

centOS5.4设置方法为:系统->管理->安全级别及防火墙->SElinux选项卡
设置为允许即可
2、查看httpd_can_network_connect选项设置为on
centOS5.4设置方法为:在终端执行setsebool httpd_can_network_connect on 即可
本人推荐使用第二种方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: