php与C语言通过AF_UNIX来进行IPC通信的例子
1)编写s_unix.c服务端文件
#include
#include
#include
#include
#define UNIX_DOMAIN "/tmp/UNIX.domain"
int main(void)
{
socklen_t clt_addr_len;
int listen_fd;
int com_fd;
int ret;
int i;
static char recv_buf[1024];
int len;
struct sockaddr_un clt_addr;
struct sockaddr_un srv_addr;
listen_fd=socket(PF_UNIX,SOCK_STREAM,0);
if(listen_fd<0)
{
perror("cannot create communication socket");
return 1;
}
//set server addr_param
srv_addr.sun_family=AF_UNIX;
strncpy(srv_addr.sun_path,UNIX_DOMAIN,sizeof(srv_addr.sun_path)-1);
unlink(UNIX_DOMAIN);
//bind sockfd & addr
ret=bind(listen_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
if(ret==-1)
{
perror("cannot bind server socket");
close(listen_fd);
unlink(UNIX_DOMAIN);
return 1;
}
//listen sockfd
ret=listen(listen_fd,1);
if(ret==-1)
{
perror("cannot listen the client connect request");
close(listen_fd);
unlink(UNIX_DOMAIN);
return 1;
}
//have connect request use accept
len=sizeof(clt_addr);
com_fd=accept(listen_fd,(struct sockaddr*)&clt_addr,&len);
if(com_fd<0)
{
perror("cannot accept client connect request");
close(listen_fd);
unlink(UNIX_DOMAIN);
return 1;
}
//read and printf sent client info
printf("/n=====info=====/n");
for(i=0;i<4;i++)
{
memset(recv_buf,0,1024);
int num=read(com_fd,recv_buf,sizeof(recv_buf));
//printf("num=%d/n",num);
printf("Message from client (%d)) :%s/n",num,recv_buf);
//printf("i=%d/n",i);
write(com_fd,"hello world",11,0);
printf("/n%s/n","server to client msg ok");
}
// write(com_fd,"hello world",11,0);
// printf("/n%s/n","server to client msg ok");
//printf("%s/n","end");
//write(listen_fd,"aaadfd",6,0);
close(com_fd);
close(listen_fd);
unlink(UNIX_DOMAIN);
return 0;
}
2)编写c_unix.c客户端文件
#include
#include
#include
#include
#define UNIX_DOMAIN "/tmp/UNIX.domain"
int main(void)
{
int connect_fd;
int ret;
char snd_buf[1024];
int i;
static struct sockaddr_un srv_addr;
//creat unix socket
connect_fd=socket(PF_UNIX,SOCK_STREAM,0);
if(connect_fd<0)
{
perror("cannot create communication socket");
return 1;
}
srv_addr.sun_family=AF_UNIX;
strcpy(srv_addr.sun_path,UNIX_DOMAIN);
//connect server
ret=connect(connect_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
if(ret==-1)
{
perror("cannot connect to the server");
close(connect_fd);
return 1;
}
memset(snd_buf,0,1024);
strcpy(snd_buf,"message from client");
//send info server
for(i=0;i<4;i++)
write(connect_fd,snd_buf,sizeof(snd_buf));
close(connect_fd);
return 0;
}
3)编写PHP客户端文件 test.php
define("UNIX_DOMAIN","/tmp/UNIX.domain");
// echo UNIX_DOMAIN;
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0); //第三个参数为0
if ($socket < 0)
{
echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
}
else
{
echo "create OK.\n";
}
$result = socket_connect($socket, UNIX_DOMAIN); //这里只要两个参数即可
if ($result < 0)
{
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
}
else
{
echo "connect OK";
}
//下面的代码和普通php socket通信一致
$in = "测试IPC通信\n";
if(!socket_write($socket, $in, strlen($in)))
{
echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n";
}
else
{
echo "send message ok!\n";
echo "\naccept message=".socket_read($socket, 8192)."\n";
}
echo "Close SOCKET...\n";
socket_close($socket);
echo "Close OK\n";
?>
4)编译s_unix.c和c_unix.c文件
gcc s_unix.c -o s_unix
gcc c_unix.c -o c_unix
5)运行测试
先启动一个窗口运行s_unix,方法 为:./s_unix
然后 再开启一个窗口运行c_unix进行测试 :./c_unix
这里主要是来测试 PHP与C语言的通信 可以再开启一个窗口运行php test.php即可
效果如图 :