I' m a learner in visual c++ developing environment.
Here's m problem . . .
------------------------>
I am using a tutorial to make a simple program to send messages to another program using CAsyncSocket class.
I wrote complete application's code as given in the example(in visual studio 6 IDE). I compiled the application and the program run successfully without any errors.
Then I compiled same program in visual studio 2008(Professional Edition) and It also run as expected.
But When I wrote the same program from scratch in visual studio 2008 it showed some misbehavior while receiving data.
The main problem with the code given below is that it sends data(as shown in the code below) but on the receiving application only first byte is received(When project is written from scratch in VS 2008 Pro).
But if application is written in Visual studio 6 then there is no such problem.
I' m totally confused what to do? where is the problem actually residing? Please send any suggestion.
-->I have VC++ 2008 FeaturePack installed.
-->Create an MFC Application named "Sockets" and link MFC as statically.
-->then use files below to make the application.
The individual codes of my complete application's source files is given here:-
#pragma once
class CMySocket : public CAsyncSocket
{
public:
CMySocket();
virtual ~CMySocket();
private:
CDialog* m_pWnd;
public:
void SetParent(CDialog* pWnd);
protected:
virtual void OnAccept(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnClose(int nErrorCode);
virtual void OnReceive(int nErrorCode);
virtual void OnSend(int nErrorCode);
};
#include "stdafx.h"
#include "Sockets.h"
#include "MySocket.h"
#include "SocketsDlg.h"
CMySocket::CMySocket()
: m_pWnd(NULL)
{
}
CMySocket::~CMySocket()
{
}
void CMySocket::SetParent(CDialog* pWnd)
{
m_pWnd = pWnd;
}
void CMySocket::OnAccept(int nErrorCode)
{
if(nErrorCode == 0)
((CSocketsDlg*)m_pWnd)->OnAccept();
}
void CMySocket::OnConnect(int nErrorCode)
{
if(nErrorCode == 0)
((CSocketsDlg*)m_pWnd)->OnConnect();
}
void CMySocket::OnClose(int nErrorCode)
{
if(nErrorCode == 0)
((CSocketsDlg*)m_pWnd)->OnClose();
}
void CMySocket::OnReceive(int nErrorCode)
{
if(nErrorCode == 0)
((CSocketsDlg*)m_pWnd)->OnReceive();
}
void CMySocket::OnSend(int nErrorCode)
{
if(nErrorCode == 0)
((CSocketsDlg*)m_pWnd)->OnSend();
}
#pragma once
#include "afxwin.h"
#include "mysocket.h"
class CSocketsDlg : public CDialog
{
public:
CSocketsDlg(CWnd* pParent = NULL);
enum { IDD = IDD_SOCKETS_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
CButton m_ctlConnect;
CString m_strMessage;
CString m_strName;
int m_iPort;
CListBox m_ctlRecvd;
CListBox m_ctlSent;
int m_iType;
afx_msg void OnRType();
private:
CMySocket m_sListenSocket;
CMySocket m_sConnectSocket;
public:
afx_msg void OnBnClickedBconnect();
void OnAccept(void);
void OnConnect(void);
void OnSend(void);
void OnReceive(void);
void OnClose(void);
afx_msg void OnBnClickedBsend();
afx_msg void OnBnClickedBclose();
};
#include "stdafx.h"
#include "Sockets.h"
#include "SocketsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
CSocketsDlg::CSocketsDlg(CWnd* pParent )
: CDialog(CSocketsDlg::IDD, pParent)
, m_strMessage(_T(""))
, m_strName(_T(""))
, m_iPort(0)
, m_iType(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSocketsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BCONNECT, m_ctlConnect);
DDX_Text(pDX, IDC_EMSG, m_strMessage);
DDX_Text(pDX, IDC_ESERVNAME, m_strName);
DDX_Text(pDX, IDC_ESERVPORT, m_iPort);
DDX_Control(pDX, IDC_LRECVD, m_ctlRecvd);
DDX_Control(pDX, IDC_LSENT, m_ctlSent);
DDX_Radio(pDX, IDC_RCLIENT, m_iType);
}
BEGIN_MESSAGE_MAP(CSocketsDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RCLIENT, &CSocketsDlg::OnRType)
ON_BN_CLICKED(IDC_BCONNECT, &CSocketsDlg::OnBnClickedBconnect)
ON_BN_CLICKED(IDC_BSEND, &CSocketsDlg::OnBnClickedBsend)
ON_BN_CLICKED(IDC_BCLOSE, &CSocketsDlg::OnBnClickedBclose)
END_MESSAGE_MAP()
BOOL CSocketsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
m_iType = 0;
m_strName = "loopback";
m_iPort = 4000;
UpdateData(FALSE);
m_sConnectSocket.SetParent(this);
m_sListenSocket.SetParent(this);
return TRUE;
}
void CSocketsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CSocketsDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CSocketsDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CSocketsDlg::OnRType()
{
UpdateData(TRUE);
if(m_iType == 0)
m_ctlConnect.SetWindowTextW(_T("Connect"));
else
m_ctlConnect.SetWindowTextW(_T("Listen"));
}
void CSocketsDlg::OnBnClickedBconnect()
{
UpdateData(TRUE);
GetDlgItem(IDC_BCONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_ESERVNAME)->EnableWindow(FALSE);
GetDlgItem(IDC_ESERVPORT)->EnableWindow(FALSE);
GetDlgItem(IDC_STATICNAME)->EnableWindow(FALSE);
GetDlgItem(IDC_STATICPORT)->EnableWindow(FALSE);
GetDlgItem(IDC_RCLIENT)->EnableWindow(FALSE);
GetDlgItem(IDC_RSERVER)->EnableWindow(FALSE);
GetDlgItem(IDC_STATICTYPE)->EnableWindow(FALSE);
if(m_iType == 0)
{
m_sConnectSocket.Create();
m_sConnectSocket.Connect(m_strName, m_iPort);
}
else
{
m_sListenSocket.Create(m_iPort);
m_sListenSocket.Listen();
}
}
void CSocketsDlg::OnAccept(void)
{
m_sListenSocket.Accept(m_sConnectSocket);
GetDlgItem(IDC_EMSG)->EnableWindow(TRUE);
GetDlgItem(IDC_BSEND)->EnableWindow(TRUE);
GetDlgItem(IDC_STATICMSG)->EnableWindow(TRUE);
}
void CSocketsDlg::OnConnect(void)
{
GetDlgItem(IDC_EMSG)->EnableWindow(TRUE);
GetDlgItem(IDC_BSEND)->EnableWindow(TRUE);
GetDlgItem(IDC_STATICMSG)->EnableWindow(TRUE);
GetDlgItem(IDC_BCLOSE)->EnableWindow(TRUE);
}
void CSocketsDlg::OnSend(void)
{
}
void CSocketsDlg::OnReceive(void)
{
char *pBuf = new char[1025];
int iBufSize = 1024;
int iRcvd;
CString strRecvd;
iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize);
if(iRcvd == SOCKET_ERROR)
{
}
else
{
pBuf[iRcvd] = NULL;
strRecvd = pBuf;
m_ctlRecvd.AddString(strRecvd);
UpdateData(FALSE);
}
}
void CSocketsDlg::OnClose(void)
{
m_sConnectSocket.Close();
GetDlgItem(IDC_EMSG)->EnableWindow(FALSE);
GetDlgItem(IDC_BSEND)->EnableWindow(FALSE);
GetDlgItem(IDC_STATICMSG)->EnableWindow(FALSE);
GetDlgItem(IDC_BCLOSE)->EnableWindow(FALSE);
if(m_iType == 0)
{
GetDlgItem(IDC_BCONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_ESERVNAME)->EnableWindow(TRUE);
GetDlgItem(IDC_ESERVPORT)->EnableWindow(TRUE);
GetDlgItem(IDC_STATICNAME)->EnableWindow(TRUE);
GetDlgItem(IDC_STATICPORT)->EnableWindow(TRUE);
GetDlgItem(IDC_RCLIENT)->EnableWindow(TRUE);
GetDlgItem(IDC_RSERVER)->EnableWindow(TRUE);
GetDlgItem(IDC_STATICTYPE)->EnableWindow(TRUE);
}
}
void CSocketsDlg::OnBnClickedBsend()
{
int iLen;
int iSent;
UpdateData(TRUE);
if(m_strMessage != "")
{
iLen = m_strMessage.GetLength();
iSent = m_sConnectSocket.Send(LPCTSTR(m_strMessage), iLen);
if(iSent == SOCKET_ERROR)
{
}
else
{
m_ctlSent.AddString(m_strMessage);
UpdateData(FALSE);
}
}
}
void CSocketsDlg::OnBnClickedBclose()
{
OnClose();
}
#define IDM_ABOUTBOX 0x0010
#define IDD_ABOUTBOX 100
#define IDS_ABOUTBOX 101
#define IDD_SOCKETS_DIALOG 102
#define IDP_SOCKETS_INIT_FAILED 103
#define IDR_MAINFRAME 128
#define IDC_BCONNECT 1000
#define IDC_BCLOSE 1001
#define IDC_BSEND 1002
#define IDC_EMSG 1003
#define IDC_ESERVNAME 1004
#define IDC_ESERVPORT 1005
#define IDC_LSENT 1006
#define IDC_LRECVD 1007
#define IDC_RCLIENT 1008
#define IDC_RSERVER 1009
#define IDC_STATICNAME 1010
#define IDC_STATICPORT 1011
#define IDC_STATICMSG 1012
#define IDC_STATICTYPE 1013
#define IDC_STATICSENT 1014
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 129
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1015
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
#ifndef APSTUDIO_INVOKED
#include "targetver.h"
#endif
#include "afxres.h"
#undef APSTUDIO_READONLY_SYMBOLS
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#ifndef APSTUDIO_INVOKED\r\n"
"#include ""targetver.h""\r\n"
"#endif\r\n"
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"#define _AFX_NO_SPLITTER_RESOURCES\r\n"
"#define _AFX_NO_OLE_RESOURCES\r\n"
"#define _AFX_NO_TRACKER_RESOURCES\r\n"
"#define _AFX_NO_PROPERTY_RESOURCES\r\n"
"\r\n"
"#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
"LANGUAGE 9, 1\r\n"
"#pragma code_page(1252)\r\n"
"#include ""res\\Sockets.rc2"" // non-Microsoft Visual C++ edited resources\r\n"
"#include ""afxres.rc"" // Standard components\r\n"
"#endif\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
IDR_MAINFRAME ICON "res\\Sockets.ico"
IDD_ABOUTBOX DIALOGEX 0, 0, 170, 62
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About Sockets"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
ICON IDR_MAINFRAME,IDC_STATIC,14,14,21,20
LTEXT "Sockets, Version 1.0",IDC_STATIC,42,14,114,8,SS_NOPREFIX
LTEXT "Copyright (C) 2009",IDC_STATIC,42,26,114,8
DEFPUSHBUTTON "OK",IDOK,113,41,50,14,WS_GROUP
END
IDD_SOCKETS_DIALOG DIALOGEX 0, 0, 280, 198
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "Sockets"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
PUSHBUTTON "Connect",IDC_BCONNECT,223,16,50,14
PUSHBUTTON "Close",IDC_BCLOSE,223,35,50,14,WS_DISABLED
PUSHBUTTON "Send",IDC_BSEND,223,58,50,14,WS_DISABLED
EDITTEXT IDC_EMSG,61,57,156,14,ES_AUTOHSCROLL | WS_DISABLED
LTEXT "Server Name :",IDC_STATICNAME,64,15,47,8
LTEXT "Server Port :",IDC_STATICPORT,69,36,42,8
LTEXT "Message :",IDC_STATICMSG,22,59,34,8,WS_DISABLED
EDITTEXT IDC_ESERVNAME,113,14,104,14,ES_AUTOHSCROLL
EDITTEXT IDC_ESERVPORT,113,33,52,14,ES_AUTOHSCROLL
LTEXT "Sent :",IDC_STATIC,36,81,20,8
LTEXT "Received :",IDC_STATIC,21,135,35,8
LISTBOX IDC_LSENT,61,79,212,55,LBS_NOINTEGRALHEIGHT | LBS_NOSEL | WS_VSCROLL
LISTBOX IDC_LRECVD,61,136,212,55,LBS_NOINTEGRALHEIGHT | LBS_NOSEL | WS_VSCROLL
GROUPBOX "Socket Type",IDC_STATICTYPE,7,7,51,47
CONTROL "Client",IDC_RCLIENT,"Button",BS_AUTORADIOBUTTON | WS_GROUP,16,23,34,10
CONTROL "Server",IDC_RSERVER,"Button",BS_AUTORADIOBUTTON,15,38,37,10
END
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x4L
FILETYPE 0x1L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904e4"
BEGIN
VALUE "CompanyName", "TODO: <Company name>"
VALUE "FileDescription", "TODO: <File description>"
VALUE "FileVersion", "1.0.0.1"
VALUE "InternalName", "Sockets.exe"
VALUE "LegalCopyright", "TODO: (c) <Company name>. All rights reserved."
VALUE "OriginalFilename", "Sockets.exe"
VALUE "ProductName", "TODO: <Product name>"
VALUE "ProductVersion", "1.0.0.1"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1252
END
END
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_ABOUTBOX, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 163
TOPMARGIN, 7
BOTTOMMARGIN, 55
END
IDD_SOCKETS_DIALOG, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 273
TOPMARGIN, 7
BOTTOMMARGIN, 191
END
END
#endif // APSTUDIO_INVOKED
STRINGTABLE
BEGIN
IDS_ABOUTBOX "&About Sockets..."
IDP_SOCKETS_INIT_FAILED "Windows sockets initialization failed."
END
#endif // English (U.S.) resources
#ifndef APSTUDIO_INVOKED
#define _AFX_NO_SPLITTER_RESOURCES
#define _AFX_NO_OLE_RESOURCES
#define _AFX_NO_TRACKER_RESOURCES
#define _AFX_NO_PROPERTY_RESOURCES
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE 9, 1
#pragma code_page(1252)
#include "res\Sockets.rc2"
#include "afxres.rc"
#endif
#endif // not APSTUDIO_INVOKED
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h"
class CSocketsApp : public CWinAppEx
{
public:
CSocketsApp();
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
extern CSocketsApp theApp;
#include "stdafx.h"
#include "Sockets.h"
#include "SocketsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
BEGIN_MESSAGE_MAP(CSocketsApp, CWinAppEx)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
CSocketsApp::CSocketsApp()
{
}
CSocketsApp theApp;
BOOL CSocketsApp::InitInstance()
{
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinAppEx::InitInstance();
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
CSocketsDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
}
else if (nResponse == IDCANCEL)
{
}
return FALSE;
}
Manmohan Bishnoi
|