C++기반 TCP 채 팅 방 기능 구현

5442 단어 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;
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기