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

소켓 통신 Server 부분

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

서버의 Client Socket

///////////////////////////////////////////////////////////////////////////////////////////////////////
//헤더 파일
#pragma once
// CClientSocket 명령 대상입니다.
class CClientSocket : public CSocket
{
private:
 CString m_strIPAddress;
 UINT m_uPortNUmber;
public: 
 CClientSocket();
 virtual ~CClientSocket();
 CAsyncSocket* m_pListenSocket;
 void SetListenSocket(CAsyncSocket* pSocket);
 virtual void OnClose(int nErrorCode);
 virtual void OnReceive(int nErrorCode);
 CString GetIP();
 UINT GetPort();
};

 
 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////
// cpp 파일
 
// ClientSocket.cpp : 구현 파일입니다.
//
#include "stdafx.h"
#include "ChartSW.h"
#include "ClientSocket.h"
#include "ListenSocket.h"
#include "MainFrm.h"
// CClientSocket
CClientSocket::CClientSocket()
{
 m_pListenSocket = NULL;
 m_strIPAddress = "";
 m_uPortNUmber = 0;
}
CClientSocket::~CClientSocket()
{
}

// CClientSocket 멤버 함수
void CClientSocket::SetListenSocket(CAsyncSocket* pSocket)
{
 m_pListenSocket = pSocket;
 GetPeerName(m_strIPAddress,m_uPortNUmber);
}
void CClientSocket::OnClose(int nErrorCode)
{
 // TODO: 여기에 특수화된 코드를 추가 및/또는 기본 클래스를 호출합니다.
 
 CSocket::OnClose(nErrorCode);
 CListenSocket* pServerSocket = (CListenSocket*)m_pListenSocket;
 pServerSocket->CloseClientSocket(this);
}
void CClientSocket::OnReceive(int nErrorCode)
{
 // TODO: 여기에 특수화된 코드를 추가 및/또는 기본 클래스를 호출합니다.
 CString strTemp="";
 TCHAR szBuffer[1024];
 ::ZeroMemory(szBuffer,sizeof(szBuffer));
 if (Receive(szBuffer,sizeof(szBuffer)) > 0)
 {
  //CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd();
  strTemp.Format("Receive[%s:%d] :\t%s",m_strIPAddress, m_uPortNUmber, szBuffer);
  AfxSetOutWindow(OUTPUT_TCPIP_S_INDEX,strTemp);
 }
 CSocket::OnReceive(nErrorCode);
}
CString CClientSocket::GetIP()
{
 return m_strIPAddress;
}
UINT CClientSocket::GetPort()
{
 return m_uPortNUmber;
}

 

Server의 ListenSocket

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

#pragma once
// CListenSocket 명령 대상입니다.
class CListenSocket : public CAsyncSocket
{
public:
 CListenSocket();
 virtual ~CListenSocket();
 CPtrList m_ptrClientSocketList;
 
 virtual void OnAccept(int nErrorCode);
 void CloseClientSocket(CSocket* pClient);
 void CloseAllClientSocket();
 void SendDataAll(TCHAR* pszMessage);
};

 
 
///////////////////////////////////////////////////////////////////////////////////////////////////////
// cpp 파일
 
// ListenSocket.cpp : 구현 파일입니다.
//
#include "stdafx.h"
#include "ChartSW.h"
#include "ListenSocket.h"
#include "ClientSocket.h"
// CListenSocket
CListenSocket::CListenSocket()
{
}
CListenSocket::~CListenSocket()
{
}

// CListenSocket 멤버 함수
void CListenSocket::OnAccept(int nErrorCode)
{
 // TODO: 여기에 특수화된 코드를 추가 및/또는 기본 클래스를 호출합니다.
 CClientSocket* pClient = new CClientSocket;
 CString str;
 if (Accept(*pClient))
 {
  pClient->SetListenSocket(this);
  m_ptrClientSocketList.AddTail(pClient);
  str.Format("Accept : new client[%s:%d]",pClient->GetIP(),pClient->GetPort());
  AfxSetOutWindow(OUTPUT_TCPIP_S_INDEX,str);
 }
 else
 {
  delete pClient;
  AfxSetOutWindow(OUTPUT_TCPIP_S_INDEX,"ERROR : Failed to accept new client!");
 }
 CAsyncSocket::OnAccept(nErrorCode);
}
void CListenSocket::CloseClientSocket(CSocket* pClient)
{
 POSITION pos;
 pos = m_ptrClientSocketList.Find(pClient);
 CString str;
 CString ip;
 UINT port;
 
 if (pos != NULL)
 {
  CClientSocket* pClientSocket = (CClientSocket*)pClient;
  ip = pClientSocket->GetIP();
  port = pClientSocket->GetPort();
  if (pClient != NULL)
  {
   pClient->ShutDown();
   pClient->Close();
  }
  m_ptrClientSocketList.RemoveAt(pos);
  delete pClient;
 }
 str.Format("Disconnect Client[%s:%d]",ip,port);
 AfxSetOutWindow(OUTPUT_TCPIP_S_INDEX,str);
}
void CListenSocket::CloseAllClientSocket()
{
 POSITION pos;
 pos = m_ptrClientSocketList.GetHeadPosition();
 CClientSocket* pClient = NULL;
 while (pos != NULL)
 {
  pClient = (CClientSocket*)m_ptrClientSocketList.GetNext(pos);
  if (pClient != NULL)
  {
   pClient->ShutDown();
   pClient->Close();
   delete pClient;
  }
 }
}
void CListenSocket::SendDataAll(TCHAR* pszMessage)
{
 POSITION pos;
 pos = m_ptrClientSocketList.GetHeadPosition();
 CClientSocket* pClient = NULL;
 while(pos != NULL)
 {
  pClient = (CClientSocket*)m_ptrClientSocketList.GetNext(pos);
  if (pClient != NULL)
  {
   pClient->Send(pszMessage,lstrlen(pszMessage)*2);
  }
 }
}
728x90
반응형

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

Socket Error Code  (0) 2020.11.12
UDP 소켓 통신  (0) 2020.11.12
소켓 통신 Client 부분  (0) 2020.11.04
MFC DLL 배포 방법  (0) 2020.11.02
MFC DLL 확인  (0) 2020.11.02