communication working on server and client mode
This commit is contained in:
59
server.go
Normal file
59
server.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
CONN_HOST = "192.168.8.177"
|
||||
CONN_PORT = "3333"
|
||||
CONN_TYPE = "tcp"
|
||||
CONN_URL = CONN_HOST + ":" + CONN_PORT
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Listen for incoming connections
|
||||
l, err := net.Listen(CONN_TYPE, CONN_URL)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error listening:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Close the listener when this application closes
|
||||
defer l.Close()
|
||||
|
||||
fmt.Println("Listening on " + CONN_URL)
|
||||
for {
|
||||
// Listen for connections
|
||||
conn, err := l.Accept()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error accepting connection:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
go handleRequest(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func handleRequest(conn net.Conn) {
|
||||
// Buffer that holds incoming information
|
||||
buf := make([]byte, 1024)
|
||||
|
||||
for {
|
||||
len, err := conn.Read(buf)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error reading:", err.Error())
|
||||
break
|
||||
}
|
||||
|
||||
s := string(buf[:len])
|
||||
|
||||
fmt.Println(s)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user