Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

ServerClient Communication With Hide Dialog Functionality

1.07/5 (17 votes)
6 Jul 20073 min read 1   43  
Communication between Server and Client and Hide Dialog in MFC

Introduction

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.

Background

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
 if(nErrorCode==0)
 {
  ((CSocket_Connection_ServerDlg*)m_pDlg)->OnAccept();  
 }
 CAsyncSocket::OnAccept(nErrorCode);
}

void CServer::OnClose(int nErrorCode) 
{
 // TODO: Add your specialized code here and/or call the base class
 if(nErrorCode==0)
 {
  ((CSocket_Connection_ServerDlg*)m_pDlg)->OnClose();
 }

 CAsyncSocket::OnClose(nErrorCode);
}

void CServer::OnReceive(int nErrorCode) 
{
 // TODO: Add your specialized code here and/or call the base class
 if(nErrorCode==0)
 {
  ((CSocket_Connection_ServerDlg*)m_pDlg)->OnReceive();  
 }

 CAsyncSocket::OnReceive(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) 
{
 if(!visible)
  lpwndpos->flags &= ~SWP_SHOWWINDOW;
 
 CDialog::OnWindowPosChanged(lpwndpos);
}

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

void CServer_Application_FileTransferDlg::OnOK() 
{
 UpdateData(true);
 if( m_PortNumber < 1024 || m_PortNumber >10000)
  AfxMessageBox("Please Enter Valid Port Number");
 else
 {
  m_Listener.Create(m_PortNumber);
  if(m_Listener.Listen()==FALSE)
  {
   AfxMessageBox("Unable to Listen on that port,please try another port");
   m_Listener.Close(); 
   exit(0);
  }   
  visible = false;
  ShowWindow(SW_HIDE);
 }
}

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;

iLen=m_Connected.Receive(pBuf,60);
if(iLen==SOCKET_ERROR)
{
 AfxMessageBox("Could not Recieve");
}
else
{
    pBuf[iLen]=NULL;
 strData=pBuf;

 DWORD dwLength = 256;
 char pathbuff[256];
 GetCurrentDirectory(dwLength,pathbuff);

 CString strPath = "Client Message";
 strPath += "::";
 strPath += pBuf;
 AfxMessageBox(strPath);

 strData.Empty();
 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?

History

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

License

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