基于C++实现TCP聊天室功能
本文实例为大家分享了C++实现TCP聊天室功能的具体代码,供大家参考,具体内容如下
【基于C++实现TCP聊天室功能】
文章图片
TCPServer.cpp:
// TCPServer.cpp : Defines the entry point for the console application.#include "stdafx.h"#include#include#include 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; //创建SocketsocketServ = 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; //ipv4addrServ.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//#include#include 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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- 基于微信小程序带后端ssm接口小区物业管理平台设计
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 基于|基于 antd 风格的 element-table + pagination 的二次封装
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 基于爱,才会有“愿望”当“要求”。2017.8.12