2012-08-16 27 views
0

我正在使用以下代码重定向客户端请求。但是,在执行以下操作时,客户端不会重定向。它在浏览器中显示“无法连接”。我使用iptables将客户端重定向到8080端口。并运行以下可执行文件重定向。如何重定向客户端。请提供溶液....重定向到c中的网站

#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <time.h> 

#include<stdlib.h> 

int main(int argc, char *argv[]) 
{ 
int listenfd = 0, connfd = 0; 
struct sockaddr_in serv_addr; 

char *reply = "HTTP/1.1 301 Moved Permanently\nServer: Apache/2.2.3\nLocation: 
http://www.google.com\nContent-Length: 1000\nConnection: close\nContent-Type: 
text/html; charset=UTF-8"; 

char sendBuff[1025]; 
time_t ticks; 

listenfd = socket(AF_INET, SOCK_STREAM, 0); 
memset(&serv_addr, '0', sizeof(serv_addr)); 
memset(sendBuff, '0', sizeof(sendBuff)); 

serv_addr.sin_family = AF_INET; 
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); 
serv_addr.sin_port = htons(8080); 


bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

listen(listenfd, 10); 

while(1) 
{ 
    connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 

printf("client connected\n"); 
    send(connfd, reply, strlen(reply), 0); 

    close(connfd); 
    sleep(1); 
} 
} 
+0

你需要发送一个位置:XXX在http输出的标题和状态301(永久移动) – Ulterior 2012-08-16 07:10:43

+0

如何添加该行。请提供代码.. – user1216216 2012-08-16 07:11:33

+0

您需要先创建一个有效的HTTP响应,请谷歌什么协议要求最低 - 在你的情况下,你只是发送一个HTML身体没有http状态和标头 – Ulterior 2012-08-16 07:13:21

回答

0

请参阅例如在this pagethis page构建在服务器端的有效的HTTP响应。然后,在它下面添加你的html主体。

最小你需要的是

HTTP/1.1 200 OK 
Content-Length: XXXXX <- put size of the your html body 
Connection: close 
Content-Type: text/html; charset=UTF-8 
0

我无法重现您看到的错误。你应该提供更多的细节(例如,什么样的客户端,iptables规则的确切文本)。对于我的测试,我没有设置任何iptables规则,而是直接将Firefox 12.0浏览器指向localhost:8080

分裂您的回复,使其更易于阅读显示:

char *reply = 
"HTTP/1.1 301 Moved Permanently\n" 
"Server: Apache/2.2.3\n" 
"Location: http://www.google.com\n" 
"Content-Length: 1000\n" 
"Connection: close\n" 
"Content-Type: text/html; charset=UTF-8" 
; 

虽然RFC指定\r\n为行终止,大多数客户会接受\n(你不说你正在使用的客户端) 。但是,另外三个明显的问题是最后一行没有终止,响应本身没有以空行结束,并且您有标头1000,但没有内容。任何这些问题都可能导致客户将响应视为无效并忽略它。

char *reply = 
"HTTP/1.1 301 Moved Permanently\r\n" 
"Server: Apache/2.2.3\r\n" 
"Location: http://www.google.com\r\n" 
"Content-Length: 0\r\n" 
"Connection: close\r\n" 
"Content-Type: text/html; charset=UTF-8\r\n" 
"\r\n" 
; 

进一步读入您的代码后,您在发送答复后立即关闭连接,而无需先读取请求。这可能会导致在请求完全传递到服务器之前关闭连接的(尽管不太可能)竞赛。然后,当请求确实到达时,它将触发对客户端的重置,并且响应可能被丢弃。所以,你应该添加代码,以使您的回复交付更强大的:

printf("client connected\n"); 
send(connfd, reply, strlen(reply), 0); 
shutdown(connfd, SHUT_WR); 
while (recv(connfd, sendBuff, sizeof(sendBuff), 0) > 0) {} 
close(connfd); 

既然我不能回应重现您的问题,因为它是,但是,它也有可能是你没有设置你的iptable正确重定向规则。

+0

我正在运行代码在端口8080.和我有一个iptable规则将客户端重定向到端口8080.所以,每当一个客户端连接重定向到这个端口,我在端口8080运行这个重定向器。因此,客户端一次又一次地击中仅限端口8080。所以它没有被重定向。请为我提供一个正确重定向的解决方案。 – user1216216 2012-08-28 12:59:19

+0

@ user1216216:由于您从未提供过您使用的规则,因此我无法告诉您您做错了什么。 – jxh 2012-08-28 15:14:58

+0

iptables -t nat -A PREROUTING -p tcp -j REDIRECT - 对端口8080 这是我用来重定向到端口8080的规则。由于我重定向到这个端口,所有请求都通过端口并在给定的程序中我重定向到某个网站[www.google.com],这也通过端口8080.因此,客户端在端口8080循环..然后我没有得到重定向页面在我的浏览器中现在告诉我如何将客户端重定向到端口8080和端口8080,客户端应该被重定向到其他网站..?。 – user1216216 2012-09-17 07:14:25