首页 > 解决方案 > Get the socket from an Apache module

问题描述

I need to retrieve the socket object in an apache C module. I have read the documentation and didn't find a way to retrieve this.

However, I can get the apr_sockaddr_t object for a request

标签: capachesocketsmodule

解决方案


Here is a way to do it :

apr_socket_t *asock = ap_get_conn_socket(req->connection);
int fd = 0;
apr_os_sock_get(&fd, asock);
char buf[1024] = "HTTP/1.1 200 OK\r\nContent-Length: 34\r\nContent-Type: text/html\r\nHost: localhost\r\n\r\nHELLO WORLD FROM AN AWESOME SOCKET";
write(fd, buf, strlen(buf));
close(fd);

推荐阅读