initial commit
This commit is contained in:
commit
8e54631e4b
8 files changed
+303
No files matched your search
+126
@@ -0,0 +1,126 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <asm-generic/socket.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define PORT 5555
|
||||
#define MAX_CLIENTS 10
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
struct client_t {
|
||||
int sock;
|
||||
char username[50];
|
||||
};
|
||||
|
||||
int main() {
|
||||
int server_fd;
|
||||
struct client_t clients[MAX_CLIENTS];
|
||||
struct sockaddr_in address;
|
||||
int addrlen = sizeof(address);
|
||||
fd_set readfds;
|
||||
char buffer[BUFFER_SIZE];
|
||||
|
||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||
clients[i].sock = 0;
|
||||
bzero(clients[i].username, sizeof(clients[i].username));
|
||||
}
|
||||
|
||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server_fd < 0) {
|
||||
perror("socket");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int opt = 1;
|
||||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
|
||||
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");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (listen(server_fd, 5) < 0) {
|
||||
perror("listen");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Chat server started on port %d\n", PORT);
|
||||
|
||||
while (1) {
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(server_fd, &readfds);
|
||||
int max_sd = server_fd;
|
||||
|
||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||
int sd = clients[i].sock;
|
||||
if (sd > 0) FD_SET(sd, &readfds);
|
||||
if (sd > max_sd) max_sd = sd;
|
||||
}
|
||||
|
||||
if (select(max_sd + 1, &readfds, NULL, NULL, NULL) < 0) {
|
||||
perror("select");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Новое подключение
|
||||
if (FD_ISSET(server_fd, &readfds)) {
|
||||
int new_socket = accept(server_fd, (struct sockaddr *) &address,
|
||||
(socklen_t *) &addrlen);
|
||||
|
||||
printf("New connection: socket %d\n", new_socket);
|
||||
read(new_socket, buffer, BUFFER_SIZE);
|
||||
|
||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||
if (clients[i].sock == 0) {
|
||||
clients[i].sock = new_socket;
|
||||
strcpy(clients[i].username, buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Сообщения от клиентов
|
||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||
int sd = clients[i].sock;
|
||||
char sd_username[50];
|
||||
bzero(sd_username, sizeof(sd_username));
|
||||
strcpy(sd_username, clients[i].username);
|
||||
|
||||
if (FD_ISSET(sd, &readfds)) {
|
||||
int valread = read(sd, buffer, BUFFER_SIZE);
|
||||
|
||||
if (valread == 0) {
|
||||
printf("Client disconnected: socket %d\n", sd);
|
||||
close(sd);
|
||||
clients[i].sock = 0;
|
||||
bzero(clients[i].username, sizeof(clients[i].username));
|
||||
} else {
|
||||
buffer[valread] = '\0';
|
||||
printf("from_socket: %d\n", sd);
|
||||
printf("from_username: %s\n", sd_username);
|
||||
printf("Message: %s\n", buffer);
|
||||
|
||||
// Рассылка всем другим
|
||||
for (int j = 0; j < MAX_CLIENTS; j++) {
|
||||
if (clients[j].sock != 0 && clients[j].sock != sd) {
|
||||
send(clients[j].sock, buffer, strlen(buffer), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user