用c++实现一个简单的聊天程序要求有1. 使用基于socket的程序设计方法设计聊天客户端和服务器端程序。2.程序至少能够实现传输文字的功能。3.能够实现多人聊天的功能
时间: 2024-02-24 15:58:15 浏览: 165
以下是一个基于socket的简单聊天程序的实现,包括服务器端和客户端代码。这个程序可以实现多人聊天的功能,用户可以通过输入文字进行聊天。
服务器端代码:
```c++
#include <iostream>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <vector>
#define PORT 8080
using namespace std;
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
vector<int> clients;
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
cout << "Waiting for connections..." << endl;
while (true) {
if ((new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
clients.push_back(new_socket);
cout << "New connection: " << inet_ntoa(address.sin_addr) << ":" << ntohs(address.sin_port) << endl;
send(new_socket, "Welcome to the chatroom!\n", strlen("Welcome to the chatroom!\n"), 0);
for (int i = 0; i < clients.size(); ++i)
send(clients[i], "New user joined the chatroom.\n", strlen("New user joined the chatroom.\n"), 0);
while (true) {
memset(buffer, 0, sizeof(buffer));
valread = recv(new_socket, buffer, sizeof(buffer), 0);
if (strcmp(buffer, "exit\n") == 0) {
cout << "Connection closed: " << inet_ntoa(address.sin_addr) << ":" << ntohs(address.sin_port) << endl;
close(new_socket);
clients.erase(remove(clients.begin(), clients.end(), new_socket), clients.end());
for (int i = 0; i < clients.size(); ++i)
send(clients[i], "User left the chatroom.\n", strlen("User left the chatroom.\n"), 0);
break;
}
for (int i = 0; i < clients.size(); ++i)
send(clients[i], buffer, strlen(buffer), 0);
}
}
return 0;
}
```
客户端代码:
```c++
#include <iostream>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8080
using namespace std;
int main(int argc, char const *argv[]) {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
char message[1024];
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
cout << "\n Socket creation error \n" << endl;
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
cout << "\nInvalid address/ Address not supported \n" << endl;
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
cout << "\nConnection Failed \n" << endl;
return -1;
}
while (true) {
memset(buffer, 0, sizeof(buffer));
valread = recv(sock, buffer, sizeof(buffer), 0);
cout << buffer << endl;
if (strcmp(buffer, "Welcome to the chatroom!\n") == 0)
continue;
cout << "Enter message: ";
fgets(message, 1024, stdin);
send(sock, message, strlen(message), 0);
if (strcmp(message, "exit\n") == 0)
break;
}
close(sock);
return 0;
}
```
注意,这只是一个简单的示例程序,没有进行错误检查和安全性检查,请不要在生产环境中使用。
阅读全文