Development of the program of file exchange between local network users

From Bauman National Library
This page was last modified on 15 January 2016, at 10:08.

The aim of this course work is to develop a program to transfer files between any two computers connected via a LAN. The program should:

  • Provide user authorization
  • Transfer data between users
  • Keep a log of events
  • Handle error conditions

The course work was done by IU7-73 students Ivanov I.V. and Stolyarenko I.V.

Introduction

During development the C# programming language for the .NET Framework platform was used. It was chosen because of the rich standard library platform.

When run, the application connects to the authorization server, and then in the appeared window user should enter login and password. After authorization there is no other interaction with the server.

After logging in, the user must connect to a multicast channel. Multicast is used for managing the list of active users without the need for server requests. Every N seconds (the period is specified in the configuration file) in a multicast group is sent the message "I'm alive"; if the client does not send such messages for 3 * N seconds, it is considered that the client is disconnected due to timeout.

List of active users is formed via their IP-addresses. Choosing the correct address, the user can select a file and initiate its transmission.

The application processes the following error situations:

  1. Connection break during file transfer (including the cancelation by the receiving party);
  2. Entering an incorrect username and / or password;
  3. Lack of disk space for the reception of the file.

User interface

User interface

The main form of the application implements the IUIWorker interface, which provides the following methods:

void Report(string message)

Shows the user a message box with the message.

string GetFileName(string ipAddress, string suggestedFileName)

Informs the user about the transfer of a file with the specified name, and prompts the user to select the full path on which the file must be saved.

void UpdateStatus(string message)

Updates the status line on the form.

void UpdateProgress(int value)

Updates the value of the file transfer progress bar.

Use of multicast

For multicast queries sending the class MulticastManager is used. When working with the instantiated manager following methods are used:

public void JoinMulticast()

Adds the client to the multicast group. Reads multicast address and port from the configuration file; creates a socket to receive and send data; a timer is started, which once in N seconds sends message "alive" to the multicast group.

public void LeaveMulticast()

Disconnects the client from the multicast group. Also sends a message "disconnected" to the group, so that other users are immediately informed abount a disconnected client without waiting for the timeout.

public delegate void ConnectionHandler(AddressState state);
public event ConnectionHandler Connection;

Event announcing the arrival of message from another user. If the IP-address of the message sender corresponds to IP-address of this computer in a network, the event is not called.

Sending file

To send a file, the class TcpFileSender is used. Following methods are used:

public TcpFileSender(IPAddress address, IUIWorker uiWorker)

Creates an instance of the class based on IP-address of the recipient and the interface form. Creates a socket for streaming data over TCP on the local network.

public bool SendFile(string fileName)

Transfers a file with the specified name. It is carried out in four stages:

  1. Calculation and sending file checksum
  2. Sending the file size
  3. Sending the file name
  4. Sending the file itself

In case of failure at any stage the function returns false, otherwise - true.

Receiving file

To receive a file a combination of two classes is used: TcpFileListener and TcpFileReceiver. The former is used when waiting for incoming connection and file transfer request, the last is to receive data itself.

Methods used in TcpFileListener:

public TcpFileListener(IUIWorker uiWorker)

Creates an instance of the class based on user interface. Creates a socket for streaming protocol TCP, waiting for incoming connections from any addresses in the local network.

After receiving the connection the TcpFileReceiver is instantiated and receives information about the file (checksum, size, file name). If there is not enough space to save the file, you will get a message and the reception of the file will not be initiated.

Methods used in TcpFileReceiver:

public TcpFileReceiver(Socket acceptedSocket, IUIWorker uiWorker)

Creates an instance of the class based on the received connection socket and user interface. Hash sum of the file is received first, then its size and name. Following the successful check of each of the parameters, responds with a confirmation message.

public bool GetFile (string filename)

Receives file (with byte blocks in accordance to the protocol TCP) and saves it on the specified path. After receiving the file its checksum is compared with the hash sent previously; in the case of matching returns true, otherwise - false.

In case of disconnecting, SocketException is caught within the functions and information message is shown.

Logging

For event logging the library NLog is used, which is the de facto standard for event log entries in the .NET Framework applications. The library allows to use messages of different levels of importance (trace, debug, information, warning, error, and many others).

Format and output style of data are specified in the configuration file Nlog.config. The library features a large number of settings: for example, messages can be written to a disk file, to a database, or displayed on the control form. Additionally, the user may leave a message only for messages of a particular importance level (e.g., only for errors).

An example of configuration file:

1]  <targets>
2]    <target xsi:type="File" name="fileLogging" fileName="${shortdate}.log"
3]            layout="${longdate} ${message}" />
4]  </targets>
5]
6]  <rules>
7]    <logger name="*" minlevel="Debug" writeTo="fileLogging" />
8]  </rules>

In line 2, we specify that the recording "target" is a file, name the "target" and specify the file path. In line 3, we specify the format in which the message will be displayed. In line 7 we define a rule that any logger object should be forwarded message with level not lower than "debug" to the target "fileLogging".

To use the library, one should use an instance of the class Logger, which can be obtained by calling the static function LogManager.GetCurrentClassLogger (). Example of use:

private static readonly Logger log = LogManager.GetCurrentClassLogger();
...
catch (ObjectDisposedException ex)
{
    log.Debug( " > Ожидаемое исключение при уничтожении сокета." + Environment.NewLine + ex );
}
catch (Exception ex)
{
    log.Error( " ! Ошибка при получении сообщения!" + Environment.NewLine + ex);
}

Authorization

The authorization application uses the WCF framework, which abstracts the entire level of messaging between multiple applications. In the authorization server application interface IAutenthificator is defined:

[ServiceContract]
public interface IAutenthificator
{
    [OperationContract]
    bool Authentificate(string login, byte[] password);
}

For the client Visual Studio autogenerates the code of class that serializes the input data, send it to the server and deserializes response. To check the authentication data on the server-side MS SQL database is used, which stores the user ID, name and the password SHA-1 hash. To work with the database ORM Entity Framework 6 is used.

Implementation of the Authentificate method on the server-side is to find the user with corresponding login in the database, and the compare received hash with a database one. The SHA-1 hash of the password is calculated on the client side before passing to the method Authentificate. Username and password check is performed once at the start of the application, IP-address of the authentication server is stored in the configuration file. If connection to the authentication server could not be established, the user will be denied the use of the application.