gpt4 book ai didi

c - 我无法使用简单的代理服务器到浏览器获得 http 响应

转载 作者:行者123 更新时间:2023-11-30 17:00:18 25 4
gpt4 key购买 nike

我已经编译了这个 c 文件并在端口 6666 上运行。我已将浏览器设置更改为 localhost 和端口 6666。当我尝试连接时,我无法加载 html 页面我收到连接重置浏览器如何解决这个问题?

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

void error(char* msg)
{
perror(msg);
exit(0);
}

int main(int argc,char* argv[])
{
pid_t pid;
struct sockaddr_in addr_in,cli_addr,serv_addr;
struct hostent* host;
int sockfd,newsockfd;

if(argc<2)
error("./proxy <port_no>");

printf("\n*****WELCOME TO PROXY SERVER*****\n");
printf("\nCopyright (c) 2014 GODLY T.ALIAS\n\n");

bzero((char*)&serv_addr,sizeof(serv_addr));
bzero((char*)&cli_addr, sizeof(cli_addr));

serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(atoi(argv[1]));
serv_addr.sin_addr.s_addr=INADDR_ANY;


sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sockfd<0)
error((char*)"Problem in initializing socket");

if(bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
error((char*)"Error on binding");


listen(sockfd,50);
//int clilen=sizeof(cli_addr);
socklen_t clilen;


accepting:

newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clilen);

if(newsockfd<0)
error((char*)"Problem in accepting connection");

pid=fork();
if(pid==0)
{
struct sockaddr_in host_addr;
int flag=0,newsockfd1,n,port=0,i,sockfd1;
char buffer[510],t1[300],t2[300],t3[10];
char* temp=NULL;
bzero((char*)buffer,500);
recv(newsockfd,buffer,500,0);

sscanf(buffer,"%s %s %s",t1,t2,t3);


if(((strncmp(t1,"GET",3)==0))&&((strncmp(t3,"HTTP/1.1",8)==0)||(strncmp(t3,"HTTP/1.0",8)==0))&&(strncmp(t2,"http://",7)==0))
{
strcpy(t1,t2);

flag=0;

for(i=7;i<strlen(t2);i++)
{
if(t2[i]==':')
{
flag=1;
break;
}
}

temp=strtok(t2,"//");
if(flag==0)
{
port=80;
temp=strtok(NULL,"/");
}
else
{
temp=strtok(NULL,":");
}

sprintf(t2,"%s",temp);
printf("host = %s",t2);
host=gethostbyname(t2);

if(flag==1)
{
temp=strtok(NULL,"/");
port=atoi(temp);
}


strcat(t1,"^]");
temp=strtok(t1,"//");
temp=strtok(NULL,"/");
if(temp!=NULL)
temp=strtok(NULL,"^]");
printf("\npath = %s\nPort = %d\n",temp,port);


bzero((char*)&host_addr,sizeof(host_addr));
host_addr.sin_port=htons(port);
host_addr.sin_family=AF_INET;
bcopy((char*)host->h_addr,(char*)&host_addr.sin_addr.s_addr,host->h_length);

sockfd1=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
newsockfd1=connect(sockfd1,(struct sockaddr*)&host_addr,sizeof(struct sockaddr));
sprintf(buffer,"\nConnected to %s IP - %s\n",t2,inet_ntoa(host_addr.sin_addr));
if(newsockfd1<0)
error("Error in connecting to remote server");

printf("\n%s\n",buffer);
//send(newsockfd,buffer,strlen(buffer),0);
bzero((char*)buffer,sizeof(buffer));
if(temp!=NULL)
sprintf(buffer,"GET /%s %s\r\nHost: %s\r\nConnection: close\r\n\r\n",temp,t3,t2);
else
sprintf(buffer,"GET / %s\r\nHost: %s\r\nConnection: close\r\n\r\n",t3,t2);


n=send(sockfd1,buffer,strlen(buffer),0);
printf("\n%s\n",buffer);
if(n<0)
error("Error writing to socket");
else{
do
{
bzero((char*)buffer,500);
n=recv(sockfd1,buffer,500,0);
if(!(n<=0))
send(newsockfd,buffer,n,0);
}while(n>0);
}
}
else
{
send(newsockfd,"400 : BAD REQUEST\nONLY HTTP REQUESTS ALLOWED",18,0);
}
close(sockfd1);
close(newsockfd);
close(sockfd);
_exit(0);
}
else
{
close(newsockfd);
goto accepting;
}
return 0;
}

最佳答案

这可能不是代码问题,而是 net::ERR_CONNECTION_RESET 问题。

可能的解决方案:转到

chrome 设置 > 隐私 > 内容设置 > Cookie > 所有 cookie 和站点数据 > 删除域问题

reference link

关于c - 我无法使用简单的代理服务器到浏览器获得 http 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37695915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com