XanTools.FtpServer
3.0.0
dotnet add package XanTools.FtpServer --version 3.0.0
NuGet\Install-Package XanTools.FtpServer -Version 3.0.0
<PackageReference Include="XanTools.FtpServer" Version="3.0.0" />
<PackageVersion Include="XanTools.FtpServer" Version="3.0.0" />
<PackageReference Include="XanTools.FtpServer" />
paket add XanTools.FtpServer --version 3.0.0
#r "nuget: XanTools.FtpServer, 3.0.0"
#:package XanTools.FtpServer@3.0.0
#addin nuget:?package=XanTools.FtpServer&version=3.0.0
#tool nuget:?package=XanTools.FtpServer&version=3.0.0
XanFTP: Custom Server-Client File Transfer Protocol
XanFTP is a C# implementation of a non-standard, lightweight file transfer protocol (FTP). It enables efficient, asynchronous file transfers between a client and a server with extensibility for advanced use cases.
Key Features
- Custom Protocol: A simplified and extendable file transfer protocol.
- Server-Client Architecture: Facilitates bidirectional file transfers.
- Dynamic Metadata: Attach metadata to file operations for advanced scenarios.
- Asynchronous Design: Optimized for performance and scalability.
- Extensible API: Integrate custom handlers, file options, and serialization methods.
Table of Contents
Installation
Install XanFTP via NuGet:
dotnet add package XanFTP
Getting Started
Prerequisites
- .NET 6.0 or later
Basic Setup
Server : Instantiate and start the server to listen for client connections.
Client : Connect to the server and initiate file operations.
Detailed API Reference
Core Classes
XanFtpServer Manages server-side operations such as accepting client connections and handling file transfers.
Constructor :
public XanFtpServer(IPEndPoint localEndPoint)
Initializes the server with a specified local endpoint.Methods :
Start()
: Starts the server with a default backlog size.Start(int backlog)
: Starts the server with a specified backlog size.Stop()
: Stops the server from listening for connections.AcceptConnectionAsync(ServerAcceptHandler acceptHandler, CancellationToken cancellationToken = default)
: Accepts incoming client connections and processes file transfer requests. XanFtpClient Handles client-side operations such as sending and requesting files.
Constructor :
public XanFtpClient()
Methods :
Connect(IPEndPoint endPoint)
: Connects to a server endpoint.SendFileAsync(FileSendOptions options, Dictionary<string, dynamic>? additionalData, CancellationToken cancellationToken = default)
: Sends a file to the server.RequestFileAsync(FileAcceptOptions options, Dictionary<string, dynamic>? additionalData, CancellationToken cancellationToken = default)
: Requests a file from the server.
Session Management
FileSession Base class for managing file-related sessions.
- Properties :
NetworkStream
: The stream used for communication.FileName
: The name of the file being transferred.AdditionalData
: Metadata associated with the session.AcceptHandler
: Delegate for handling server-side permissions.OperationType
: The type of operation (e.g., send or request file).FileOptions
: Options for file transfer. SendFileSession Manages file-sending sessions and extendsFileSession
.
- Properties :
FileSegments
: A collection of file segments to be sent. AcceptFileSession Manages file-accepting sessions and extendsFileSession
.
Options and Metadata
FileOptions Base class for file-related options.
Properties :
FilePath
: The path to the file being transferred. FileSendOptions Options for sending files.
Properties :
MaxBufferSize
: Maximum buffer size for file segments. FileAcceptOptions Options for accepting files.
Properties :
OutputFile
: Path to save the accepted file.
Helper Methods
XanFtpHelper Provides static methods for handling file transfers.
- Methods :
SendFileAsync(SendFileSession session, CancellationToken cancellationToken = default)
: Sends a file in segments to a server.AcceptFileAsync(AcceptFileSession acceptFileSession, CancellationToken cancellationToken = default)
: Accepts and writes a file to a specified location.
Examples
Server Example
var server = new XanFtpServer(new IPEndPoint(IPAddress.Any, 12345));
server.Start();
await server.AcceptConnectionAsync((operation, filePath, additionalData) =>
{
Console.WriteLine($"Operation: {operation}, File: {filePath}");
return new FileAcceptOptions { OutputFile = "received_file.txt" };
});
Client Example
var client = new XanFtpClient();
client.Connect(new IPEndPoint(IPAddress.Loopback, 12345));
// Send a file to the server
await client.SendFileAsync(new FileSendOptions
{
FilePath = "file_to_send.txt",
MaxBufferSize = 4096
}, null);
// Request a file from the server
await client.RequestFileAsync(new FileAcceptOptions
{
OutputFile = "downloaded_file.txt"
}, null);
Technical Explanation
File Transfer Workflow
Connection Establishment : The client connects to the server using a TCP socket.
Session Creation : Depending on the operation, a
SendFileSession
orAcceptFileSession
is initialized.File Segmentation : The file is divided into segments, which are sent sequentially.
Metadata Handling : Metadata and permissions are exchanged between the client and server to validate the operation.
Completion : Once all segments are transferred, the session is finalized.
JSON Serialization
The DynamicJsonConverter
dynamically maps JSON tokens to corresponding .NET types, enabling seamless metadata handling. Example:
var json = "{\"key\": 123}";
var dictionary = JsonSerializer.Deserialize<Dictionary<string, dynamic>>(json, new JsonSerializerOptions
{
Converters = { new DynamicJsonConverter() }
});
Contributing
We welcome contributions to XanFTP. To contribute:
Fork this repository.
Create a feature branch.
Submit a pull request with detailed explanations of your changes.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- XanTools.StreamExtensions (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.