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.
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.
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.
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.
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
methods in Dlg file of Dialog based application.
void CServer::OnAccept(int nErrorCode)
void CServer::OnClose(int nErrorCode)
void CServer::OnReceive(int nErrorCode)
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.
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
char *pBuf =new char;
AfxMessageBox("Could not Recieve");
DWORD dwLength = 256;
CString strPath = "Client Message";
strPath += "::";
strPath += 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.