WxTFTP

From WxWiki

Jump to: navigation, search

Contents

[edit] wxTFTP

wxTFTP is a class which implements the trivial file transfer protocol, as described in RFCs 1350, 1782, 1783 and 1784. TFTP uses UDP to transfer files and does not have the capability to retreive directory listings. This class can be used to implement a client or a server.

[edit] Client

One can retreive a file by one of two ways:

  • Call GetFile(wxIPV4address server, wxString & filename), which requests the file. After this, use <tt>Read(char * buffer, size_t len)</tt> to get the contents of the file.
  • Call <tt>GetInputStream(wxString & uri)</tt>, with tftp://server/filename as the URI.

To put a file on a server, use <tt>PutFile</tt>, <tt>Write</tt> and <tt>GetOutputStream</tt>.

[edit] Server

To implement a server, you have to overload <tt>HandleRead</tt> and <tt>HandleWrite</tt> and call <tt>HandlePacket(true)</tt> repeatedly.

<tt>HandlePacket(true)</tt> should be continiously called, indicating the incoming packet will be the first of a session. When a request comes in, the port number is changed. This because the TFTP specification says that the server must react on a different port than which it is listening on. After this, <tt>HandleRequest</tt> is called, which will be typically overloaded by the server program. The task of this function is to send the file to the client (using <tt>Write</tt>), send an error (<tt>SendError</tt>), or other things. When this function returns, the port is switched back and the program waits for the next request.

[edit] Functions

=== wxTFTP() === [#wxTFTP]

Default constructor. Default settings:

  • random port
  • blocksize of 512 bytes
  • octet mode
  • 1 second timeout

=== wxTFTP(wxDatagramSocket * sock) === [#wxTFTPsock]

Uses <tt>sock</tt> to send datagrams to the server.

=== ~wxTFTP() === [#dwxTFTP]

Default destructor.

=== void Reset() === [#Reset]

Sets default settings, described above in <tt>wxTFTP()</tt>.

=== bool GetFile(wxIPV4address server, wxString &filename) === [#GetFile]

Requests a file from the server. Returns true on success, false on failure. Use <tt>Read()</tt> to retreive the file.

=== bool PutFile(wxIPV4address server, wxString & filename) === [#PutFile]

Request to put a file on the server. Returns true on success, false on failure. Use <tt>Write()</tt> to put the file on the server.

=== bool Abort() === [#Abort]

Abort transfer in progress, if there is one. This sends an error packet to the server.

=== wxTFTPError GetError() === [#GetError]

Gets the last error. See wxProtocol::GetError().

=== void SetMode(wxTFTPMode mode) === [#SetMode]

Sets the mode (wxTFTP_OCTET or wxTFTP_NETASCII). Netascii presumably alters end-of-line characters.

=== wxTFTPMode GetMode() === [#GetMode]

Returns the mode (see SetMode).

=== void SetBlockSize(size_t newsize) === [#SetBlockSize]

Sets the block size in bytes. Default is 512, as specified by RFC1350. Increasing blocksize leads to better performance on reliable networks. Blocksize must be between 8 and 65464, inclusive. Blocksize can not be set by a server and a server may ignore the blocksize requested by the client.

=== size_t GetBlockSize() === [#GetBlockSize]

Returns the blocksize in bytes. See SetBlockSize.

=== void SetTimeout(int timeout) === [#SetTimeout]

Sets timeout in seconds. This is the time after which a datagram is considered lost and is resend. Should be at least the round-trip delay.

=== int GetTimeout() === [#GetTimeout]

Returns the timeout in seconds.

=== void SetTransferSize(int tsize) === [#SetTransferSize]

Set the filesize in octets (e.g. bytes). Only useful when sending. When receiving, setting the filesize to any other value than 0 (zero) may confuse the server.

=== int GetTransferSize() === [#GetTransferSize]

Returns the transfer size in bytes, or -1 if unknown.

=== size_t Read(void * buffer, size_t len) === [#Read]

Read data and returns the amount actually read.

=== size_t Write(const void * buffer, size_t len) === [#Write]

Writes data and returns the amount actually written. Call <tt>Flush()</tt> when done.

=== bool Flush() === [#Flush]

This flushes the buffer and writes the remaining data. Note that this automatically ends the transfer. Use this if you are done writing (e.g. end of file).

=== wxInputStream * GetInputStream(const wxString & uri) === [#GetInputStream]

Returns an wxInputStream with the contents of the file indicated by <tt>uri</tt>, which is of the form <tt>tftp://server/filename</tt>.

=== wxOutputStream * GetOutputStream(const wxString & uri) === [#GetOutputStream]

Returns a wxOutputStream, which can be used to write a file indicated by <tt>uri</tt> to the server.

Personal tools