大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本文小编为大家详细介绍“C++基于socket UDP网络编程怎么实现聊天室功能”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++基于socket UDP网络编程怎么实现聊天室功能”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
目前创新互联已为成百上千家的企业提供了网站建设、域名、网站空间、网站托管、服务器托管、企业网站设计、开州网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。(左:服务器;右:客户端;)
1.1服务器类头文件(CServer_UDP.h)
#pragma once #includeclass CServer_UDP { public: CServer_UDP(); void SendMsg(const char sendBuf[]); void RecMsg(); ~CServer_UDP(); private: SOCKET m_sServer; struct sockaddr_in m_SocAddrClient; //建立连接时,用于保存客户端信息 bool m_terminal; };
1.2服务器类源文件(CServer_UDP.cpp)
#define _WINSOCK_DEPRECATED_NO_WARNINGS #include "CServer_UDP.h" #include#pragma comment(lib, "ws2_32.lib") CServer_UDP::CServer_UDP():m_terminal(false) { //必须进行如下初始化, 否则socket()会返回10093错误 //初始化WSA WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) //通过一个进程初始化ws2_32.dll { std::cout << "Initialize WSA failed" << std::endl; return; } //初始化UDDP套接字 m_sServer = socket(AF_INET, SOCK_DGRAM, 0); struct sockaddr_in m_SocAddrserver; m_SocAddrserver.sin_addr.S_un.S_addr = 0;//htonl(INADDR_ANY); m_SocAddrserver.sin_family = AF_INET; m_SocAddrserver.sin_port = htons(8090); int ret = bind(m_sServer, (sockaddr*)&m_SocAddrserver, sizeof(m_SocAddrserver)); if (ret == -1) { std::cout << "bind failed!" << std::endl; WSACleanup(); } else { //此处必须赋初值,不然会导致服务器端无法正常发送 int len_Client = sizeof(sockaddr); char recBuf[1025]; int len = recvfrom(m_sServer, recBuf, 1024, 0, (sockaddr*)&m_SocAddrClient, &len_Client); if (len > 0) { recBuf[len] = '\0'; std::cout << "Client say:" << recBuf << std::endl; } } } void CServer_UDP::SendMsg(const char sendBuf[]) { int ret = sendto(m_sServer, sendBuf, strlen(sendBuf), 0, (sockaddr*)&m_SocAddrClient, sizeof(m_SocAddrClient)); if (ret == -1) { std::cout << "send failed" << std::endl; std::cout << GetLastError()<< std::endl; } } void CServer_UDP::RecMsg() { char recBuf[1025]; while (!m_terminal) { //std::cout << "Begin rec...(server)" << std::endl; int len = recvfrom(m_sServer, recBuf, 1024, 0, 0, 0); if (len > 0) { recBuf[len] = '\0'; std::cout << "Client say:" << recBuf << std::endl; } } } CServer_UDP::~CServer_UDP() { closesocket(m_sServer); WSACleanup(); }
1.3服务器主函数
#include#include #include #include "CServer_UDP.h" using namespace std; int main() { CServer_UDP server_UDP; thread recProc(&CServer_UDP::RecMsg, &server_UDP); while (1) { //cout << "Pleaes input content:" << endl; string content; cin >> content; server_UDP.SendMsg(content.c_str()); } recProc.join(); cout << "I love china!" << endl; system("pause"); return 0; }
2.1客户端类头文件(CClient.h)
#pragma once #includeclass CClient { public: CClient(); void RecMsg(); void SendMsg(const char sendBuf[]); ~CClient(); private: SOCKET m_sockClient; sockaddr_in m_TargetServer; };
2.2客户端类源文件(CClient.cpp)
#define _WINSOCK_DEPRECATED_NO_WARNINGS #include "CClient.h" #include#pragma comment(lib, "ws2_32.lib") CClient::CClient() { //必须进行如下初始化, 否则socket()会返回10093错误 //初始化WSA WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) //通过一个进程初始化ws2_32.dll { std::cout << "Initialize WSA failed" << std::endl; return; } m_sockClient = socket(AF_INET, SOCK_DGRAM, 0); m_TargetServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); m_TargetServer.sin_family = AF_INET; m_TargetServer.sin_port = htons(8090); if (m_sockClient == -1) { std::cout << "Create socket failed!" << std::endl; WSACleanup(); } else { //发送信息与服务器建立连接(必须加) sendto(m_sockClient, "hello server", strlen("hello server"), 0, (sockaddr*)&m_TargetServer, sizeof(m_TargetServer)); } } void CClient::SendMsg(const char sendBuf[]) { sendto(m_sockClient, sendBuf, strlen(sendBuf), 0, (sockaddr*)&m_TargetServer, sizeof(m_TargetServer)); } void CClient::RecMsg() { char recBuf[1025]; while (1) { //std::cout << "Begin rec...(client)" << std::endl; int len = recvfrom(m_sockClient, recBuf, 1024, 0, 0, 0); if (len > 0) { recBuf[len] = '\0'; std::cout << "Server say: " << recBuf << std::endl; } } } CClient::~CClient() { closesocket(m_sockClient); WSACleanup(); }
2.3客户端主函数
#include#include #include #include "CClient.h" using namespace std; int main() { CClient client_UDP; thread RecProc(&CClient::RecMsg, &client_UDP); while (1) { //cout << "Please input content:" << endl; string content; cin >> content; client_UDP.SendMsg(content.c_str()); } RecProc.join(); cout << "I love china!" << endl; system("pause"); return 0; }
3.1服务端
3.2客户端
读到这里,这篇“C++基于socket UDP网络编程怎么实现聊天室功能”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注创新互联行业资讯频道。