基於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;
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: