user-0 提问: 在Linux下用C写一个代理服务器，在用Socket链接远端服务器的时候connect总是返回-1

回复:
user: user-0
content: 引用 3 楼 wanguser-1 的回复:[quote=引用 2 楼 u014598831 的回复:] [quote=引用 1 楼 wanguser-1 的回复:] [quote=引用 楼主 u014598831 的回复:] 这是我的代码，其中connect是要连接到例如百度等页面，然后再发送一个HTTP请求，GET一个页面，我还没有写http请求解析部分，linux下运行./proxy 8888 www.baidu.com telnet（这里我把telnet写成了23，方便调试），每次我开第二个终端时，telnet localhost 8888能够连接到我的代理，但是connect返回值是-1，无法连接。求大神帮助 #include "proxy_parse.h" #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netdb.h> #include<string.h> #define BACKLOG 2 int main(int argc, char * argv[])  { char buf[1000]; int connectsocket,clientsocket,proxysocket; int port; struct sockaddr_in server_addr;//server addr struct sockaddr_in client_addr; struct sockaddr_in proxy_addr;//this addr  struct{            char remote_host[64];            char service_name[32];         } pargs; //remote information  struct hostent *hostp; //struct servent *servp;   strcpy(pargs.remote_host,argv[2]); strcpy(pargs.service_name,argv[3]); //set remote information  connectsocket = socket(AF_INET,SOCK_STREAM,0);//socketformainserver clientsocket = socket(AF_INET,SOCK_STREAM,0);//socketforclientserver proxysocket = socket(AF_INET,SOCK_STREAM,0);//socketforproxyserver   int i; for(i=0;i<strlen(argv[1]);i++) {    if(!isdigit(*(argv[1]+i)))    {         break;    }    if(i==(strlen(argv[1])-1))    port = htons(atoi(argv[1])); } // //set proxy_addr bzero(&proxy_addr,sizeof(proxy_addr)); proxy_addr.sin_family = AF_INET; proxy_addr.sin_addr.s_addr = htonl(INADDR_ANY);//ip error! proxy_addr.sin_port = port; //  //set remote server_addr bzero(&server_addr,sizeof(server_addr)); if((hostp = gethostbyname(pargs.remote_host))!=NULL); {     memcpy(hostp->h_addr,&server_addr.sin_addr,hostp->h_length); }  server_addr.sin_family = AF_INET; server_addr.sin_port = htons(23); //  // bind&listen bind(proxysocket,(struct sockaddr*)&proxy_addr,sizeof(proxy_addr)); listen(proxysocket,BACKLOG);  while(1) { int a=0; socklen_t addrlen = sizeof(struct sockaddr); clientsocket = accept(proxysocket,(struct sockaddr*)&client_addr,&addrlen); connectsocket = socket(AF_INET,SOCK_STREAM,0); a=connect(connectsocket,(struct sockaddr *)&server_addr,sizeof(server_addr)); printf("return value of connect = %d\n",a); //connected //printf("connected!\n"); int iolen=0; iolen = recv(clientsocket,buf,sizeof(buf),0); send(connectsocket,buf,iolen,0); //send to romote  if((iolen = recv(connectsocket,buf,sizeof(buf),0)) <= 0) {    send(clientsocket,buf,iolen,0); }  } //    return 0; }  第61行：memcpy(hostp->h_addr,&server_addr.sin_addr,hostp->h_length);写反了 应该是： memcpy(&server_addr.sin_addr,hostp->h_addr,hostp->h_length);[/quote] 谢谢！！这个是后面加的。。之前我直接把ip地址赋给server_addr也是链接不上。。然后我改了这个也连不上。。。[/quote] 从这句server_addr.sin_port = htons(23);看你要连baidu的telnet端口？百度都没开这个端口你咋连，应该是80才对吧[/quote] 太感谢了。。我一直以为那边telnet能用，前面夜试了ssh。。就是没想到要改成web。。是我愚蠢了。。

user: user-1
content: 引用 2 楼 u014598831 的回复:[quote=引用 1 楼 wanguser-1 的回复:] [quote=引用 楼主 u014598831 的回复:] 这是我的代码，其中connect是要连接到例如百度等页面，然后再发送一个HTTP请求，GET一个页面，我还没有写http请求解析部分，linux下运行./proxy 8888 www.baidu.com telnet（这里我把telnet写成了23，方便调试），每次我开第二个终端时，telnet localhost 8888能够连接到我的代理，但是connect返回值是-1，无法连接。求大神帮助 #include "proxy_parse.h" #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netdb.h> #include<string.h> #define BACKLOG 2 int main(int argc, char * argv[])  { char buf[1000]; int connectsocket,clientsocket,proxysocket; int port; struct sockaddr_in server_addr;//server addr struct sockaddr_in client_addr; struct sockaddr_in proxy_addr;//this addr  struct{            char remote_host[64];            char service_name[32];         } pargs; //remote information  struct hostent *hostp; //struct servent *servp;   strcpy(pargs.remote_host,argv[2]); strcpy(pargs.service_name,argv[3]); //set remote information  connectsocket = socket(AF_INET,SOCK_STREAM,0);//socketformainserver clientsocket = socket(AF_INET,SOCK_STREAM,0);//socketforclientserver proxysocket = socket(AF_INET,SOCK_STREAM,0);//socketforproxyserver   int i; for(i=0;i<strlen(argv[1]);i++) {    if(!isdigit(*(argv[1]+i)))    {         break;    }    if(i==(strlen(argv[1])-1))    port = htons(atoi(argv[1])); } // //set proxy_addr bzero(&proxy_addr,sizeof(proxy_addr)); proxy_addr.sin_family = AF_INET; proxy_addr.sin_addr.s_addr = htonl(INADDR_ANY);//ip error! proxy_addr.sin_port = port; //  //set remote server_addr bzero(&server_addr,sizeof(server_addr)); if((hostp = gethostbyname(pargs.remote_host))!=NULL); {     memcpy(hostp->h_addr,&server_addr.sin_addr,hostp->h_length); }  server_addr.sin_family = AF_INET; server_addr.sin_port = htons(23); //  // bind&listen bind(proxysocket,(struct sockaddr*)&proxy_addr,sizeof(proxy_addr)); listen(proxysocket,BACKLOG);  while(1) { int a=0; socklen_t addrlen = sizeof(struct sockaddr); clientsocket = accept(proxysocket,(struct sockaddr*)&client_addr,&addrlen); connectsocket = socket(AF_INET,SOCK_STREAM,0); a=connect(connectsocket,(struct sockaddr *)&server_addr,sizeof(server_addr)); printf("return value of connect = %d\n",a); //connected //printf("connected!\n"); int iolen=0; iolen = recv(clientsocket,buf,sizeof(buf),0); send(connectsocket,buf,iolen,0); //send to romote  if((iolen = recv(connectsocket,buf,sizeof(buf),0)) <= 0) {    send(clientsocket,buf,iolen,0); }  } //    return 0; }  第61行：memcpy(hostp->h_addr,&server_addr.sin_addr,hostp->h_length);写反了 应该是： memcpy(&server_addr.sin_addr,hostp->h_addr,hostp->h_length);[/quote] 谢谢！！这个是后面加的。。之前我直接把ip地址赋给server_addr也是链接不上。。然后我改了这个也连不上。。。[/quote] 从这句server_addr.sin_port = htons(23);看你要连baidu的telnet端口？百度都没开这个端口你咋连，应该是80才对吧

user: user-0
content: 引用 1 楼 wanguser-1 的回复:[quote=引用 楼主 u014598831 的回复:] 这是我的代码，其中connect是要连接到例如百度等页面，然后再发送一个HTTP请求，GET一个页面，我还没有写http请求解析部分，linux下运行./proxy 8888 www.baidu.com telnet（这里我把telnet写成了23，方便调试），每次我开第二个终端时，telnet localhost 8888能够连接到我的代理，但是connect返回值是-1，无法连接。求大神帮助 #include "proxy_parse.h" #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netdb.h> #include<string.h> #define BACKLOG 2 int main(int argc, char * argv[])  { char buf[1000]; int connectsocket,clientsocket,proxysocket; int port; struct sockaddr_in server_addr;//server addr struct sockaddr_in client_addr; struct sockaddr_in proxy_addr;//this addr  struct{            char remote_host[64];            char service_name[32];         } pargs; //remote information  struct hostent *hostp; //struct servent *servp;   strcpy(pargs.remote_host,argv[2]); strcpy(pargs.service_name,argv[3]); //set remote information  connectsocket = socket(AF_INET,SOCK_STREAM,0);//socketformainserver clientsocket = socket(AF_INET,SOCK_STREAM,0);//socketforclientserver proxysocket = socket(AF_INET,SOCK_STREAM,0);//socketforproxyserver   int i; for(i=0;i<strlen(argv[1]);i++) {    if(!isdigit(*(argv[1]+i)))    {         break;    }    if(i==(strlen(argv[1])-1))    port = htons(atoi(argv[1])); } // //set proxy_addr bzero(&proxy_addr,sizeof(proxy_addr)); proxy_addr.sin_family = AF_INET; proxy_addr.sin_addr.s_addr = htonl(INADDR_ANY);//ip error! proxy_addr.sin_port = port; //  //set remote server_addr bzero(&server_addr,sizeof(server_addr)); if((hostp = gethostbyname(pargs.remote_host))!=NULL); {     memcpy(hostp->h_addr,&server_addr.sin_addr,hostp->h_length); }  server_addr.sin_family = AF_INET; server_addr.sin_port = htons(23); //  // bind&listen bind(proxysocket,(struct sockaddr*)&proxy_addr,sizeof(proxy_addr)); listen(proxysocket,BACKLOG);  while(1) { int a=0; socklen_t addrlen = sizeof(struct sockaddr); clientsocket = accept(proxysocket,(struct sockaddr*)&client_addr,&addrlen); connectsocket = socket(AF_INET,SOCK_STREAM,0); a=connect(connectsocket,(struct sockaddr *)&server_addr,sizeof(server_addr)); printf("return value of connect = %d\n",a); //connected //printf("connected!\n"); int iolen=0; iolen = recv(clientsocket,buf,sizeof(buf),0); send(connectsocket,buf,iolen,0); //send to romote  if((iolen = recv(connectsocket,buf,sizeof(buf),0)) <= 0) {    send(clientsocket,buf,iolen,0); }  } //    return 0; }  第61行：memcpy(hostp->h_addr,&server_addr.sin_addr,hostp->h_length);写反了 应该是： memcpy(&server_addr.sin_addr,hostp->h_addr,hostp->h_length);[/quote] 谢谢！！这个是后面加的。。之前我直接把ip地址赋给server_addr也是链接不上。。然后我改了这个也连不上。。。

user: user-1
content: 引用 楼主 u014598831 的回复:这是我的代码，其中connect是要连接到例如百度等页面，然后再发送一个HTTP请求，GET一个页面，我还没有写http请求解析部分，linux下运行./proxy 8888 www.baidu.com telnet（这里我把telnet写成了23，方便调试），每次我开第二个终端时，telnet localhost 8888能够连接到我的代理，但是connect返回值是-1，无法连接。求大神帮助 #include "proxy_parse.h" #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netdb.h> #include<string.h> #define BACKLOG 2 int main(int argc, char * argv[])  { char buf[1000]; int connectsocket,clientsocket,proxysocket; int port; struct sockaddr_in server_addr;//server addr struct sockaddr_in client_addr; struct sockaddr_in proxy_addr;//this addr  struct{            char remote_host[64];            char service_name[32];         } pargs; //remote information  struct hostent *hostp; //struct servent *servp;   strcpy(pargs.remote_host,argv[2]); strcpy(pargs.service_name,argv[3]); //set remote information  connectsocket = socket(AF_INET,SOCK_STREAM,0);//socketformainserver clientsocket = socket(AF_INET,SOCK_STREAM,0);//socketforclientserver proxysocket = socket(AF_INET,SOCK_STREAM,0);//socketforproxyserver   int i; for(i=0;i<strlen(argv[1]);i++) {    if(!isdigit(*(argv[1]+i)))    {         break;    }    if(i==(strlen(argv[1])-1))    port = htons(atoi(argv[1])); } // //set proxy_addr bzero(&proxy_addr,sizeof(proxy_addr)); proxy_addr.sin_family = AF_INET; proxy_addr.sin_addr.s_addr = htonl(INADDR_ANY);//ip error! proxy_addr.sin_port = port; //  //set remote server_addr bzero(&server_addr,sizeof(server_addr)); if((hostp = gethostbyname(pargs.remote_host))!=NULL); {     memcpy(hostp->h_addr,&server_addr.sin_addr,hostp->h_length); }  server_addr.sin_family = AF_INET; server_addr.sin_port = htons(23); //  // bind&listen bind(proxysocket,(struct sockaddr*)&proxy_addr,sizeof(proxy_addr)); listen(proxysocket,BACKLOG);  while(1) { int a=0; socklen_t addrlen = sizeof(struct sockaddr); clientsocket = accept(proxysocket,(struct sockaddr*)&client_addr,&addrlen); connectsocket = socket(AF_INET,SOCK_STREAM,0); a=connect(connectsocket,(struct sockaddr *)&server_addr,sizeof(server_addr)); printf("return value of connect = %d\n",a); //connected //printf("connected!\n"); int iolen=0; iolen = recv(clientsocket,buf,sizeof(buf),0); send(connectsocket,buf,iolen,0); //send to romote  if((iolen = recv(connectsocket,buf,sizeof(buf),0)) <= 0) {    send(clientsocket,buf,iolen,0); }  } //    return 0; }  第61行：memcpy(hostp->h_addr,&server_addr.sin_addr,hostp->h_length);写反了 应该是： memcpy(&server_addr.sin_addr,hostp->h_addr,hostp->h_length);