本文小編為大家詳細介紹“C++基于socket UDP網(wǎng)絡編程怎么實現(xiàn)聊天室功能”,內容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“C++基于socket UDP網(wǎng)絡編程怎么實現(xiàn)聊天室功能”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
目前創(chuàng)新互聯(lián)已為成百上千家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)站空間、網(wǎng)站托管、服務器托管、企業(yè)網(wǎng)站設計、開州網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。(左:服務器;右:客戶端;)
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 { //此處必須賦初值,不然會導致服務器端無法正常發(fā)送 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服務器主函數(shù)
#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 { //發(fā)送信息與服務器建立連接(必須加) 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客戶端主函數(shù)
#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網(wǎng)絡編程怎么實現(xiàn)聊天室功能”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。