gpt4 book ai didi

c++ - "The requested URL/announce was not found on this server"比特流 HTTP 请求错误

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:09:18 25 4
gpt4 key购买 nike

我正在尝试向 bittorrent 跟踪器服务器发出 HTTP 请求以获取对等列表。

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h> // for gethostbyname()
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
using namespace std;

struct in_addr *atoaddr( const char* address)
{
static struct hostent *host;
static struct in_addr saddr;

// First try nnn.nnn.nnn.nnn form
saddr.s_addr = inet_addr(address);
if (saddr.s_addr != -1)
return &saddr;

host = gethostbyname(address);
if( host )
return (struct in_addr *) *host->h_addr_list;

return 0;
}

int main(){
int numbytes=0;
int m_Port=80;
string m_Host="torrent.ubuntu.com";
int m_Sock;
struct hostent *host;
static struct in_addr saddr;
string buffer;
char* buf;

//HTTP request string
buffer.append("GET http://torrent.ubuntu.com:6969/announce HTTP/1.1\r\n");
buffer.append("Host: torrent.ubuntu.com\r\n");
buffer.append("Accept-Encoding: identity\r\n");
buffer.append("info_hash: %36%be%21%35%ab%cc%f4%03%2f%bb%28%bb%1b%0d%4f%da%f7%49%f8%58\r\n");
buffer.append("peer_id: ABCDEFGHIJKLMNOPQRST\r\n");
buffer.append("port: 6881\r\n");
buffer.append("compact: 1\r\n");
buffer.append("uploaded: 0\r\n");
buffer.append("downloaded: 0\r\n");
buffer.append("left: 987758592\r\n");
buffer.append("event: started\r\n");
buffer.append("\r\n");

in_addr* addr = atoaddr( m_Host.c_str() );
if( !addr )
{
cout<<"Error address\n";
return 1;
}

sockaddr_in address;
memset( (char*)&address, 0, sizeof(address) );
address.sin_family = AF_INET;
address.sin_port = htons( m_Port );
address.sin_addr.s_addr = addr->s_addr;

m_Sock = socket( AF_INET, SOCK_STREAM, 0 );
if(m_Sock<0){
cout<<"Socket error\n";
return 1;
}

if( ::connect( m_Sock, (sockaddr const*)&address, sizeof(address) ) < 0 )
{
cout<<"connect error";
return 1;
}

buf = (char*)buffer.c_str();
numbytes = buffer.length();
while( numbytes > 0 )
{
int n = ::send( m_Sock, buf, buffer.size(), 0 );
std::cout<<"\n\n--HTTP request:\n"<<buf<<std::endl;
if( n<0 )
{
cout<<"number of bytes sent <0\n";
return 1;
}
numbytes -= n;
buf += n;
}

cout<<"\ngetting response: \n";
unsigned char recvBuf[ 2048 ];
int a;
do{
a = recv( m_Sock, (char*)recvBuf, sizeof(recvBuf), 0 );
fwrite(recvBuf, 1, a, stdout);
}while(a!=0);

if(m_Sock>0)
close(m_Sock);

return 0;
}

我总是得到这样的回应:

HTTP/1.1 404 Not Found
Date: Sun, 16 Mar 2014 13:40:10 GMT
Server: Apache/2.2.22 (Ubuntu)
Vary: Accept-Encoding
Content-Length: 294
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /announce was not found on this server.</p>
<hr>
<address>Apache/2.2.22 (Ubuntu) Server at bttracker.debian.org Port 6969</address>
</body></html>

我尝试使用 Google Chrome 的 POSTMAN 扩展来发出相同的请求,我得到的响应如下:

d8:completei12e10:incompletei1e8:intervali1800e5:peers18:[�_�����4&N=;8��e

这是我在从我的 C 代码发出请求时期望看到的。我做错了什么?

最佳答案

我相信您的代码失败是因为您犯了一个小错误:您不应将套接字连接到端口 80,而应连接到端口 6969,然后发送一个 GET/announce 查询,而不是发送GET 方法中的完整 URL。

关于c++ - "The requested URL/announce was not found on this server"比特流 HTTP 请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22436879/

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