added log.h and some update
This commit is contained in:
1 parent
8e54631e4b
commit
df50428f4c
4 files changed
+81
-32
No files matched your search
+16
-13
@@ -1,13 +1,16 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <asm-generic/socket.h>
|
||||
#include <colors.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include "log.h"
|
||||
|
||||
#define PORT 5555
|
||||
#define PORT 7777
|
||||
#define MAX_CLIENTS 10
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
@@ -31,13 +34,13 @@ int main() {
|
||||
|
||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server_fd < 0) {
|
||||
perror("socket");
|
||||
log_error("socket");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int opt = 1;
|
||||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
|
||||
perror("setsockopt");
|
||||
log_error("setsockopt");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -46,12 +49,12 @@ int main() {
|
||||
address.sin_port = htons(PORT);
|
||||
|
||||
if (bind(server_fd, (struct sockaddr *) &address, sizeof(address)) < 0) {
|
||||
perror("bind");
|
||||
log_error("bind");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (listen(server_fd, 5) < 0) {
|
||||
perror("listen");
|
||||
log_error("listen");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -69,7 +72,7 @@ int main() {
|
||||
}
|
||||
|
||||
if (select(max_sd + 1, &readfds, NULL, NULL, NULL) < 0) {
|
||||
perror("select");
|
||||
log_error("select");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -78,7 +81,7 @@ int main() {
|
||||
int new_socket = accept(server_fd, (struct sockaddr *) &address,
|
||||
(socklen_t *) &addrlen);
|
||||
|
||||
printf("New connection: socket %d\n", new_socket);
|
||||
log_info("New connection: socket %d\n", new_socket);
|
||||
read(new_socket, buffer, BUFFER_SIZE);
|
||||
|
||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||
@@ -101,20 +104,20 @@ int main() {
|
||||
int valread = read(sd, buffer, BUFFER_SIZE);
|
||||
|
||||
if (valread == 0) {
|
||||
printf("Client disconnected: socket %d\n", sd);
|
||||
log_info("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);
|
||||
|
||||
log_info("[%s]: %s", sd_username, buffer);
|
||||
char *message = malloc(strlen(buffer) + strlen(sd_username) + 5);
|
||||
sprintf(message, "[%s]: %s", sd_username, 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);
|
||||
send(clients[j].sock, message, strlen(message), 0);
|
||||
free(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user