服务器之家

服务器之家 > 正文

C++基于socket编程实现聊天室功能

时间:2021-11-19 15:59     来源/作者:Hugh677

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

服务端

?
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// server.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
 
#include "pch.h"
#include <iostream>
#include "winsock2.h"
#include "stdlib.h"
#include "stdio.h"
#include "string"
#pragma comment (lib, "ws2_32.lib")
 
using namespace std;
 
 
SOCKET clientSocket[1024];
int k = 0;
 
void communication(LPVOID n)
{
 char buff[256];
 int r;
 int i = (int)n;
 //cout << i << ":" << endl;
 while (1)
 {
 
  memset(buff, 0, 256);
  r = recv(clientSocket[i - 1], buff, 255, NULL);
  if (r > 0)
  {
   cout << i << ":" << buff << endl;
   for (int j = 0; j < k; j++)
   {
    send(clientSocket[j], buff, strlen(buff), NULL);
   }
  }
 
 }
}
 
 
int main()
{
 
 //加载socket库 版本号
 WSADATA wsaData;
 WSAStartup(MAKEWORD(2, 2), &wsaData) != 0;//成功==0
 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
 {
  cout << "请求版本失败!\n" << endl;
  return -1;
 }
 cout << "请求版本成功!\n" << endl;
 //创建socket
 //sockSer = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);//AF=Address family ,ipv4,TCP,0
 SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 if (serverSocket == INVALID_SOCKET)
 {
  cout << "创建socket失败!\n" << endl;
  return -1;
 }
 cout << "创建socket成功!\n" << endl;
 //addrSer.sin_addr.S_un.S_addr
 SOCKADDR_IN addr = { 0 };
 
 //初始化地址
 addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");// htonl(INADDR_ANY);//inet_addr("192.168.0.13");//dec--->2进制-->网络字节序
 addr.sin_family = AF_INET;
 addr.sin_port = htons(10086);//端口号~65535
 //绑定Socket
 int r = bind(serverSocket, (SOCKADDR*)&addr, sizeof(addr));
 if (r == -1)
 {
  cout << "bind失败!\n" << endl;
  return -1;
 }
 cout << "bind成功!\n" << endl;
 //listen
 r = listen(serverSocket, 10);
 if (r == -1)
 {
  cout << "listen失败!\n" << endl;
  return -1;
 }
 cout << "listen成功!\n" << endl;
 //连接
 //地址族
 SOCKADDR_IN cAddr = { 0 };
 int len = sizeof cAddr;
 
 //SOCKET clientSocket[1024];
 int i = 0;
 while (i < 1024)
 {
  clientSocket[i++] = accept(serverSocket, (sockaddr*)&cAddr, &len);
  k++;
  if (clientSocket[i - 1] == SOCKET_ERROR)
  {
   cout << "错误的客户端!\n" << endl;
   closesocket(serverSocket);
   WSACleanup();
   return -1;
  }
  cout << "有客户端接入进来!" << inet_ntoa(cAddr.sin_addr) << endl;
  CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)communication, (LPVOID)i, NULL, NULL);
 }
 
 
 //closesocket(clientSocket);
 //closesocket(serverSocket);
 //WSACleanup();
 return 0;
}

客户端

?
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "pch.h"
#include <iostream>
#include "winsock2.h"
#include "stdlib.h"
#include "stdio.h"
#include "string"
#pragma comment (lib, "ws2_32.lib")
 
//#include "graphics.h"
 
using namespace std;
 
SOCKET serverSocket;//服务器
 
 
void recvAndShow()
{
 int r, i = 0;
 char buff[256];
 //ofstream out;
 while (1)
 {
  memset(buff, 0, 256);
  r = recv(serverSocket, buff, 255, NULL);
  if (r > 0)
  {
  // out.open("QQ.txt", ios::app||ios::_Nocreate);
  // out << buff << endl;
   //outtextxy(0, i * 20, buff);
   i++;
   //out.close();
  }
 }
}
 
int main()
{
 //initgraph(300, 300, SHOWCONSOLE);
 //加载socket库 版本号
 
 WSADATA wsaData;
 WSAStartup(MAKEWORD(2, 2), &wsaData) != 0;//成功==0
 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
 {
  cout << "请求版本失败!\n" << endl;
  return -1;
 }
 cout << "请求版本成功!\n" << endl;
 
 //创建socket
 
 serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 if (serverSocket == INVALID_SOCKET)
 {
  cout << "创建socket失败!\n" << endl;
  return -1;
 }
 cout << "创建socket成功!\n" << endl;
 
 //地址族
 
 SOCKADDR_IN addr = { 0 };
 
 //初始化地址
 
 addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");// htonl(INADDR_ANY);//inet_addr("192.168.0.13");//dec--->2进制-->网络字节序
 addr.sin_family = AF_INET;
 addr.sin_port = htons(10086);//端口号~65535尽量大于1W
 
 //连接到服务器
 
 int r = connect(serverSocket, (SOCKADDR*)&addr, sizeof addr);
 if (r == -1)
 {
  cout << "连接服务器失败!\n" << endl;
  return -1;
 }
 cout << "连接服务器成功!\n" << endl;
 
 CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)recvAndShow, NULL, NULL, NULL);
 
 char buff[256];
 while (1)
 {
  memset(buff, 0, 256);
  cout << "你要发啥?\n" << endl;
  cin >> buff;
  r = send(serverSocket, buff, strlen(buff), NULL);
  if (r > 0)
  {
   cout << "发送" << r << "字节到服务器成功!\n" << endl;
  }
 
 }
 //closesocket(serverSocket);
 //WSACleanup();
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。 

原文链接:https://blog.csdn.net/weixin_42542313/article/details/85935862

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部