1:1 ������ ������그������ ������ ��� ������ ��������� ������ ��������� ��� ��.
----
----
������ ���������기 ¶
������기 | ������ |
��������� ���기 ������ '������ ������' | IP������ |
������������ ���결������ ��������� ������ ��� ������ ������ | ���기������(Listen) |
�������� 걸������ | ���결 ������(Connect) |
��������� ������ | ���결 ������(Accept) |
~cpp Socket ������ -> Socket ��� ���������결 (bind) -> ������������������ ���결��� 기������(listen) -> ������������������ ������������ (Accept) -> ������������������ ��������� ��������� ��������� ������������ ������
~cpp Socket ������ -> ��������� ���결 ������(connect) -> ��������� 각��� ��������� ������
������(������) ¶
Dialog based -> Windows Sockets��� ������
기��� ����������� CAsyncSocket��� ��������� ��������� CListenSock, CChildSock��� ������ ������������.
������ �������� ���개��� ������������ ������과 ����� ������������.
��������������������� CListenSock��� ����� ������ OnAccept()��� �������� ��� ������ ��������� ������������.
기��� ����������� CAsyncSocket��� ��������� ��������� CListenSock, CChildSock��� ������ ������������.
������ �������� ���개��� ������������ ������과 ����� ������������.
��������������������� CListenSock��� ����� ������ OnAccept()��� �������� ��� ������ ��������� ������������.
~cpp ((CServerApp*)AfxGetApp())->Accept();
��������������������� CChildSock��� ����� ������ OnReceive()��� OnClose()��� �������� ��� ������ ��������� ������������.
~cpp ((CServerApp*)AfxGetApp())->CloseChild();
~cpp ((CServerApp*)AfxGetApp())->ReceiveData();
������������������ ������������ ��������� ��������������� ������ ��������� ������������.
~cpp #include "ChildSock.h" #include "ListenSock.h"
~cpp void InitServer(); void Accept(); void SendData(CString& strData); void ReceiveData(); void CloseChild(); void CleanUp(); CListenSock* m_pServer; CChildSock* m_pChild;
������������������ ������������ ��������������� �������� ��������������� ���기���������.
~cpp m_pServer = NULL; m_pChild = NULL;
그���고 ������ �������� ��������������� ������과 ����� ������������.
~cpp void CServerApp::InitServer() { m_pServer = new CListenSock; m_pServer->Create(7000); m_pServer->Listen(); } void CServerApp::Accept() { AfxMessageBox("������ ������"); m_pChild = new CChildSock; m_pServer->Accept(*m_pChild); m_pMainWnd->GetDlgItem(IDC_SEND)->EnableWindow(TRUE); }
������������ ��������� ��������� ��������� ������ ��������� �����������. 그 ������, ��������� ��������� ��� ��������� ��������� ������.
~cpp void CServerApp::SendData(CString& strData) { m_pChild->Send(LPCSTR(strData), strData.GetLength()+1); CString strText; UINT nPort; m_pChild->GetSockName(strText, nPort); strText = "[" + strText + "]" + strData; ((CListBox*)m_pMainWnd->GetDlgItem(IDC_LIST1))->InsertString(-1, strText); } void CServerApp::ReceiveData() { char temp[1000]; m_pChild->Receive(temp, sizeof(temp)); CString strText; UINT nPort; m_pChild->GetPeerName(strText, nPort); strText = "[" + strText + "]" + temp; ((CListBox*)m_pMainWnd->GetDlgItem(IDC_LIST1))->InsertString(-1, strText); } void CServerApp::CleanUp() { if (m_pChild) delete m_pChild; if (m_pServer) delete m_pServer; } void CServerApp::CloseChild() { AfxMessageBox("������"); delete m_pChild; m_pMainWnd->GetDlgItem(IDC_SEND)->EnableWindow(FALSE); }
������������������ ������������그 ��������� "IDD_SERVER_DIALOG"��� ������������.
~cpp ���������������ID : IDC_LIST1 ��������� ���������ID : IDC_DATA ������ ���������ID : IDC_SEND
������������그�� ���기������ ��� ��������� ��������������� CServerDlg ������������ OnInit Dialog()��������� ������ ��������� ������������.
~cpp ((CServerApp*)AfxGetApp)->InitServer(); GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
그���고 ������ ��������������������� CServerDlg��� IDC_SEND��� ��������� ��� ������ ��������� �����������.
~cpp CString strData; GetDlgItemText(IDC_DATA, strData); ((CServerApp*)AfxGetApp())->SendData(strData); SetDlgItemText(IDC_DATA,"");
������(���������������) ¶
��������� ��������� ������������ ��������������� ������그��������� ��������� ������ ��������� CClientSock��� ������(기��� ���������: CAsyncSocket)������. 그���고 ������ ��������������������� CClientSock��� ����������� OnReceive()��� OnClose()��� �������� ���, ������ ��������� ������������.
~cpp ((CClientApp*)AfxGetApp())->CloseChild();
~cpp ((CClientApp*)AfxGetApp())->ReceiveData();
������ ��������� ��������� IP ��������� ���������기 ������ ������������������ ������������그��� ������ �������� ��� ������과 ����� ������������.
~cpp IP������ ���������ID : IDC_IPADDRESS1 ������ ID : IDOK, IDCANCLE
��������������������� ������������ CConnectDlg ������������ ������ ��������� ��� CConnectDlg ������������ ������ ��������� �����������.
~cpp CString m_strAddress;
그���고 ��������������������� CConnectDlg��� IDOK��� ������������ ����������� ��������� IP ��������� ������������ m_strAddress��� ������������.
~cpp GetDlgItemText(IDC_IPADDRESS1, m_strAddress);
������������������ ������������ ������과 ����� ��������������� ������ ��������� ������������.
~cpp #include "ClientSock.h"
~cpp void Connect(); void SendData(CString& strData); void ReceiveData(); void CloseChild(); void CleanUp(); CClientSock* m_pClient;������������������ ������������ ��������������� �������� ��������������� ���기���������.
~cpp m_pClient = NULL;
~cpp #include "ConnectDlg.h" void CClientApp::Connect() { CConnectDlg dlg; if (dlg.DoModal() == IDOK) { m_pClient = new CClientSock; m_pClient->Create(); m_pClient->Connect(dlg.m_strAddress, 7000); m_pMainWnd->GetDlgItem(IDC_SEND)->EnableWindow(TRUE); m_pMainWnd->GetDlgItem(IDC_CONNECT)->EnableWindow(FALSE); } }
������ ��������� ����� ������ ������그������ ������ ������(7000)��� ��������� ������ �������� ������������ ������.
��������� ��������� ��������� 과���과 ��������� ��������� ������ ������그���과 ������������.
��������� ��������� ��������� 과���과 ��������� ��������� ������ ������그���과 ������������.
~cpp void CClientApp::SendData(CString& strData) { m_pClient->Send(LPCSTR(strData), strData.GetLength()+1); CString strText; UINT nPort; m_pClient->GetSockName(strText, nPort); strText = "[" + strText + "]" + strData; ((CListBox*)m_pMainWnd->GetDlgItem(IDC_LIST1))->InsertString(-1, strText); } void CClientApp::ReceiveData() { char temp[100]; m_pClient->Receive(temp, sizeof(temp)); CString strText; UINT nPort; m_pClient->GetPeerName(strText, nPort); strText = "[" + strText + "]" + temp; ((CListBox*)m_pMainWnd->GetDlgItem(IDC_LIST1))->InsertString(-1, strText); } void CClientApp::CleanUp() { if (m_pClient) delete m_pClient; } void CClientApp::CloseChild() { AfxMessageBox("������"); m_pMainWnd->GetDlgItem(IDC_SEND)->EnableWindow(FALSE); }
������������������ ������������그 ��������� "IDD_CLIENT_DIALOG"��� ��������������� ��� ������과 ����� ������������.
~cpp ��������� ID : IDC_LIST1 ��������� ������ID : IDC_DATA ������ ID : IDC_SEND, IDC_CONNECT
~cpp ((CClientApp*)AfxGetApp())->Connect();
~cpp CString strData; GetDlgItemText(IDC_DATA, strData); ((CClientApp*)AfxGetApp())->SendData(strData); SetDlgItemText(IDC_DATA,"");
���고 ¶
http://www.rein.pe.kr/technote/read.cgi?board=programing&y_number=260&nnew=2
http://www.rein.pe.kr/technote/read.cgi?board=programing&y_number=261&nnew=2
http://www.rein.pe.kr/technote/read.cgi?board=programing&y_number=262&nnew=2
http://www.rein.pe.kr/technote/read.cgi?board=programing&y_number=263&nnew=2
http://165.194.17.15/pub/upload/p2pChattingProgram
----
5������C++���������
http://www.rein.pe.kr/technote/read.cgi?board=programing&y_number=261&nnew=2
http://www.rein.pe.kr/technote/read.cgi?board=programing&y_number=262&nnew=2
http://www.rein.pe.kr/technote/read.cgi?board=programing&y_number=263&nnew=2
http://165.194.17.15/pub/upload/p2pChattingProgram
----
5������C++���������