我正在尝试用C语言使用SOCKS5代理进行超文本传输协议请求,我尝试了下面的代码
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char** argv) {
struct addrinfo hints, *res;
int sockfd;
memset(&hints,0,sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo("localhost","9050",&hints,&res);
sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
int connected = connect(sockfd,res->ai_addr,res->ai_addrlen);
if (connected == -1) {
perror("Error");
}
char buffer[256];
char msginit[256];
msginit[0] = '\5'; //Protocol number
msginit[1] = '\2'; //# of authentication methods
msginit[2] = '\0'; //no authentication
msginit[3] = '\2'; //user+pass auth
//get dest
memset(&hints,0,sizeof hints);
memset(res,0,sizeof *res);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo("duckduckgo.com","80",&hints,&res);
struct sockaddr_in *ip = (struct sockaddr_in *)res->ai_addr;
uint32_t* addr=&(ip->sin_addr.s_addr);
//copy dest to request
memcpy (msginit+4,addr,sizeof (uint32_t));
send(sockfd, (void *)msginit, (size_t)strlen(msginit),0);
printf("Sent.\n");
recv(sockfd,buffer,256,0); //This is where it gets stuck!
printf("%s\n",buffer);
}它说它发送,但请求没有发送到站点,而且我也无法从recv读取任何数据,你知道我如何解决这个问题吗?
发布于 2018-03-24 23:08:04
正如注释中所建议的,您不能使用strlen()来获取二进制缓冲区的使用量。在这种情况下,您知道要发送4个字节加上sizeof (uint32_t),所以您可以在发送调用中使用它,您将不再被卡住。否则,您将只发送前两个字节(\0之前的字节),代理将在响应之前等待更多数据。
(有关SOCKS5的更多帮助,请参阅其他地方的大量信息。但上面的代码解决了您发布的代码的特定问题。)
https://stackoverflow.com/questions/49465708
复制相似问题