[bittorrent] compact ip problem

Dazen D dazensix at gmail.com
Wed Aug 22 22:59:32 EDT 2007


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
>



More information about the BitTorrent mailing list