-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (42 loc) · 768 Bytes
/
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
package main
import (
"fmt"
"log"
"net"
)
type Server struct {
Address string
Port string
}
func NewServer(addr, port string) *Server {
return &Server{
Address: addr,
Port: port,
}
}
func (s *Server) Run() {
log.Printf("Server working on: %s:%s", s.Address, s.Port)
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%s", s.Address, s.Port))
if err != nil {
log.Fatal("Error creating listener: ", err)
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Error accepting connection: ", err)
}
c := &Client{
Connection: conn,
}
go func() {
if err := c.HandleConnection(); err != nil {
log.Println(err)
}
}()
}
}
func main() {
tcp := NewServer("", "8080")
tcp.Run()
}