Click here to Skip to main content
Click here to Skip to main content

Simple File Transfer Using the Network Development Kit 2.0

, 29 Dec 2006
Rate this:
Please Sign up or sign in to vote.
NDK File Transfer is a simple demonstration on how to send and receive a file using the NDK 2.0.

Sample Image - NDKFileTransfer.jpg


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.


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 | 

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

            SendMessageToUser(lUserId, message);

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


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

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

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

                CString strActivity;


            SendMessageToUser(lUserId, message);

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);


    m_bIsDownloading = TRUE;


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);



            message.GetAt(0, m_nFileSize);
            m_progressDownload.SetRange32(0, m_nFileSize);
            // Ask the server for the first file part

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

            m_fileDownload.Write(m_byteBuffer, m_unBufferLength);


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





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.


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


About the Author

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.
Follow on   Twitter

Comments and Discussions

QuestionClient issue Pinmemberwebrider2k5-Jul-12 8:33 
QuestionFile Transfer trought Internet PinmemberMember 820773914-Jun-12 4:46 
AnswerRe: File Transfer trought Internet PinmemberSebastien_Lachance14-Jun-12 4:48 
Questionabout ce Pinmemberyanzhiwei14726-Jan-12 2:53 
AnswerRe: about ce PinmemberSebastien_Lachance26-Jan-12 3:35 
Questionhow it works with multiple clients PinmemberMember 820483921-Dec-11 4:35 
QuestionHow can the server support multiple clients? PinmemberMember #452162027-Mar-08 8:01 
GeneralNDKFileTransferServer PinmemberMember 431713112-Dec-07 0:24 
GeneralProgram C# PinmemberMember 431713112-Dec-07 0:14 
QuestionVB.NET PinmemberBIRDENT19-Nov-07 12:44 
GeneralStep by step instructions Pinmemberrootdial27-Aug-07 2:35 
GeneralDefault Server IP PinmemberBehzad Ebrahimi27-Aug-07 0:21 
GeneralTy for this PinmemberProgramerManiac2-Aug-07 2:29 
Questioncan you add it to NDK2.0 ?you always give us happy! Pinmemberprocessjudger8831-Jan-07 5:15 
Questionslow Pinmembersergiols2-Jan-07 4:47 
why the transfer is too slow?Sniff | :^)
Eg.: 2 mb in 1 min
AnswerRe: slow PinmemberSebastien_Lachance2-Jan-07 5:37 
GeneralRe: slow Pinmemberpj220_200618-Dec-11 20:34 
GeneralRe: slow PinmemberSebastien_Lachance19-Dec-11 4:09 
GeneralRe: slow Pinmemberpj220_200619-Dec-11 16:11 
GeneralRe: slow PinmemberSebastien_Lachance19-Dec-11 16:13 
GeneralRe: slow Pinmemberamitgajjar23-Jan-12 20:58 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 29 Dec 2006
Article Copyright 2006 by ArchieCoder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid