C++基於socket UDP網絡編程實現簡單聊天室功能
本文實例為大傢分享瞭C++基於socket UDP實現簡單聊天室功能的具體代碼,供大傢參考,具體內容如下
0.通信步驟流程圖
(左:服務器;右:客戶端;)
1.服務器代碼
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.客戶端代碼
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.效果圖(win7+VS2017)
3.1服務端
3.2客戶端
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。