Click here to Skip to main content
12,818,664 members (28,768 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 5 Jun 2008

Simple client-server UDP socket program

, 5 Jun 2008 GPL3
Rate this:
Please Sign up or sign in to vote.
A simple MFC application for sending and receiving UDP datagrams.


This is a simple application demonstrating a UDP client server in MFC. There are many examples related to TCP, but very few for UDP. So, I thought of making a simple application and uploading it here.


The main dialog acts as a server. The server will keep on waiting for data on a particular port, it receives the data and then displays it. The client is started on clicking a Start button of the main dialog. It can send data to the server. The server then receives the data and displays it.

Using the code

The main dialog class, CSimpleUDPDlg, acts as a server. A thread is started in OnInitDialog() of the main dialog to receive the data sent by the client.

Code snippets of CSimpleUDPDlg.cpp and CClientSend.cpp are shown below:

#include "afxsock.h" 
//Add global variables 
unsigned long id1; 
// Definition of Thread ReceiveData shown below. 
UINT ReceiveData(LPVOID pParam)
    CSimpleUDPDlg *dlg=(CSimpleUDPDlg*)pParam;
    CSocket echoServer;  

     // Create socket for sending/receiving datagrams
     if (echoServer.Create(514, SOCK_DGRAM, NULL)== 0) {
        AfxMessageBox("Create() failed");

    for(;;) { // Run forever

    // Client address
    SOCKADDR_IN echoClntAddr; 

    // Set the size of the in-out parameter
    int clntAddrLen = sizeof(echoClntAddr);

    // Buffer for echo string
    char echoBuffer[ECHOMAX]; 
    // Block until receive message from a client
    int recvMsgSize = echoServer.ReceiveFrom(echoBuffer, 
      ECHOMAX, (SOCKADDR*)&echoClntAddr, &clntAddrLen, 0);
    if (recvMsgSize < 0) {
      AfxMessageBox("RecvFrom() failed");

BOOL CSimpleUDPDlg::OnInitDialog()

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
        CString strAboutMenu;
        if (!strAboutMenu.IsEmpty())
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);

    // Set the icon for this dialog. The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    //start a thread to receive data send by client 
    // TODO: Add extra initialization here
    return TRUE;  // return TRUE  unless you set the focus to a control

//The client dialog is started in OnStart() function  
void CSimpleUDPDlg::OnStart() 
    // TODO: Add your control notification handler code here
    CClientSend send;

//Code Snippet of CClientSend.cpp is shown below which is a simple dialog class. 
#include "afxsock.h" 

void CClientSend::OnSend() 
    // TODO: Add your control notification handler code here
    AfxSocketInit( NULL);
    CString Buffer;
    int buflen=strlen(Buffer);

    CSocket echoClient;  

    if (echoClient.Create(0,SOCK_DGRAM,NULL) == 0) {
         AfxMessageBox("Create() failed");
    // in place of localhost we can give IP Address of the Particular machine.
    if (echoClient.SendTo(Buffer, buflen,514,
      (LPCSTR)"localhost", 0) != buflen) {
        AfxMessageBox("SendTo() sent a different number of bytes than expected");


Well, this is my first article, and I hope it would be helpful.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Software Developer
India India
I am a software developer working with Visual C++.

You may also be interested in...

Comments and Discussions

Questiontype LPCTSTR in ReplaceSel doesn't like char Pin
Member 127126822-Sep-16 2:29
memberMember 127126822-Sep-16 2:29 
QuestionSimple client-server UDP socket program Pin
eyo609g17-Oct-12 7:12
membereyo609g17-Oct-12 7:12 
GeneralMy vote of 4 Pin
slzaleski23-Sep-10 10:31
memberslzaleski23-Sep-10 10:31 
GeneralGood One Pin
Nyachhyon1234522-Apr-09 3:34
memberNyachhyon1234522-Apr-09 3:34 
GeneralCode Pin
Xeifrank12-Jun-08 12:58
memberXeifrank12-Jun-08 12:58 
AnswerRe: Code Pin
buntyrolln12-Jun-08 19:52
memberbuntyrolln12-Jun-08 19:52 
GeneralRe: Code Pin
Xeifrank16-Jun-08 8:33
memberXeifrank16-Jun-08 8:33 
AnswerRe: Code Pin
buntyrolln16-Jun-08 19:01
memberbuntyrolln16-Jun-08 19:01 
GeneralRe: Code Pin
Madhu_Rani24-Jul-09 0:05
memberMadhu_Rani24-Jul-09 0:05 
Question.aspx wtf? Pin
leppie6-Jun-08 3:25
member leppie 6-Jun-08 3:25 

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 | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 6 Jun 2008
Article Copyright 2008 by buntyrolln
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid