본문 바로가기
프로그래밍/MFC

소켓 통신 Client 부분

by 완소루피 2020. 11. 4.
728x90
반응형

Client 컨트롤

///////////////////////////////////////////////////////////////////////////////////////////////////////
//헤더 파일

#pragma once
#include "ConnectSocket.h"
// CClientCtrl
class CClientCtrl : public CWnd
{
 DECLARE_DYNAMIC(CClientCtrl)
private:
 CConnectSocket m_ConnectSocket;
public:
 CClientCtrl();
 virtual ~CClientCtrl();
 void SetSend(CString msg); 
 BOOL Connect(LPCTSTR ipAddr, UINT port);
 void SetServerIPPort(CString ip, UINT port);
 
protected:
 afx_msg LRESULT SetClose(WPARAM wParam, LPARAM lParam);
 afx_msg LRESULT SetReceive(WPARAM wParam, LPARAM lParam);
 afx_msg LRESULT SetConnect(WPARAM wParam, LPARAM lParam);
 
 DECLARE_MESSAGE_MAP()
public:
 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};

 
///////////////////////////////////////////////////////////////////////////////////////////////////////
// cpp 파일
 
// ClientCtrl.cpp : 구현 파일입니다.
//
#include "stdafx.h"
#include "ChartSW.h"
#include "ClientCtrl.h"

// CClientCtrl
IMPLEMENT_DYNAMIC(CClientCtrl, CWnd)
CClientCtrl::CClientCtrl()
{ 
}
CClientCtrl::~CClientCtrl()
{
}

BEGIN_MESSAGE_MAP(CClientCtrl, CWnd)
 ON_MESSAGE(WM_CONNCET,&CClientCtrl::SetConnect)
 ON_MESSAGE(WM_DISCONNCET,&CClientCtrl::SetClose)
 ON_MESSAGE(WM_RECIVEDATA,&CClientCtrl::SetReceive)
 ON_WM_CREATE()
END_MESSAGE_MAP()

// CClientCtrl 메시지 처리기입니다.

LRESULT CClientCtrl::SetClose(WPARAM wParam, LPARAM lParam)
{
 CConnectSocket* pCSocket = (CConnectSocket*)wParam;
 pCSocket->ShutDown();
 pCSocket->Close();
 CString str;
 str.Format("Disconnected from server[%s:%d]",pCSocket->GetServerIp(),pCSocket->GetServerPort());
 AfxSetOutWindow(OUTPUT_TCPIP_C_INDEX,str);
 return TRUE;
}
LRESULT CClientCtrl::SetReceive(WPARAM wParam, LPARAM lParam)
{
 CConnectSocket* pCSocket = (CConnectSocket*)wParam;
 TCHAR szBuffer[1024];
 ::ZeroMemory(szBuffer, sizeof(szBuffer));
 CString str;
 if (pCSocket->Receive(szBuffer, sizeof(szBuffer)) > 0)
 {
  str.Format("Receive[%s:%d] :\t%s",pCSocket->GetServerIp(),pCSocket->GetServerPort(),szBuffer);
  AfxSetOutWindow(OUTPUT_TCPIP_C_INDEX,str);
 }
 return TRUE;
}
LRESULT CClientCtrl::SetConnect(WPARAM wParam, LPARAM lParam)
{
 CConnectSocket* pCSocket = (CConnectSocket*)wParam;
 CString str;
 str.Format("Connect : [%s:%d]",pCSocket->GetServerIp(),pCSocket->GetServerPort());
 AfxSetOutWindow(OUTPUT_TCPIP_C_INDEX,str);
 return TRUE;
}
void CClientCtrl::SetSend(CString msg)
{
 m_ConnectSocket.Send((LPVOID)(LPCTSTR)msg, msg.GetLength() * 2);
}

BOOL CClientCtrl::Connect(LPCTSTR ipAddr, UINT port)
{
 return m_ConnectSocket.Connect(ipAddr, port);
}
void CClientCtrl::SetServerIPPort(CString ip, UINT port)
{
 m_ConnectSocket.SetServerIPPort(ip,port);
}

int CClientCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CWnd::OnCreate(lpCreateStruct) == -1)
  return -1;
 // TODO:  여기에 특수화된 작성 코드를 추가합니다.
 m_ConnectSocket.SetCWnd(this);
 return m_ConnectSocket.Create();
}

 

Client 연결 소켓

///////////////////////////////////////////////////////////////////////////////////////////////////////
//헤더 파일
#pragma once
// CConnectSocket 명령 대상입니다.
#define WM_RECIVEDATA WM_USER+1
#define WM_DISCONNCET WM_USER+2
#define WM_CONNCET WM_USER+3
#define WM_SEND WM_USER+5
class CConnectSocket : public CAsyncSocket
{
private:
 CString m_ServerIP;
 UINT m_ServerPort;
 CWnd* m_pCWnd;
public:
 CConnectSocket();
 virtual ~CConnectSocket();
 virtual void OnClose(int nErrorCode);
 virtual void OnReceive(int nErrorCode);
 virtual void OnConnect(int nErrorCode);
 
 void SetServerIP(CString ip);
 void SetServerPort(UINT port);
 void SetServerIPPort(CString ip, UINT port);
 void SetCWnd(CWnd* pCWnd);
 
 CString GetServerIp();
 UINT GetServerPort();
};
 
 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////
// cpp 파일
 
// ConnectSocket.cpp : 구현 파일입니다.
//
#include "stdafx.h"
#include "ChartSW.h"
#include "ConnectSocket.h"

// CConnectSocket
CConnectSocket::CConnectSocket()
{
 m_ServerIP = "";
 m_ServerPort = 0;
 m_pCWnd = NULL;
}
CConnectSocket::~CConnectSocket()
{
}

// CConnectSocket 멤버 함수
void CConnectSocket::OnClose(int nErrorCode)
{  
 m_pCWnd->SendMessage(WM_DISCONNCET, (WPARAM)this, (LPARAM)nErrorCode);
 CAsyncSocket::OnClose(nErrorCode);
}
void CConnectSocket::OnReceive(int nErrorCode)
{
 m_pCWnd->SendMessage(WM_RECIVEDATA, (WPARAM)this, (LPARAM)nErrorCode);
 CAsyncSocket::OnReceive(nErrorCode);
}
void CConnectSocket::OnConnect(int nErrorCode)
{ 
 m_pCWnd->SendMessage(WM_CONNCET, (WPARAM)this, (LPARAM)nErrorCode);
 CAsyncSocket::OnConnect(nErrorCode);
}

void CConnectSocket::SetServerIP(CString ip)
{
 if (m_ServerIP == "")
 {
  m_ServerIP = ip;
 } 
}
void CConnectSocket::SetServerPort(UINT port)
{
 if (m_ServerPort == 0)
 {
  m_ServerPort = port;
 }
}
void CConnectSocket::SetServerIPPort(CString ip, UINT port)
{
 SetServerIP(ip);
 SetServerPort(port);
}
void CConnectSocket::SetCWnd(CWnd* pCWnd)
{
 m_pCWnd= pCWnd;
}
CString CConnectSocket::GetServerIp()
{
 return m_ServerIP;
}
UINT CConnectSocket::GetServerPort()
{
 return m_ServerPort;
}
728x90
반응형

'프로그래밍 > MFC' 카테고리의 다른 글

UDP 소켓 통신  (0) 2020.11.12
소켓 통신 Server 부분  (0) 2020.11.04
MFC DLL 배포 방법  (0) 2020.11.02
MFC DLL 확인  (0) 2020.11.02
MFC 문자열 Format  (0) 2020.11.02