基于C++实现TCP聊天室功能

时间:2021-12-29 07:13:43

本文实例为大家分享了C++实现TCP聊天室功能的具体代码,供大家参考,具体内容如下

基于C++实现TCP聊天室功能

TCPServer.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
79
80
81
82
83
84
85
86
// TCPServer.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<iostream>
#include<stdio.h>
#include<WinSock2.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
const char *WELCOMTOCHATROOM = "欢迎来到聊天室";
const char *MESSAGECOMED = "收到客户端发来的信息:";
const char *ANSWER = "您的回复是:";
const char *WAIFORCLIENT = "等待客户连接.......";
int main()
{
 BOOL bSuccess = FALSE;//是否执行成功
 int nError = ERROR;
 SOCKET socketServ = INVALID_SOCKET;
 SOCKET socketClient = INVALID_SOCKET;
 do
 {
  WSADATA wsaData;
  nError = WSAStartup(MAKEWORD(2, 2), &wsaData);//初始化
  if (0 != nError)
   break;
  //创建Socket
  socketServ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (INVALID_SOCKET == socketServ)
   break;
  //创建地址
  sockaddr_in addrServ = { 0 };
  addrServ.sin_addr.S_un.S_addr = INADDR_ANY;//所有ip地址
  addrServ.sin_family = AF_INET;//ipv4
  addrServ.sin_port = htons(10800);//端口
  //绑定
  if (SOCKET_ERROR == bind(socketServ, (SOCKADDR*)&addrServ, sizeof(addrServ)))
   break;
  if (SOCKET_ERROR == listen(socketServ, SOMAXCONN))
   break;
  sockaddr_in addrClient = { 0 };
  int nAddrSize = sizeof(addrClient);
  //等待客户连接
  cout << WAIFORCLIENT << endl;
  socketClient = accept(socketServ, (SOCKADDR*)&addrClient, &nAddrSize);
  if (INVALID_SOCKET == socketClient)
   break;
  if (SOCKET_ERROR == send(socketClient, WELCOMTOCHATROOM, strlen(WELCOMTOCHATROOM)+1,0))
   break;
  char szRecvMessage[MAXBYTE] = { 0 };
  char szSendMessage[MAXBYTE] = { 0 };
  while (true)
  {
   int nResult = recv(socketClient, szRecvMessage, MAXBYTE, 0);
   if (nResult > 0)
   {
    cout << MESSAGECOMED << szRecvMessage << endl;
    cout << ANSWER;
    cin >> szSendMessage;
    if (SOCKET_ERROR == send(socketClient, szSendMessage, strlen(szSendMessage)+1, 0))
     break;//跳出while
   }
   else if (nResult == 0)
   {
    cout << "连接被关闭:" << endl;
   break;
   }
   else
   {
    cout << "recv错误!" << endl;
    break;
   }
  }
  bSuccess = TRUE;
 } while (false);
 
 if (!bSuccess)
 {
  nError = WSAGetLastError();
  cout << "出现错误信息" << nError << endl;
  if (INVALID_SOCKET != socketServ)
  closesocket(socketServ);
  if (INVALID_SOCKET != socketClient)
  closesocket(socketClient);
 }
 WSACleanup();
 system("pause");
 return 0;
}

TCPClient.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
// TCPClient.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
//#include<stdio.h>
#include<WinSock2.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
const char *SERVERIPADDRESS = "192.168.8.106";
int main()
{
 bool bSuccess = FALSE;
 SOCKET socketClient = INVALID_SOCKET;//客户端的套接字
 do
 {
  WSADATA wsaData;
  if (0 != WSAStartup(MAKEWORD(2, 2), &wsaData))//叫醒
  break;
  //判断
  if (2 != LOBYTE(wsaData.wVersion) ||
  2 != HIBYTE(wsaData.wVersion))
  break;
  //拿手机
  socketClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (INVALID_SOCKET == socketClient)
  break;
  //拿到电话号码
  sockaddr_in addrServ = { 0 };
  addrServ.sin_family = AF_INET;
  addrServ.sin_port = htons(10800);
  addrServ.sin_addr.S_un.S_addr = inet_addr(SERVERIPADDRESS);
  //拨打号码
  if (SOCKET_ERROR == connect(socketClient, (SOCKADDR*)&addrServ, sizeof(addrServ)))
  break;
  char szRecvMessage[MAXBYTE] = { 0 };
  char szSendMessage[MAXBYTE] = { 0 };
  while (true)
  {
  int nResult = recv(socketClient, szRecvMessage, MAXBYTE, 0);
  if (nResult > 0)//接收到信息
  {
  cout << "接收到来自服务器的消息:" << szRecvMessage << endl;
  cout << "你的回复是:";
  cin >> szSendMessage;
  if (SOCKET_ERROR == send(socketClient, szSendMessage, MAXBYTE, 0))
   break;//跳出循环
  }
  else if (nResult == 0)//连接被关闭
  {
   cout << "连接被关闭:" << endl;
   break;
  }
  else
  {
   cout << "未知原因:" << endl;
   break;
  }
  }
  bSuccess = TRUE;
 } while (FALSE);
 
 
 if (!bSuccess)
 {
  int nError=WSAGetLastError();
  cout << "出现错误信息" << nError << endl;
  if (INVALID_SOCKET != socketClient)
  closesocket(socketClient);
 }
 WSACleanup();
 system("pause");
 return 0;
}

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

原文链接:https://blog.csdn.net/SWY18929564409/article/details/81077927

相关文章