小编给大家分享一下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聊天室功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。