我需要的是一个用C++或者JAVA实现的,你可以给出一个吗
时间: 2024-02-27 10:57:43 浏览: 149
基于C++实现的计算器
5星 · 资源好评率100%
当然,我可以为您提供一个简单的C++实现的即时消息传递实用程序的模板,您可以根据您的需求进行修改和扩展:
```c++
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <thread>
#include <vector>
using namespace std;
#define MAX_BUF_SIZE 1024
#define DEFAULT_PORT 1234
// 客户端类
class Client {
public:
Client(const char* addr, int port) {
// 创建socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址和端口号
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
if (inet_pton(AF_INET, addr, &server_addr.sin_addr) <= 0) {
perror("inet_pton");
exit(EXIT_FAILURE);
}
// 连接服务器
if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(EXIT_FAILURE);
}
// 启动接收消息线程
recv_thread = thread(&Client::recv_msg, this);
}
// 发送消息
void send_msg(const char* msg) {
send(sockfd, msg, strlen(msg), 0);
}
// 接收消息
void recv_msg() {
char buf[MAX_BUF_SIZE] = {0};
while (true) {
int n = recv(sockfd, buf, MAX_BUF_SIZE, 0);
if (n <= 0) {
break;
}
printf("%s\n", buf);
memset(buf, 0, MAX_BUF_SIZE);
}
}
private:
int sockfd;
struct sockaddr_in server_addr;
thread recv_thread;
};
// 服务器类
class Server {
public:
Server(int port) {
// 创建socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址和端口号
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);
// 绑定socket和地址
if (bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
// 监听socket
if (listen(sockfd, 5) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
}
// 处理客户端连接
void handle_client(int fd) {
char username[MAX_BUF_SIZE] = {0};
char password[MAX_BUF_SIZE] = {0};
if (recv(fd, username, MAX_BUF_SIZE, 0) <= 0 || recv(fd, password, MAX_BUF_SIZE, 0) <= 0) {
close(fd);
return;
}
if (authenticate(username, password)) {
printf("%s connected\n", username);
clients.push_back(fd);
while (true) {
char buf[MAX_BUF_SIZE] = {0};
int n = recv(fd, buf, MAX_BUF_SIZE, 0);
if (n <= 0) {
break;
}
// 将消息广播给其他客户端
for (int i = 0; i < clients.size(); i++) {
if (clients[i] != fd) {
send(clients[i], buf, strlen(buf), 0);
}
}
}
printf("%s disconnected\n", username);
clients.erase(remove(clients.begin(), clients.end(), fd), clients.end());
} else {
send(fd, "Authentication failed", strlen("Authentication failed"), 0);
close(fd);
}
}
// 认证用户
bool authenticate(const char* username, const char* password) {
// TODO: 在此处添加您的认证逻辑
return true;
}
// 启动服务器
void start() {
while (true) {
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
int client_fd = accept(sockfd, (struct sockaddr*)&client_addr, &client_len);
if (client_fd < 0) {
perror("accept");
continue;
}
thread(&Server::handle_client, this, client_fd).detach();
}
}
private:
int sockfd;
struct sockaddr_in server_addr;
vector<int> clients;
};
int main(int argc, char* argv[]) {
if (argc == 2) {
Server server(atoi(argv[1]));
server.start();
} else if (argc == 3) {
Client client(argv[1], atoi(argv[2]));
char msg[MAX_BUF_SIZE];
while (fgets(msg, MAX_BUF_SIZE, stdin) != NULL) {
client.send_msg(msg);
}
} else {
printf("Usage: %s [port] (for server)\n", argv[0]);
printf(" %s [address] [port] (for client)\n", argv[0]);
return 1;
}
return 0;
}
```
上述代码提供了一个简单的服务器和客户端实现,其中服务器支持多个客户端连接,并且客户端可以发送消息给其他客户端。您可以根据您的需求对其进行修改和扩展,例如添加认证、加密、持久化存储等功能。
阅读全文