VC基于TCP 的网络聊天室代码
时间: 2023-09-15 17:22:23 浏览: 109
下面是一个基于TCP的简单聊天室的代码示例,使用C++编写,可以在Visual Studio中编译运行。
服务器端:
```c++
#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main()
{
// 初始化Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
{
cout << "WSAStartup failed with error: " << iResult << endl;
return 1;
}
// 创建套接字
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET)
{
cout << "socket failed with error: " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 绑定套接字
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(12345);
iResult = bind(listenSocket, (const sockaddr*)&addr, sizeof(addr));
if (iResult == SOCKET_ERROR)
{
cout << "bind failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 监听连接请求
iResult = listen(listenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR)
{
cout << "listen failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return 1;
}
cout << "Server started, listening on port 12345." << endl;
// 接受连接请求并处理客户端消息
SOCKET clientSocket;
sockaddr_in clientAddr;
int clientAddrSize = sizeof(clientAddr);
char recvBuf[1024];
while (true)
{
clientSocket = accept(listenSocket, (sockaddr*)&clientAddr, &clientAddrSize);
if (clientSocket == INVALID_SOCKET)
{
cout << "accept failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return 1;
}
cout << "Client connected, address: " << inet_ntoa(clientAddr.sin_addr) << ":" << ntohs(clientAddr.sin_port) << endl;
while (true)
{
iResult = recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
if (iResult == SOCKET_ERROR)
{
cout << "recv failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
break;
}
else if (iResult == 0)
{
cout << "Client disconnected, address: " << inet_ntoa(clientAddr.sin_addr) << ":" << ntohs(clientAddr.sin_port) << endl;
closesocket(clientSocket);
break;
}
cout << "Received message from client (" << inet_ntoa(clientAddr.sin_addr) << ":" << ntohs(clientAddr.sin_port) << "): " << recvBuf << endl;
iResult = send(clientSocket, recvBuf, iResult, 0);
if (iResult == SOCKET_ERROR)
{
cout << "send failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
break;
}
}
}
closesocket(listenSocket);
WSACleanup();
return 0;
}
```
客户端:
```c++
#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main()
{
// 初始化Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
{
cout << "WSAStartup failed with error: " << iResult << endl;
return 1;
}
// 创建套接字
SOCKET connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connectSocket == INVALID_SOCKET)
{
cout << "socket failed with error: " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 连接服务器
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(12345);
iResult = connect(connectSocket, (const sockaddr*)&serverAddr, sizeof(serverAddr));
if (iResult == SOCKET_ERROR)
{
cout << "connect failed with error: " << WSAGetLastError() << endl;
closesocket(connectSocket);
WSACleanup();
return 1;
}
cout << "Connected to server, input 'exit' to quit." << endl;
// 发送消息给服务器
char sendBuf[1024];
while (true)
{
cin.getline(sendBuf, sizeof(sendBuf));
if (strcmp(sendBuf, "exit") == 0)
{
break;
}
iResult = send(connectSocket, sendBuf, strlen(sendBuf), 0);
if (iResult == SOCKET_ERROR)
{
cout << "send failed with error: " << WSAGetLastError() << endl;
closesocket(connectSocket);
WSACleanup();
return 1;
}
iResult = recv(connectSocket, sendBuf, sizeof(sendBuf), 0);
if (iResult == SOCKET_ERROR)
{
cout << "recv failed with error: " << WSAGetLastError() << endl;
closesocket(connectSocket);
WSACleanup();
return 1;
}
sendBuf[iResult] = '\0';
cout << "Received message from server: " << sendBuf << endl;
}
closesocket(connectSocket);
WSACleanup();
return 0;
}
```
运行客户端程序,输入任意消息即可将消息发送给服务器,服务器将收到消息并将其原样发送回客户端。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)