欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-27 09:05:57
本文实例为大家分享了c++实现tcp聊天室功能的具体代码,供大家参考,具体内容如下tcpserver.cpp:// tcpserver.cpp : defines the entry point fo...

本文实例为大家分享了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;
}

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

相关标签: C++ TCP 聊天室