less
This commit is contained in:
parent
4928f24e79
commit
fee2134652
@ -1,49 +1,24 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
package main
|
import ("net")
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/gob"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
func server() {
|
|
||||||
// listen on a port
|
|
||||||
ln, err := net.Listen("tcp", ":9999")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for {
|
|
||||||
// accept a connection
|
|
||||||
c, err := ln.Accept()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// handle the connection
|
|
||||||
go handleServerConnection(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleServerConnection(c net.Conn) {
|
|
||||||
// receive the message
|
|
||||||
var msg string
|
|
||||||
err := gob.NewDecoder(c).Decode(&msg)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
} else {
|
|
||||||
fmt.Println("Received", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go server()
|
listener, _ := net.Listen("tcp", "0.0.0.0:5000")
|
||||||
go client()
|
for {
|
||||||
|
conn, _ := listener.Accept()
|
||||||
var input string
|
go Serve(conn)
|
||||||
fmt.Scanln(&input)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Serve(conn net.Conn) {
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
for {
|
||||||
|
_, err := conn.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
conn.Write(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user