linux中c语言和php语言通信代码UDP&TCP
http://blog.chinaunix.net/uid-24015214-id-2644174.html
UDP方式通信
服务器端server.c
1 /*server.c*/ 2 #include <string.h> 3 #include <stdio.h> 4 #include <netinet/in.h> 5 #include <stdlib.h> 6 7 #define SERV_PORT 8000 8 9 int main() 10 { 11 int sockfd,n,servaddr_len; 12 char * server_ip = "127.0.0.1"; 13 char buf[80]; 14 struct sockaddr_in server,client; 15 16 17 18 19 20 sockfd = socket (AF_INET,SOCK_DGRAM,0); 21 22 23 24 25 26 bzero(&server,sizeof(server)); 27 server.sin_family = AF_INET; 28 server.sin_addr.s_addr = inet_addr(server_ip); 29 server.sin_port = htons(SERV_PORT); 30 31 servaddr_len = sizeof(server); 32 33 34 35 36 bind(sockfd,(struct sockaddr *) &server,servaddr_len); 37 listen(sockfd,6); 38 while(1) 39 { 40 printf("等待读取数据.......\n"); 41 42 servaddr_len = sizeof(client); 43 n = read(sockfd,buf,sizeof(buf)); 44 write(sockfd,buf,n); 45 printf("读取的数据为:%s\n",buf); 46 bzero(buf,sizeof(buf)); 47 } 48 close(sockfd); 49 return 0; 50 } 51 52 53
客户端client.php
1 <?php 2 $server_ip="127.0.0.1"; 3 $port = 8000; 4 if($_POST['submit']) 5 { 6 $buf=trim($_POST['buf']); 7 $sockfd=socket_create(AF_INET,SOCK_DGRAM,SOL_UDP)or die("创建失败"); 8 socket_connect($sockfd,$server_ip,$port)or die("连接错误"); 9 socket_write($sockfd,$buf,strlen($buf))or die("写入错误"); 10 11 socket_read($sockfd,$buf,80); 12 echo $buf; 13 $buf=""; 14 socket_close($sockfd); 15 } 16 ?> 17 <form action="client.php" method="post"> 18 请选则发送方式:<br><input type=text name="buf"> 19 <input type=submit name="submit" value="发送"> 20 </form>
server.c文件通过gcc编译器编译
client.php通过浏览器运行
TCP方式
服务器端
1 /*server.c*/ 2 #include <string.h> 3 #include <stdio.h> 4 #include <netinet/in.h> 5 #include <stdlib.h> 6 7 #define SERV_PORT 8000 8 9 int main() 10 { 11 int sockfd,n,servaddr_len,client_len,connfd; 12 char * server_ip = "127.0.0.1"; 13 char buf[80]; 14 struct sockaddr_in server,client; 15 16 17 18 sockfd = socket (AF_INET,SOCK_STREAM,0); 19 20 21 22 bzero(&server,sizeof(server)); 23 server.sin_family = AF_INET; 24 server.sin_addr.s_addr = inet_addr(server_ip); 25 server.sin_port = htons(SERV_PORT); 26 27 28 29 servaddr_len = sizeof(server); 30 client_len=sizeof(client); 31 32 33 bind(sockfd,(struct sockaddr *) &server,servaddr_len); 34 listen(sockfd,6); 35 while(1) 36 { 37 bzero(buf,sizeof(buf)); 38 printf("等待读取数据.......\n"); 39 connfd=accept(sockfd,(struct sockaddr *)&client,&client_len); 40 n = read(connfd,buf,sizeof(buf)); 41 // write(connfd,buf,n); 42 printf("读取的数据为:%s\n",buf); 43 44 } 45 close(sockfd); 46 return 0; 47 }
客户端
1 <?php 2 $server_ip="127.0.0.1"; 3 $port = 8000; 4 if($_POST['submit']) 5 { 6 $buf=trim($_POST['buf']); 7 $sockfd=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)or die("创建失败"); 8 socket_connect($sockfd,$server_ip,$port)or die("连接错误"); 9 socket_write($sockfd,$buf,strlen($buf))or die("写入错误"); 10 11 $a=socket_read($sockfd,80); 12 echo $a; 13 $buf=""; 14 socket_close($sockfd); 15 } 16 ?> 17 <form action="tcpclient.php" method="post"> 18 请选则发送方式:<br><input type=text name="buf"> 19 <input type=submit name="submit" value="发送"> 20 </form>
注意:1、tcp方式要设置安全级别及防火墙
centOS5.4设置方法为:系统->管理->安全级别及防火墙->SElinux选项卡
设置为允许即可
2、查看httpd_can_network_connect选项设置为on
centOS5.4设置方法为:在终端执行setsebool httpd_can_network_connect on 即可
本人推荐使用第二种方法