Click here to Skip to main content
15,880,651 members
Articles / Desktop Programming / MFC
Article

Simple File Transfer Using the Network Development Kit 2.0

Rate me:
Please Sign up or sign in to vote.
4.74/5 (14 votes)
29 Dec 20062 min read 91.4K   4.7K   99   21
NDK File Transfer is a simple demonstration on how to send and receive a file using the NDK 2.0.

Sample Image - NDKFileTransfer.jpg

Introduction

Since I have posted the article NDK 2.0 (Network Development Kit), I received many emails on how to send and receive a file using the NDK. For those who does not know the Network Development Kit 2.0, the NDK 2.0 is a set of simple classes for a client-server architecture using the class CSocket from MFC. With this article, you should be able to understand easily the process about sending and receiving a file with the NDK. This article contains 2 programs: an NDK File Transfer Server and an NDK File Transfer Client.

Algorithm

You can not send a big file with a simple call to over a network, because the socket will freeze. The basic idea is to split a file in many parts and send one part at a time. Here is the process used by these two programs:

  1. The client sends a message to the server asking to download the specific file.
  2. When the server receives the request, it sends the length of the file to upload.
  3. When the client receives the length of the file, it creates the downloaded file.
  4. After that, it sends a message to the server asking for the next part of the file.
  5. When the server receives the request to send the next part, it reads the next part of the file and it sends it to the client.
  6. When the client receives the next part of the file, it writes this part in the downloaded file.
  7. The steps 4-5-6 are repeated until the file will reach the end.

In this project, the buffer size (part of the file) is 1024 bytes. The define BUFFER_SIZE is in the file NDKFileTransferCommon.h.

NDK File Transfer Server

The NDK File Transfer Server provides to the client a list of files in download. To minimize the complexity, the NDK File Transfer Server accepts only one client. Before starting the server, you need to click the Add button in order to choose the files that will be available to download by the client.

The main method in the NDKFileTransferServerDlg is the overridden method OnMessage from the class CNDKServer:

// Called whenever a message is received from a user.
void CNDKFileTransferServerDlg::OnMessage(long lUserId, 
                              CNDKMessage& message)
{
    switch (message.GetId())
    {
    // The client requests a file to download
    case REQUEST_FILE:
        {
            CString strFileName;
            message.GetAt(0, strFileName);

            m_fileUpload.Open(strFileName, CFile::modeRead | 
                              CFile::shareDenyWrite);

            // Send the file length
            message.SetId(START_TRANSFERT);
            message.SetAt(0, (int)m_fileUpload.GetLength());

            SendMessageToUser(lUserId, message);

            CString strActivity;
            strActivity.Format(IDS_UPLOAD_FILE, strFileName);

            AddActivity(strActivity);
        }
        break;

    // The client asks for the next file part to download
    case REQUEST_NEXT_FILE_PART:
        {
            m_unBufferLength = m_fileUpload.Read(m_byteBuffer, BUFFER_SIZE);

            if (m_unBufferLength != 0)
            {
                // Send the file part
                message.SetId(NEXT_FILE_PART);
                message.SetAt(0, m_byteBuffer, m_unBufferLength);
            }
            else
            {
                // When there is no more read bytes,
                // send the acknowledgment that the file is completed
                message.SetId(TRANSFERT_COMPLETED);

                // Close the file if it is opened.
                if (m_fileUpload.m_hFile != INVALID_HANDLE_VALUE)
                    m_fileUpload.Close();

                CString strActivity;
                strActivity.Format(IDS_UPLOAD_FILE_COMPLETED);

                AddActivity(strActivity);
            }

            SendMessageToUser(lUserId, message);
        }
        break;
    }
}

NDK File Transfer Client

The NDK File Transfer Client can download a file from the server. To minimize the complexity, only one file at a time can be downloaded.

The method OnBnClickedButtonDownloadFiles contains the code to ask the server to start the upload of the selected file:

// Open the file
if (m_fileDownload.Open(strFileNameToCreate, 
    CFile::modeCreate | CFile::modeWrite))
{
    // Ask the server to start the download
    CNDKMessage message(REQUEST_FILE);
    message.Add(strFileName);

    SendMessageToServer(message);

    m_bIsDownloading = TRUE;

    UpdateUI();
}

The main method in the NDKFileTransferClientDlg is the overridden method OnMessage from the class CNDKClient:

// Called when a message is received.
void CNDKFileTransferClientDlg::OnMessage(CNDKMessage& message)
{
    switch (message.GetId())
    {
    case SERVER_FILES:
        {
            // Add the file name in the list
            for (int nFileIndex = 0; 
                 nFileIndex < message.GetNbElements(); nFileIndex++)
            {
                CString strFileName;

                message.GetAt(nFileIndex, strFileName);

                m_listServerFiles.AddString(strFileName);
            }

            UpdateUI();
        }
        break;

    case START_TRANSFERT:
        {
            message.GetAt(0, m_nFileSize);
            
            m_progressDownload.SetRange32(0, m_nFileSize);
            
            // Ask the server for the first file part
            message.SetId(REQUEST_NEXT_FILE_PART);
            SendMessageToServer(message);
        }
        break;

    case NEXT_FILE_PART:
        {
            message.GetAt(0, m_byteBuffer, m_unBufferLength);

            m_fileDownload.Write(m_byteBuffer, m_unBufferLength);

            m_progressDownload.OffsetPos(m_unBufferLength);

            // Ask the server for the first file part
            CNDKMessage requestMessage(REQUEST_NEXT_FILE_PART);
            SendMessageToServer(requestMessage);
        }
        break;

    case TRANSFERT_COMPLETED:
        m_fileDownload.Close();

        AfxMessageBox(IDS_FILE_DOWNLOADED_SUCCESSFULLY);

        UpdateUI();
        break;
    }
}

Conclusion

This sample project shows you how a server can send a file to a client. However, this project can be extended to support multiple clients, simultaneous uploads to connected clients, and both ways transfer of a file between the server and the client.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Mirego
Canada Canada
My name is Sébastien Lachance.

I love C# developing Windows Phone and Windows 8 applications.

When I’m not in front of a computer, my hobbies include playing bridge, poker and other card games, biking, reading technology news.

Comments and Discussions

 
QuestionClient issue Pin
webrider2k5-Jul-12 7:33
webrider2k5-Jul-12 7:33 
QuestionFile Transfer trought Internet Pin
Member 820773914-Jun-12 3:46
Member 820773914-Jun-12 3:46 
AnswerRe: File Transfer trought Internet Pin
ArchieCoder14-Jun-12 3:48
ArchieCoder14-Jun-12 3:48 
Questionabout ce Pin
yanzhiwei14726-Jan-12 1:53
yanzhiwei14726-Jan-12 1:53 
AnswerRe: about ce Pin
ArchieCoder26-Jan-12 2:35
ArchieCoder26-Jan-12 2:35 
Questionhow it works with multiple clients Pin
Member 820483921-Dec-11 3:35
Member 820483921-Dec-11 3:35 
QuestionHow can the server support multiple clients? Pin
bz_code27-Mar-08 7:01
bz_code27-Mar-08 7:01 
GeneralNDKFileTransferServer Pin
Member 431713111-Dec-07 23:24
Member 431713111-Dec-07 23:24 
GeneralProgram C# Pin
Member 431713111-Dec-07 23:14
Member 431713111-Dec-07 23:14 
QuestionVB.NET Pin
BIRDENT19-Nov-07 11:44
BIRDENT19-Nov-07 11:44 
GeneralStep by step instructions Pin
rootdial27-Aug-07 1:35
rootdial27-Aug-07 1:35 
Hi,

another nice code/idea you got. Thanks for that one Wink | ;-)

Maybe you should, for the not so experienced programmer,
add a step by step instruction, how to implement the
classes and how to use them (that would be 5 starsWink | ;-) )

I agree, that this could be a nice one to add to NDK 2.0

Looking forward to read new articles from you.
Keep the good work upLaugh | :laugh:

GeneralDefault Server IP 127.0.0.1 Pin
Behzad Ebrahimi26-Aug-07 23:21
Behzad Ebrahimi26-Aug-07 23:21 
GeneralTy for this Pin
ZProgramer2-Aug-07 1:29
ZProgramer2-Aug-07 1:29 
Questioncan you add it to NDK2.0 ?you always give us happy! Pin
processjudger8831-Jan-07 4:15
processjudger8831-Jan-07 4:15 
Questionslow Pin
sergiols2-Jan-07 3:47
sergiols2-Jan-07 3:47 
AnswerRe: slow Pin
ArchieCoder2-Jan-07 4:37
ArchieCoder2-Jan-07 4:37 
GeneralRe: slow Pin
pj220_200618-Dec-11 19:34
pj220_200618-Dec-11 19:34 
GeneralRe: slow Pin
ArchieCoder19-Dec-11 3:09
ArchieCoder19-Dec-11 3:09 
GeneralRe: slow Pin
pj220_200619-Dec-11 15:11
pj220_200619-Dec-11 15:11 
GeneralRe: slow Pin
ArchieCoder19-Dec-11 15:13
ArchieCoder19-Dec-11 15:13 
GeneralRe: slow Pin
AmitGajjar23-Jan-12 19:58
professionalAmitGajjar23-Jan-12 19:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.