我正在使用以下代码重定向客户端请求。但是在执行以下操作时,客户端不会被重定向。它在浏览器中显示“无法连接”。我使用 iptables 将客户端重定向到端口 8080。并运行以下可执行文件进行重定向。如何重定向客户端。请提供解决方案......
#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);
}
}