-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (79 loc) · 1.57 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"log"
"net"
"strconv"
"sync"
"sync/atomic"
)
type Client struct {
conn net.Conn
name string
}
var (
clients []*Client
clientsMu sync.Mutex
clientCounter int32 // Using int32 for atomic operations
)
func main() {
ln, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatalf("Error starting server: %s", err)
}
defer ln.Close()
fmt.Println("Chat server started on :8080")
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("Error accepting connection: %s", err)
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
// Add client to the list
client := &Client{
conn: conn,
name: "Client " + strconv.Itoa(int(atomic.AddInt32(&clientCounter, 1))),
}
addClient(client)
defer removeClient(client)
fmt.Fprintf(conn, "You are %s\n", client.name)
buf := make([]byte, 1024)
for {
n, err := conn.Read(buf)
if err != nil {
log.Printf("Error reading from client: %s", err)
break
}
msg := string(buf[:n])
forwardMessage(client, msg)
}
}
func addClient(client *Client) {
clientsMu.Lock()
defer clientsMu.Unlock()
clients = append(clients, client)
}
func removeClient(client *Client) {
clientsMu.Lock()
defer clientsMu.Unlock()
for i, c := range clients {
if c == client {
clients = append(clients[:i], clients[i+1:]...)
return
}
}
}
func forwardMessage(sender *Client, msg string) {
clientsMu.Lock()
defer clientsMu.Unlock()
for _, client := range clients {
if client != sender {
fmt.Fprintf(client.conn, "< %s\n", msg)
}
}
}