gpt4 book ai didi

C++基于socket UDP网络编程实现简单聊天室功能

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章C++基于socket UDP网络编程实现简单聊天室功能由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例为大家分享了C++基于socket UDP实现简单聊天室功能的具体代码,供大家参考,具体内容如下 。

0.通信步骤流程图

(左:服务器;右:客户端;) 。

C++基于socket UDP网络编程实现简单聊天室功能

1.服务器代码

1.1服务器类头文件(CServer_UDP.h) 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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) 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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服务器主函数 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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) 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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) 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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客户端主函数 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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服务端 。

C++基于socket UDP网络编程实现简单聊天室功能

3.2客户端 。

C++基于socket UDP网络编程实现简单聊天室功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:https://blog.csdn.net/CSDN_WHB/article/details/89187866 。

最后此篇关于C++基于socket UDP网络编程实现简单聊天室功能的文章就讲到这里了,如果你想了解更多关于C++基于socket UDP网络编程实现简单聊天室功能的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com