Click here to Skip to main content
13,867,148 members
Click here to Skip to main content
Add your own
alternative version


96 bookmarked
Posted 17 Dec 2006

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.

You may also be interested in...


Comments and Discussions

QuestionClient issue Pin
webrider2k5-Jul-12 8:33
memberwebrider2k5-Jul-12 8:33 
QuestionFile Transfer trought Internet Pin
Member 820773914-Jun-12 4:46
memberMember 820773914-Jun-12 4:46 
AnswerRe: File Transfer trought Internet Pin
ArchieCoder14-Jun-12 4:48
memberArchieCoder14-Jun-12 4:48 
Questionabout ce Pin
yanzhiwei14726-Jan-12 2:53
memberyanzhiwei14726-Jan-12 2:53 
AnswerRe: about ce Pin
ArchieCoder26-Jan-12 3:35
memberArchieCoder26-Jan-12 3:35 
Questionhow it works with multiple clients Pin
Member 820483921-Dec-11 4:35
memberMember 820483921-Dec-11 4:35 
QuestionHow can the server support multiple clients? Pin
bz_code27-Mar-08 8:01
memberbz_code27-Mar-08 8:01 
GeneralNDKFileTransferServer Pin
Member 431713112-Dec-07 0:24
memberMember 431713112-Dec-07 0:24 
GeneralProgram C# Pin
Member 431713112-Dec-07 0:14
memberMember 431713112-Dec-07 0:14 
QuestionVB.NET Pin
BIRDENT19-Nov-07 12:44
memberBIRDENT19-Nov-07 12:44 
GeneralStep by step instructions Pin
rootdial27-Aug-07 2:35
memberrootdial27-Aug-07 2:35 
GeneralDefault Server IP Pin
Behzad Ebrahimi27-Aug-07 0:21
memberBehzad Ebrahimi27-Aug-07 0:21 
GeneralTy for this Pin
ProgramerManiac2-Aug-07 2:29
memberProgramerManiac2-Aug-07 2:29 
Questioncan you add it to NDK2.0 ?you always give us happy! Pin
processjudger8831-Jan-07 5:15
memberprocessjudger8831-Jan-07 5:15 
Questionslow Pin
sergiols2-Jan-07 4:47
membersergiols2-Jan-07 4:47 
AnswerRe: slow Pin
ArchieCoder2-Jan-07 5:37
memberArchieCoder2-Jan-07 5:37 
GeneralRe: slow Pin
pj220_200618-Dec-11 20:34
memberpj220_200618-Dec-11 20:34 
GeneralRe: slow Pin
ArchieCoder19-Dec-11 4:09
memberArchieCoder19-Dec-11 4:09 
GeneralRe: slow Pin
pj220_200619-Dec-11 16:11
memberpj220_200619-Dec-11 16:11 
GeneralRe: slow Pin
ArchieCoder19-Dec-11 16:13
memberArchieCoder19-Dec-11 16:13 
GeneralRe: slow Pin
@AmitGajjar23-Jan-12 20:58
professional@AmitGajjar23-Jan-12 20: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190214.1 | Last Updated 29 Dec 2006
Article Copyright 2006 by ArchieCoder
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid