This project implements a chat server and a netcat client using the Go programming language. The server allows multiple clients to connect and send messages, while the netcat client can be used to connect to the server and send messages.
The project consists of two main files:
chat.go: Contains the code for the chat server.netcat.go: Contains the code for the netcat client.
- Go 1.22 or higher
-
Make sure you have Go installed on your system.
-
Clone this repository or download the
chat.goandnetcat.gofiles. -
Open a terminal and navigate to the project directory.
-
Run the following command to download the dependencies:
go mod download
-
In the terminal, run the following command to start the chat server:
go run chat.go -
By default, the server will run on
localhostand port8080. You can specify a different host and port using the-hostand-portflags, respectively. For example:go run chat.go -host 0.0.0.0 -port 9000
-
Open another terminal and run the following command to start the netcat client and connect to the chat server:
go run netcat.go -
By default, the client will connect to
localhostand port8080. You can specify a different host and port using the-hostand-portflags, respectively. For example:go run netcat.go -host 192.168.0.100 -port 9000 -
Once connected, you will receive a welcome message that includes your IP address and port. For example:
You are 127.0.0.1:52440 Welcome to the server -
You can now send messages to the chat server. The messages will be broadcast to all connected clients, along with your client name (IP address and port). For example:
Hello, I'm Josh 127.0.0.1:52440: Hello, I'm Josh -
When other clients send messages, you will receive them with their respective client names. For example:
127.0.0.1:52438: Hi, my name is Jose -
To exit the netcat client, press
Ctrl+C. Upon exiting, a message will be sent indicating that you have left the chat. For example:127.0.0.1:52438 has left
Here are examples of interaction between two clients:
Client 1:
./netcat
You are 127.0.0.1:52440 Welcome to the server
Hello, I'm Josh
127.0.0.1:52440: Hello, I'm Josh
What's your name?
127.0.0.1:52440: What's your name?
127.0.0.1:52438: Hi, my name is Jose
127.0.0.1:52438: Have a great day, I have to go.
127.0.0.1:52438: Bye.
127.0.0.1:52438:
127.0.0.1:52438 has left
Client 2:
./netcat
You are 127.0.0.1:52438 Welcome to the server
127.0.0.1:52440 has arrived
127.0.0.1:52440: Hello, I'm Josh
127.0.0.1:52440: What's your name?
Hi, my name is Jose
127.0.0.1:52438: Hi, my name is Jose
Have a great day, I have to go.
127.0.0.1:52438: Have a great day, I have to go.
Bye.
127.0.0.1:52438: Bye.
^C
127.0.0.1:52438:
^C
- The server uses channels to manage clients and messages.
- Three channels are defined:
incomingClients,leavingClients, andmessages, representing incoming clients, leaving clients, and messages sent by clients, respectively. - The
HandleConnectionfunction runs in a separate goroutine for each connected client. It handles the connection, sends welcome and closing messages, and forwards client messages to themessageschannel. - The
MessageWritefunction runs in a separate goroutine for each client and is responsible for sending messages from themessageschannel to the corresponding client. - The
Broadcasterfunction runs in a goroutine and is responsible for receiving messages from themessageschannel and sending them to all connected clients. - In the
mainfunction, the server is started, new client connections are accepted, and they are handled in separate goroutines using theHandleConnectionfunction.
- The netcat client connects to the chat server using the
net.Dialfunction. - A control channel
doneis created to indicate when the copying of data from the connection to the standard output is completed. - A goroutine is started that copies the received data from the connection to the standard output using
io.Copy. When the copying is complete, a signal is sent to thedonechannel. - The
CopyContentfunction is used to copy data from the standard input to the connection, allowing the user to send messages to the server. - In the
mainfunction, a connection to the server is established, goroutines are started to handle input and output data, and it waits for the data copying to complete or the program to be interrupted.
Enjoy chatting using the Go chat server and netcat client!
Este proyecto implementa un servidor de chat y un cliente netcat utilizando el lenguaje de programación Go. El servidor permite que múltiples clientes se conecten y envíen mensajes, mientras que el cliente netcat se puede utilizar para conectarse al servidor y enviar mensajes.
El proyecto consta de dos archivos principales:
chat.go: Contiene el código del servidor de chat.netcat.go: Contiene el código del cliente netcat.
- Go 1.22 o superior
-
Asegúrate de tener Go instalado en tu sistema.
-
Clona este repositorio o descarga los archivos
chat.goynetcat.go. -
Abre una terminal y navega hasta el directorio del proyecto.
-
Ejecuta el siguiente comando para descargar las dependencias:
go mod download
-
En la terminal, ejecuta el siguiente comando para iniciar el servidor de chat:
go run chat.go -
Por defecto, el servidor se ejecutará en
localhosten el puerto8080. Puedes especificar un host y puerto diferentes utilizando los flags-hosty-port, respectivamente. Por ejemplo:go run chat.go -host 0.0.0.0 -port 9000
-
Abre otra terminal y ejecuta el siguiente comando para iniciar el cliente netcat y conectarte al servidor de chat:
go run netcat.go -
Por defecto, el cliente se conectará a
localhosten el puerto8080. Puedes especificar un host y puerto diferentes utilizando los flags-hosty-port, respectivamente. Por ejemplo:go run netcat.go -host 192.168.0.100 -port 9000 -
Una vez conectado, recibirás un mensaje de bienvenida que incluye tu dirección IP y puerto. Por ejemplo:
You are 127.0.0.1:52440 Welcome to the server -
Ahora puedes enviar mensajes al servidor de chat. Los mensajes se enviarán a todos los clientes conectados, junto con tu nombre de cliente (dirección IP y puerto). Por ejemplo:
Hola, Soy Josh 127.0.0.1:52440: Hola, Soy Josh -
Cuando otros clientes envíen mensajes, los recibirás con su respectivo nombre de cliente. Por ejemplo:
127.0.0.1:52438: Hola, mi nombre es Jose -
Para salir del cliente netcat, presiona
Ctrl+C. Al salir, se enviará un mensaje indicando que has abandonado el chat. Por ejemplo:127.0.0.1:52438 has left
A continuación se muestran ejemplos de interacción entre dos clientes:
Cliente 1:
./netcat
You are 127.0.0.1:52440 Welcome to the server
Hola, Soy Josh
127.0.0.1:52440: Hola, Soy Josh
Tu como te llamas?
127.0.0.1:52440: Tu como te llamas?
127.0.0.1:52438: Hola, mi nombre es Jose
127.0.0.1:52438: que tengas un excelente dia, me tengo que ir.
127.0.0.1:52438: bye.
127.0.0.1:52438:
127.0.0.1:52438 has left
Cliente 2:
./netcat
You are 127.0.0.1:52438 Welcome to the server
127.0.0.1:52440 has arrived
127.0.0.1:52440: Hola, Soy Josh
127.0.0.1:52440: Tu como te llamas?
Hola, mi nombre es Jose
127.0.0.1:52438: Hola, mi nombre es Jose
que tengas un excelente dia, me tengo que ir.
127.0.0.1:52438: que tengas un excelente dia, me tengo que ir.
bye.
127.0.0.1:52438: bye.
^C
127.0.0.1:52438:
^C
- El servidor utiliza canales para gestionar los clientes y los mensajes.
- Se definen tres canales:
incomingClients,leavingClientsymessages, que representan los clientes entrantes, los clientes salientes y los mensajes enviados por los clientes, respectivamente. - La función
HandleConnectionse ejecuta en una goroutine separada para cada cliente que se conecta. Maneja la conexión, envía mensajes de bienvenida y cierre, y reenvía los mensajes del cliente al canalmessages. - La función
MessageWritese ejecuta en una goroutine separada para cada cliente y se encarga de enviar los mensajes del canalmessagesal cliente correspondiente. - La función
Broadcasterse ejecuta en una goroutine y se encarga de recibir los mensajes del canalmessagesy enviarlos a todos los clientes conectados. - En la función
main, se inicia el servidor, se aceptan nuevas conexiones de clientes y se manejan en goroutines separadas utilizando la funciónHandleConnection.
- El cliente netcat se conecta al servidor de chat utilizando la función
net.Dial. - Se crea un canal de control
donepara indicar cuándo se ha completado la copia de datos desde la conexión a la salida estándar. - Se inicia una goroutine que copia los datos recibidos desde la conexión a la salida estándar utilizando
io.Copy. Cuando se completa la copia, se envía una señal al canaldone. - La función
CopyContentse utiliza para copiar los datos desde la entrada estándar a la conexión, permitiendo al usuario enviar mensajes al servidor. - En la función
main, se establece la conexión con el servidor, se inician las goroutines para manejar la entrada y salida de datos, y se espera a que se complete la copia de datos o se interrumpa el programa.
¡Disfruta del chat utilizando el servidor y el cliente netcat en Go!