Click here to Skip to main content
15,886,137 members
Articles / Desktop Programming / MFC
Article

ServerClient Communication With Hide Dialog Functionality

Rate me:
Please Sign up or sign in to vote.
1.07/5 (17 votes)
6 Jul 20073 min read 24.6K   15   9
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


Written By
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 chaitanya.ce@gmail.com if you have any query.

Comments and Discussions

 
GeneralGood Work Pin
rilov7-Jul-07 4:05
rilov7-Jul-07 4:05 
GeneralRe: Good Work Pin
chaitanya shah9-Jul-07 19:33
chaitanya shah9-Jul-07 19:33 
Generalvery useful Pin
eumelmann2-Jul-07 20:15
eumelmann2-Jul-07 20:15 
GeneralRe: very useful Pin
chaitanya shah9-Jul-07 19:38
chaitanya shah9-Jul-07 19:38 
QuestionArticle? Pin
Wes Aday2-Jul-07 4:16
professionalWes Aday2-Jul-07 4:16 
WTF | :WTF:

Why is common sense not common?

Never argue with an idiot. They will drag you down to their level where they are an expert.

Sometimes it takes a lot of work to be lazy

AnswerRe: Article? Pin
chaitanya shah9-Jul-07 19:37
chaitanya shah9-Jul-07 19:37 
GeneralNot an article Pin
#realJSOP2-Jul-07 0:28
mve#realJSOP2-Jul-07 0:28 
GeneralRe: Not an article Pin
chaitanya shah3-Jul-07 18:23
chaitanya shah3-Jul-07 18:23 
GeneralRe: Not an article Pin
chaitanya shah9-Jul-07 19:35
chaitanya shah9-Jul-07 19: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.