PHP 怎么与 C 语言通信?
phb 怎么与 C 语言通信?用 C 语言作为服务端,用 php 作为客户端。方法如下:
/*server.c*/
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
#define SERV_PORT 8000
int main()
{
int sockfd,n,servaddr_len,client_len,connfd;
char * server_ip = "127.0.0.1";
char buf[80];
struct sockaddr_in server,client;
sockfd = socket (AF_INET,SOCK_STREAM,0);
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(server_ip);
server.sin_port = htons(SERV_PORT);
servaddr_len = sizeof(server);
client_len=sizeof(client);
bind(sockfd,(struct sockaddr *) &server,servaddr_len);
listen(sockfd,6);
while(1)
{
bzero(buf,sizeof(buf));
printf("等待读取数据.......");
connfd=accept(sockfd,(struct sockaddr *)&client,&client_len);
n = read(connfd,buf,sizeof(buf));
write(connfd,buf,n);
printf("读取的数据为:%s",buf);
}
close(sockfd);
return 0;