I am trying to implement a bit torrent client.
The first step is decode the torrent file, which I did and here is result:
d8:announce36:http://tracker.mininova.org/announce7:comment41:Auto-generated torrent by Mininova.org CD13:creation datei1212041255e4:infod5:filesld6:lengthi291e4:pathl27:Distributed by Mininova.txteed6:lengthi199784e4:pathl19:the cs song.mp3.mp3eee4:name33:The Counter Strike Song version 212:piece lengthi1048576e6:pieces20:趬oîdÏ9`•×=ü¼e6:locale2:en5:title33:The Counter Strike Song version 2e.
The second step is sending a HTTP GET request to the tracker’s
announce URL with “?” and the following parameters (encoded as above)
appended. This is the part I am struck on. I have been researching how
to send HTTP GET in C; it seem you need first set up a TCP socket and
connect to the tracker server first. I have been doing this:
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
printf("fail create socket");
return 0;
}
char *path = “tracker.mininova.org/announce7”;
struct hostent *hp = gethostbyname(path);
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
&servAddr.sin_addr.s_addr = ((struct sockaddr_in*)(res->ai_addr))->sin_addr.s_addr;
servAddr.sin_port = htons(portNum);
However, gethostbyname keep returning null. What am I doing wrong?
You are calling
gethostbynameon a string that’s not just a domain,"tracker.mininova.org/announce7". You need to be calling it on just the domain,"tracker.mininova.org". You could check theh_errnovalue to figure this out.However, the use of
gethostbynameand related functions should be replaced by use ofgetaddrinfoanyway (which has the same limitations, but will provide better results).