您好,登錄后才能下訂單哦!
這篇文章主要介紹C++基于socket UDP網絡編程如何實現聊天室功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體內容如下
(左:服務器;右:客戶端;)
1.1服務器類頭文件(CServer_UDP.h)
#pragma once #include <winsock2.h> class 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 <iostream> #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 <iostream> #include <thread> #include <string> #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 #include <winsock2.h> class 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 <iostream> #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 <iostream> #include <string> #include <thread> #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網絡編程如何實現聊天室功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。