当前位置:网站首页>Chat software project development 1
Chat software project development 1
2022-07-18 00:15:00 【Domineering Xiao Ming】
Catalog
1.1 client / Server interface design
1.3 rewrite socket, Connecting server and client
1.1 client / Server interface design
client
Use 100 million diagram , Draw the client interface style to be implemented

With the blessing of powerful artistic cells , Our chat software interface has also taken shape !
client MFC Layout

Compilation result

Server side

Server side MFC Layout

Compilation result

1.2IP And port acquisition
client
obtain IP And port
#include<atlbase.h>
void CMFCChatClientDlg::OnBnClickedConnectBtn()
{
CString strPort, strIP;
// Get the contents of the control
GetDlgItem(IDC_PORT_EDIT)->GetWindowText(strPort);
GetDlgItem(IDC_IPADDRESS)->GetWindowText(strIP);
//CString turn char*
USES_CONVERSION;
LPCSTR szPort = (LPCSTR)T2A(strPort);
LPCSTR szIP = (LPCSTR)T2A(strIP);
TRACE("szPort=%s,szIP=%s", szPort, szIP);
}Server side
Get port
#include<atlbase.h>
void CMFCChartServerDlg::OnBnClickedStartBtn()
{
TRACE("OnBnClickedStartBtn");
CString strPort;
// Get the contents of the control
GetDlgItem(IDC_PORT_EDIT)->GetWindowText(strPort);
//CString turn char*
USES_CONVERSION;
LPCSTR szPort = (LPCSTR)T2A(strPort);
TRACE("szPort=%s", szPort);
}1.3 rewrite socket, Connecting server and client
Final effect

client
obtain IP And port , establish Socket ( rewrite OnConnect and OnReceive), call Connect Connect
Server side
Get port , establish Socket( rewrite OnAccept), Start listening ; Create another one after each connection with the client Socket( rewrite OnReceive)
client
Inherit CAsyncSocket class , And rewrite OnConnect( ) and onReceive
#pragma once
#include<afxsock.h>
class CMySocket :
public CAsyncSocket
{
public:
CMySocket();
virtual ~CMySocket();
// rewrite Connect function
virtual void OnConnect(int nErrorCode);
// rewrite Receive function
virtual void onReceive(int nErrorCode);
};
Rewrite function implementation
void CMySocket::OnConnect(int nErrorCode)
{
TRACE("############OnConnect");
// Get the main window
CMFCChatClientDlg* dlg = (CMFCChatClientDlg*)AfxGetApp()->GetMainWnd();
CString str;
// Get the current time of the system
dlg->m_tm = CTime::GetCurrentTime();
// Dead back
str = dlg->m_tm.Format("%X");
str += _T(" The connection to the server was successful ");
// Display the time in the history message
dlg->m_list.AddString(str);
// Standard writing
CAsyncSocket::OnConnect(nErrorCode);
}
void CMySocket::OnReceive(int nErrorCode)
{
TRACE("############onReceive");
}On the client side “ Connect ” Control function

Get port and IP, Create a Socket object , Create a Socket, Then connect
void CMFCChatClientDlg::OnBnClickedConnectBtn()
{
// Get the port and IP
CString strPort, strIP;
// Get the contents of the control
GetDlgItem(IDC_PORT_EDIT)->GetWindowText(strPort);
GetDlgItem(IDC_IPADDRESS)->GetWindowText(strIP);
//CString turn char*
USES_CONVERSION;
LPCSTR szPort = (LPCSTR)T2A(strPort);
LPCSTR szIP = (LPCSTR)T2A(strIP);
TRACE("szPort=%s,szIP=%s", szPort, szIP);
// String to number
int iPort = _ttoi(strPort);
// Create a socket object
m_client = new CMySocket;
// Create socket
if (!m_client->Create()) {
// Error code 10093, Need in the project .cpp Of documents InitInstance( ) Call in function AfcSocketInit() Function initial words
TRACE("m_client Create error %d", GetLastError());
return;
}
else {
TRACE("m_client Creat Success");
}
// Connect
/* There is no way to judge whether the connection is successful , You need to judge in the callback function ; So if you use
* if(!m_client->Connect(strIP,iPort)) If you judge whether the connection is successful, you will report 10035(WSAEWOULDBLOCK) error
* If you want to judge here, you only need to judge whether it is equal to SOCKET_ERROR That's all right.
*/
if (m_client->Connect(strIP, iPort) != SOCKET_ERROR) {
TRACE("m_client Connect errot %d", GetLastError());
}
}Server side
CServerSocket Inherit CAsyncSocket class , And rewrite OnAccept( ) function —— Monitor class
#pragma once
#include<afxsock.h>
class CServerSocket :
public CAsyncSocket
{
public:
CServerSocket();
virtual ~CServerSocket();
virtual void OnAccept(int nErrorCode);
};
Rewrite function implementation
void CServerSocket::OnAccept(int nErrorCode)
{
TRACE("#####OnAccept");
CMFCChartServerDlg* dlg = (CMFCChartServerDlg*)AfxGetApp()->GetMainWnd();
// Create one connection at a time socket
dlg->m_chat = new CChatSocket;
// Receive client connections
Accept(*(dlg->m_chat));
CString str;
dlg->m_tm = CTime::GetCurrentTime();
str = dlg->m_tm.Format("%X");
str += _T(" Client connection successful ");
dlg->m_list.AddString(str);
dlg->m_list.UpdateData(FALSE);
CAsyncSocket::OnAccept(nErrorCode);
}CChatServet Inherit CAsyncSocket class , And rewrite OnReceive( ) function —— Connection class
#pragma once
#include <afxsock.h>
class CChatSocket :
public CAsyncSocket
{
public:
CChatSocket();
virtual ~CChatSocket();
virtual void OnReceive(int nErrorCode);
};Rewrite function implementation
void CChatSocket::OnReceive(int nErrorCode)
{
}On the server “ start-up ” Control function
Get the port value , establish Socket object , establish Socket, monitor
void CMFCChartServerDlg::OnBnClickedStartBtn()
{
TRACE("OnBnClickedStartBtn");
CString strPort;
// Get the contents of the control
GetDlgItem(IDC_PORT_EDIT)->GetWindowText(strPort);
//CString turn char*
USES_CONVERSION;
LPCSTR szPort = (LPCSTR)T2A(strPort);
TRACE("szPort=%s", szPort);
// Create a server Socket The object of
m_server = new CServerSocket;
// establish Socket( Socket )
int iPort = _ttoi(strPort);
if (!m_server->Create(iPort)) {
TRACE("m_server Create errorCode=%d", GetLastError());
return;
}
// monitor , Callback when there is a client connection OnAccept
if (!m_server->Listen()) {
TRACE("m_sercer listen Create errorCode=%d", GetLastError());
return;
}
CString str;
m_tm = CTime::GetCurrentTime();
str = m_tm.Format("%X");
str += _T(" Building services ");
m_list.AddString(str);
// Update the data ,FALSE Get the data out of the control ,TRUE Get the data from the control
UpdateData(FALSE);
}边栏推荐
- 拓扑排序原理
- MFC基于单个文档的文件读写
- [answer questions and solve doubts] in the wave of layoffs, what exactly does n+1 mean?
- BeautifulSoap基本使用
- oracledb_exporter监控Oracle,一个入侵性极低的监控方案。
- Analysis of eip-2535 diamond agreement from the Saudi NFT event
- Voice chat source code - voice chat source code development, design and construction
- H5 start applet
- C language shift operation
- Degree engine (12): video loading
猜你喜欢

Gao Fushui in unit testing, pytest framework (II) pre post method and fixture mechanism

小程序 拉起企业微信进群二维码
![[Huang ah code] Introduction to MySQL - 3. I use select *, and the boss directly rushed me home by train, but I still bought a station ticket](/img/7b/f50c5f4b16a376273ba8cd27543676.png)
[Huang ah code] Introduction to MySQL - 3. I use select *, and the boss directly rushed me home by train, but I still bought a station ticket

【数学建模暑期培训】Matlab绘图命令

科技公司纷纷反对 英国网络安全法案搁置

Using the idea shortcut key, you can know your technical level?
![[Gu Yue 21 talks] ROS introduction series (2) -- programming implementation of publisher and subscriber + programming implementation of customized topic message](/img/3e/f6de39326fb9d499da52dcc707d499.png)
[Gu Yue 21 talks] ROS introduction series (2) -- programming implementation of publisher and subscriber + programming implementation of customized topic message

Dlvm netcore open source Framework

Four thinking abilities that front-line technicians should pay attention to
![[Huawei online battle] download and run Huawei's official unity example code, prompting authentication failure and returning error code 100114](/img/32/7d796399ac996fd7da5609f0252dfb.png)
[Huawei online battle] download and run Huawei's official unity example code, prompting authentication failure and returning error code 100114
随机推荐
View CPU information mode
Typescript 14 starting from 0: built in tool type
Basic use of beautifulsoap
Spike project learning
The security optimization settings after the installation of Zhimeng CMS effectively protect Trojans
System solutions
Cf1265e beautiful mirrors (probability DP)
[live class] Tencent classroom ----- cloud native tool secondary development training camp based on go language ----- kubernetes operator development
NFT industry analysis of metauniverse: China's digital collection industry is expected to move towards standardization and differentiation
Topological sorting principle
Suspected of being recruited by apple, playcover author deleted the library and ran away
JMeter regular expression gets login token
Charles的基本使用及教程
【LeetCode】Day100-颜色分类
BeautifulSoap基本使用
The most common algorithm interview questions
Electric razor touch chip-dlt8t10s-jericho
stack-protector enabled but compiler support broken
Technology companies have opposed the shelving of the UK cybersecurity act
MacM1芯片,centos8虚拟机安装mysql8,服务起来了,登录报错