首页 > 编程语言 > 基于C++实现TCP聊天室功能
2021
10-12

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

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

TCPServer.cpp:

// 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:

// 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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧