Click here to Skip to main content
12,697,340 members (27,724 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked

ServerClient Communication With Hide Dialog Functionality

, 6 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Communication between Server and Client and Hide Dialog in MFC


This application is posted for beginners. This will help beginner in implementing socket connection using CAsynchronous. How to hide dialog in MFC. If you found this artical only for Hiding Dialog purpose then fide "Hide Dialog" from this articale you will get you solution immediatly. How to establish connection between Dialog and Socket.

There are two way of creating socket in MFC.

  1. CAsynchronous
  2. CScoket

I have choose CAsynchronous one because its supports both TCP and UPD socket connection. Where CScoket only supports TCP connections.


Creating Socket connection using MFC class is easy. But establishing connection between dialog and socket is bit difficult for new baby. Even some time we need to hide that dialog on completion of our activity.

Using the code

I have created this article for beginner that's why I am explaining how to create CAsynchronous Socket connection in MFC. So Intermediate can skip next paragraph.

First of all you need to create Dialog based application from VC++ wizards. Open Visual Studio VC6.0 go to menu File --> New --> Projects (Select Tab) enter project name ex Socket_Connection_Server. When we are speaking Socket Connection this will include.

1. Server 2. Client. Firtly we will create Server.

What is "Server" ? Server is application which always gives answer when some body ask some questions. Whoever is asking it becomes client in computer world.

Programming world Server is doing listening job.It is listening for client request and will answer to that query as per it is programmed. This is generalize meaning of this words.

We will start Creating Socket Connection.

1. Once you have created. Dialog based applicationl you need to create New Class. For that follow this steps. From menu select.

Insert --> New Class --> New Class popup.

Class Infromation:

Name :: "Enter Your Socket Server Name example" CServer.

Base Class :: "Select from items" CAsyncScoket.

This steps meance you have created Sever class derived from CAsyncSocket. All Socket methos can be created using wizard.

Go to ClassView and right click on your created class here in my example right click on CServer. When right click is executed you can see context menu having different options.

Seletct "Add Virtual Functions".

We are creating Server so we need to implement following methods.

1. OnAccept
2. OnClose
3. OnReceive
4. OnSend
5. Receive
6. Send
7. OnConnect

Now Important feature is how you can create connection between Socket and Dialog. For doing this what we need to implement one method in Socket Class which can take Dialog instance. ex.

SetDialog(CDialog *pDlg);

This method will be called from OnInitDialog. There we will pass "this". We also need to create CDialog instance in Socket Class ex

CDialog * m_pDlg;

in CPP file of Server.cpp

CSocket_Connection_ServerDlg is Project file. We also need to define

1. OnAccept()

2. OnClose()

3. OnReceive()

methods in Dlg file of Dialog based application.

void CServer::OnAccept(int nErrorCode) 
 // TODO: Add your specialized code here and/or call the base class

void CServer::OnClose(int nErrorCode) 
 // TODO: Add your specialized code here and/or call the base class


void CServer::OnReceive(int nErrorCode) 
 // TODO: Add your specialized code here and/or call the base class


Hide Dialog ::

How to hide dialog ?

THis I have include here because when I was creating application I need to hide my dialog and I was not know how to hide dialog and then I have search for it so this way how MFC allows us to hide our dialog.

If you want to hide your dialog then you need to define "OnWindowPosChanged". This can done using Wizard. Following setps.

In your porject open "MFC ClassWizard" this can be done using short cut key "Ctrl + W".

Inside Menu go to

View --> ClassWizard

popup with MFC ClassWizard.

MFC ClassWizard --> Class Info 

1. "Message filter" select "Window" in that case this will come in display.

once you have selected window In Class Info tabe now go to Message Maps there select Dlg class name and now you can see WM_WINDOWPOSCHANGED double click on it and this message will be implemented. Then you need to BOOL visible

void CServer_Application_FileTransferDlg::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos) 
  lpwndpos->flags &= ~SWP_SHOWWINDOW;

Remember to set the Language of your code snippet using the Language dropdown.

void CServer_Application_FileTransferDlg::OnOK() 
 if( m_PortNumber < 1024 || m_PortNumber >10000)
  AfxMessageBox("Please Enter Valid Port Number");
   AfxMessageBox("Unable to Listen on that port,please try another port");
  visible = false;

Use the "var" button to to wrap Variable or class names in <code> tags like this.

char *pBuf =new char[60];
CString strData;
int iLen;

 AfxMessageBox("Could not Recieve");

 DWORD dwLength = 256;
 char pathbuff[256];

 CString strPath = "Client Message";
 strPath += "::";
 strPath += pBuf;

 delete pBuf;

Points of Interest

Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?


Keep a running update of any changes or improvements you've made here.


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

chaitanya shah
Technical Lead Tata Consultancy Servcies
India India
I have experience in iOs, Objective C,Java,C++/Vc++ 6.0,vc.Net,MFC,ATL,COM,WTL.

You can contact me on if you have any query.

You may also be interested in...


Comments and Discussions

GeneralGood Work Pin
rilov7-Jul-07 5:05
memberrilov7-Jul-07 5:05 
GeneralRe: Good Work Pin
chaitanya shah9-Jul-07 20:33
memberchaitanya shah9-Jul-07 20:33 
Generalvery useful Pin
eumelmann2-Jul-07 21:15
membereumelmann2-Jul-07 21:15 
GeneralRe: very useful Pin
chaitanya shah9-Jul-07 20:38
memberchaitanya shah9-Jul-07 20:38 
QuestionArticle? Pin
Wes Aday2-Jul-07 5:16
memberWes Aday2-Jul-07 5:16 
AnswerRe: Article? Pin
chaitanya shah9-Jul-07 20:37
memberchaitanya shah9-Jul-07 20:37 
GeneralNot an article Pin
John Simmons / outlaw programmer2-Jul-07 1:28
mvpJohn Simmons / outlaw programmer2-Jul-07 1:28 
GeneralRe: Not an article Pin
chaitanya shah3-Jul-07 19:23
memberchaitanya shah3-Jul-07 19:23 
GeneralRe: Not an article Pin
chaitanya shah9-Jul-07 20:35
memberchaitanya shah9-Jul-07 20:35 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 7 Jul 2007
Article Copyright 2007 by chaitanya shah
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid