49 lines
810 B
Go
49 lines
810 B
Go
package main
|
|
|
|
package main
|
|
|
|
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() {
|
|
go server()
|
|
go client()
|
|
|
|
var input string
|
|
fmt.Scanln(&input)
|
|
} |