[bittorrent] compact ip problem

Ryan Walklin ryanwalklin at gmail.com
Wed Aug 22 23:05:35 EDT 2007


Hi,

I'm unfamiliar with Java's network code, but surely you want to write the
response to the client with a raw data socket, rather than with HTTP? The
peer wire protocol is used instead of HTTP, rather than on top of it. You
only use HTTP when communicating with trackers. In any case, you should
probably be sending octet-stream rather than text/plain.

Apologies if the above is incorrect, I don't know Java well, and the methods
for sending below might not be introducing HTTP headers.

Regards,

Ryan

On 8/23/07, Dazen D <dazensix at gmail.com> wrote:
>
> Ben, Andre, thanks for your replies.
>
> Ben, inetAddress.getAddress() returns a byte array,
> which is expressed by "byte[]". I'll check the
> endianness of the data, but i think that java deals
> with that using different types (short, int, long, etc).
>
> Andre, i omitted some portions of the code, and didn't
> realize that it made the code confuse. The full code
> is:
>
>     public byte[] getCompactIP(String ip, int port) {
>
>         byte[] rawAddress = null;
>         try {
>           InetAddress inetAddress = InetAddress.getByName(ip);
>           rawAddress = inetAddress.getAddress();
>         }
>         catch(UnknownHostException e) {
>           return null;
>         }
>         if (rawAddress.length == 4) {
>             byte[] compactIP = new byte[6];
>             for (int i = 0; i < 4; i++) {
>                 compactIP[i] = rawAddress[i];
>             }
>             compactIP[4] = (byte)(port >> 8);
>             compactIP[5] = (byte)(port & 0xff);
>
>             return compactIP;
>         }
>         return null;
>     }
>
>
> I tried your suggestion and changed the bitwise operator
> but the outputed bytes didn't change, and the client
> continues to not recognize the data. For example, for
> port 28387 the bytes generated are "nã", in both cases.
>
> Another thing, when i send the response to the client
> i set the response using:
>
>     OutputStream outputStream = httpServletResponse.getOutputStream();
>     httpServletResponse.setContentType("text/plain");
>     (write bytes to output)
>
> Should the content type be different?
>
> Thanks again.
>
>
> On 8/22/07, André <ghost.ued at gmail.com> wrote:
> > "You want to save each section of the
> > IP address as a unsigned 8 bit integer. "
> >
> > actually, in a 32 bit integer xD
> >
> > Dazen, the operator you need is not >>. It's >>> .
> >
> >  Look for java bitwise operators, and you will see the difference.
> >
> >
> > i did not understand your code, but the size seems right: each pair Ip
> > and port uses 6 bytes.
> >
> > here is my code:
> >
> > byte[] ipembytes =InetAddress.getByName(ip);
> >
> >                       peerByteResponse[ actualLength] = ipembytes[0];
> >                       peerByteResponse[ actualLength+1] = ipembytes[1] ;
> >                       peerByteResponse[ actualLength+2] = ipembytes[2] ;
> >                       peerByteResponse[ actualLength+3] = ipembytes[3] ;
> >                       peerByteResponse[actualLength+4]= (byte)(port
> >>>8);
> >                       peerByteResponse[actualLength+5]= (byte)port;
> >
> > (peerByteResponse is a bytre array with the peers string)
> > _______________________________________________
> > BitTorrent mailing list
> > BitTorrent at lists.ibiblio.org
> > http://lists.ibiblio.org/mailman/listinfo/bittorrent
> >
> _______________________________________________
> BitTorrent mailing list
> BitTorrent at lists.ibiblio.org
> http://lists.ibiblio.org/mailman/listinfo/bittorrent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ibiblio.org/pipermail/bittorrent/attachments/20070823/154aec92/attachment.html 


More information about the BitTorrent mailing list