-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
56 lines (44 loc) · 1005 Bytes
/
server.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
package main
import (
"log"
"net"
"github.com/leNicDev/retromc/packethandler"
)
const (
CON_HOST = "localhost"
CON_PORT = "25565"
CON_TYPE = "tcp"
)
func main() {
l, err := net.Listen(CON_TYPE, CON_HOST+":"+CON_PORT)
if err != nil {
log.Panicln("Failed to bind to address", err.Error())
}
// close listener when the application closes
defer l.Close()
log.Println("Server listening on " + CON_HOST + ":" + CON_PORT)
for {
// listen for incoming connections
connection, err := l.Accept()
if err != nil {
log.Fatalln("Failed to accept connection: ", err.Error())
continue
}
// handle connection
go handleConnection(connection)
}
}
func handleConnection(connection net.Conn) {
// create buffer to hold incoming data
var buf []byte
for {
buf = make([]byte, 1024)
// read incoming data
_, err := connection.Read(buf)
if err != nil {
log.Fatalln("Failed to read packet: ", err.Error())
return
}
packethandler.HandlePacket(connection, &buf)
}
}