我想为具有基于UNIX套接字的服务器接口的程序编写Web界面,但似乎无法在CGI程序中成功连接到它。
我查看了httpd.conf文件并在网上搜索,但没有找到。 CGI程序是用C ++编写的。 UNIX套接字位于httpd服务器允许的路径中。
答案 0 :(得分:2)
要作为客户端连接到unix本地套接字,首先要创建一个匿名套接字,例如对于TCP:
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
然后使用目标路径创建适当的struct sockaddr_un
:
struct sockaddr_un addr;
addr.sun_family = AF_LOCAL;
strcpy(addr.sun_path, "/path/to/server/socket/file");
并致电connect()
:
connect(fd, (struct sockaddr*)&addr, sizeof(addr));
然而,这些都在手册页和在线文章中有详细记载。除非您使用第三方库,否则没有特定于C ++的方法;这些是标准C的* nix特定扩展:
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <sys/types.h>
GNU C参考手册has a chapter。
答案 1 :(得分:0)
您的服务器创建的套接字没有“&#39; group&#39;的写入权限”。和&#39;所有&#39;:srwxr_xr_x。 因此,在服务器创建管道后将其更改为所有访问权限(chmod 777 ..等)。然后,我打赌,你的CGI程序将连接...; - )
在绑定之后执行chmod ..在c程序中它看起来像这样:
if (bind(sockfd, (struct sockaddr *)&serv_addr, servlen) < 0) {
perror("Error binding socket:");
exit 1;
}
if (chmod(PipeName, 0777) == -1) {
perror("Error in chmod:");
exit 1;
}