← ProgettiProjects

04CodiceCode

Multiple-Chat

Applicazione di chat di gruppo in C per la comunicazione tra più host in rete.

Group chat application in C for communication between multiple hosts over a network.

Fig. 1 Multiple-Chat

L'ideaThe idea

Multiple-Chat è una chat di gruppo da terminale, scritta interamente in C, con cui più host della stessa rete si scambiano messaggi. Lo stesso eseguibile può fare da server o da client: basta scegliere la modalità all'avvio.

Multiple-Chat is a terminal group chat, written entirely in C, that lets several hosts on the same network exchange messages. The same executable can act as server or client: you just pick the mode at startup.

Il serverThe server

Il server apre un socket TCP, accetta le connessioni e avvia un thread per ogni client. Quando arriva un messaggio lo inoltra a tutti gli altri partecipanti, in modo simile a un flooding. I client connessi sono tenuti in una lista concatenata protetta da un mutex, così più thread possono aggiungerli e rimuoverli senza conflitti.

The server opens a TCP socket, accepts connections and starts a thread for each client. When a message arrives it forwards it to every other participant, much like flooding. Connected clients are kept in a linked list guarded by a mutex, so several threads can add and remove them safely.

Il clientThe client

Lato client due thread lavorano in parallelo, uno per ricevere e uno per inviare. L'interfaccia è disegnata con ncurses e divide il terminale in tre finestre: una per scrivere, una per i propri messaggi e una per quelli degli altri. I comandi /cls e /exit puliscono la cronologia ed escono dalla chat; a fine sessione viene scritto un file di log con tutto quello che è successo.

On the client side two threads run in parallel, one receiving and one sending. The interface is drawn with ncurses and splits the terminal into three windows: one to type in, one for your own messages and one for everyone else's. The /cls and /exit commands clear the history and leave the chat; at the end of a session a log file records everything that happened.

AvvioRunning it

Uno script Bash avvia il server con multiple-chat.sh -s, oppure un client con multiple-chat.sh -c seguito da indirizzo IP, porta del server e nome da usare nella chat.

A Bash script starts the server with multiple-chat.sh -s, or a client with multiple-chat.sh -c followed by the server's IP address, port and the name to use in the chat.