2

私はtftpクライアントをプログラミングしていますが、デフォルトのブロックサイズ(512)を使用している限り、問題なく動作します。しかし、それは学校の課題なので、1430と4300のブロックサイズでもテストする必要があります。

サーバーと最初に通信するときは、次の方法を使用します。

public void setFilename( String s, String mode) {
        byte []a = s.getBytes();
        int i,j,k;
        for ( i=0; i+2<lenght && i<a.length; i++ ) {
            packet[i+2] = a[i];
        }
        packet[i+2] = 0;
        a = mode.getBytes();
        for ( j=0,i++; i<lenght && j<a.length; i++,j++ ) {
            packet[i+2] = a[j];
        }
        packet[i+2] = 0;


    }

読みたいファイル名を設定します。そしてそれはうまく機能します。

しかし、ブロックサイズを定義できるように変更しました。

public void setFilename( String s, String mode, String blockSize ) {
        byte []a = s.getBytes();
        int i,j,k;
        for ( i=0; i+2<lenght && i<a.length; i++ ) {
            packet[i+2] = a[i];
        }
        packet[i+2] = 0;
        a = mode.getBytes();
        for ( j=0,i++; i<lenght && j<a.length; i++,j++ ) {
            packet[i+2] = a[j];
        }
        packet[i+2] = 0;

        a = BLOCKSIZE.getBytes();
        for ( k=0,i++; i<lenght && k<a.length; i++,k++ ) {
            packet[i+2] = a[k];
        }
        packet[i+2] = 0;

        a = blockSize.getBytes();
        for ( k=0,i++; i<lenght && k<a.length; i++,k++ ) {
            packet[i+2] = a[k];
        }
        packet[i+2] = 0;

    }

ここで、BLOCKSIZE = "blksize"(文字列)およびblockSize = 1430(int); 問題は、それが機能しないことです:-/

誰かがブロックサイズを定義する方法を教えてもらえますか?

君たちありがとう :-)

4

2 に答える 2

0

問題は読書の部分にあり、ここではありませんでした。誰かが興味を持っている場合、パケットを構築するための完全なコードは次のとおりです:-)

/ ** *

*TftpPacket-バイト配列をtftpパケットと見なします。
 *
 *

TFTPプロトコル(rfc 1350、2347)に従ったメッセージ形式。 *

注:この実装では、使用されるすべてのJAva文字列に含まれるのは *ASCII文字。そうでない場合、lenght()とgetBytes()は異なる値を返します *予期しない問題が発生する可能性があります...

*tftpリクエストパケット * 2バイト文字列1バイト文字列1バイトオプションオプション(文字列) * ----------------------------------------- + --- --+-+- --- +-+-> * RRQ / WRQ | 01/02 | ファイル名| 0 | モード| 0 | opt1 | 0 | value1 | 0 | ..。 * ----------------------------------------- + --- --+-+- --- +-+-> *モード(ASCII文字/各1バイト): *「netascii」、「octet」、..。 * *2バイト2バイトnバイト * --------------------------------- *データ| 03 | ブロック番号| データ| * --------------------------------- *2バイト2バイト * ------------------- * ACK | 04 | ブロック番号| * -------------------- *2バイト2バイト文字列1バイト * ---------------------------------------- *エラー| 05 | エラーコード| ErrMsg | 0 | * ---------------------------------------- *エラーコード: * 0定義されていません。エラーメッセージ(ある場合)を参照してください。 *1ファイルが見つかりません。 *2アクセス違反。 *3ディスクがいっぱいまたは割り当てを超えました。 *4不正なTFTP操作。 *5不明な転送ID。 *6ファイルはすでに存在します。 *7そのようなユーザーはいません。 *

* /

public class TftpPacket {

// Opcodes
protected static final short RRQ=1;
protected static final short WRQ=2;
protected static final short DATA=3;
protected static final short ACK=4;
protected static final short ERROR=5;
protected static final String BLOCKSIZE = "blksize";

byte []packet;
int lenght;

/**
 *  Builds a view to a byte array as a tftp packet.
 *  The original byte array is the one manipulated by all methods
 */
public TftpPacket( byte []data, int len ) {
    packet = data;
    lenght = len;
}

/**
 *  Gets number at first two bytes of packet
 */
public int getOpcode() {
    return (packet[0]<<8)|(packet[1]&0xff);  //net byte order = Big-Endian
}

/**
 *  Sets first two bytes of packet
 */
public void setOpcode(int code) {
    packet[0] = (byte) ((code>>8)&0xff);
    packet[1] = (byte) (code&0xff);
}

/**
 *  Gets string starting at byte 2
 */
public String getFileName() {
    int i;
    for ( i=2; i<lenght; i++ )
        if (packet[i]==0) //end of string
            return new String(packet, 2, i-2);
    return null;
}

/**
 *  Sets two strings (NUL terminated) starting at byte 2
 */
public void setFilename( String s, String mode, String blockSize ) {
    byte []a = s.getBytes();
    int i,j,k;
    for ( i=0; i+2<lenght && i<a.length; i++ ) {
        packet[i+2] = a[i];
    }
    packet[i+2] = 0;
    a = mode.getBytes();
    for ( j=0,i++; i<lenght && j<a.length; i++,j++ ) {
        packet[i+2] = a[j];
    }
    packet[i+2] = 0;

    a = BLOCKSIZE.getBytes();
    for ( k=0,i++; i<lenght && k<a.length; i++,k++ ) {
        packet[i+2] = a[k];
    }
    packet[i+2] = 0;

    a = blockSize.getBytes();
    for ( k=0,i++; i<lenght && k<a.length; i++,k++ ) {
        packet[i+2] = a[k];
    }
    packet[i+2] = 0;

}

/**
 *  Gets second string in packet
 */
public String getMode(){
    for ( int i=2; i<lenght; i++ )
        if (packet[i]==0) { //end of 1st string
            for ( int j=i+2; j<lenght; j++ )
                if ( packet[j]==0 ) return new String(packet, i+1, j-i-1);
        }
    return null; 
}

/**
 *  Gets number at bytes 2 and 3 of packet (can be block count or error code)
 */
public int getBlockCount() {
    return (packet[2]<<8)|(packet[3]&0xff);  //net byte order = Big-Endian
}

/**
 *  Sets bytes 2 and 3 of packet (can be block count or error code)
 */
public void setBlockCount(int count) {
    packet[2] = (byte) ((count>>8)&0xff);
    packet[3] = (byte) (count&0xff);
}

/**
 *  Sets string (NUL-terminated) starting at byte 4
 */
public void setErrMsg( String s ) {
    byte []a = s.getBytes();
    int i;
    for ( i=0; i<a.length; i++ ) {
        packet[i+4] = a[i];
    }
    packet[i+4] = 0;
}

}

于 2014-04-15T09:59:09.837 に答える
-3

Wiresharkを使用して、パケットがどのように適合され、ワイヤに送信されるかを確認することをお勧めします。そうすれば、TFTPプロトコルをコードに組み込むときに問題をすばやく見つけることができます。

于 2013-03-25T14:43:21.610 に答える