Receive connections and add client
This commit is contained in:
commit
1cfb9cd50b
|
@ -0,0 +1,22 @@
|
|||
# If you prefer the allow list template instead of the deny list, see community template:
|
||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||
#
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
socket-server
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
|
@ -0,0 +1,10 @@
|
|||
package internal
|
||||
|
||||
import "net"
|
||||
|
||||
var Clients map[string]net.Conn
|
||||
|
||||
func AddClient(conn net.Conn) {
|
||||
addr := conn.RemoteAddr().(*net.TCPAddr).IP.String()
|
||||
Clients[addr] = conn
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
func TCPHandler(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
for {
|
||||
var buf = make([]byte, 1024)
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
log.Default().Println(err)
|
||||
return
|
||||
}
|
||||
log.Default().Println(string(buf[:n]))
|
||||
switch msg := string(buf[:n]); {
|
||||
case msg == "HELO":
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"socket-server/internal"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ln, err := net.Listen("tcp", ":65432")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
panic(err)
|
||||
}
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
panic(err)
|
||||
}
|
||||
go internal.TCPHandler(conn)
|
||||
go internal.AddClient(conn)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue