From 6ad756ce3c2afd5a3286735fec6f136906779f6c Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 25 Jun 2018 13:42:15 -0400 Subject: [PATCH] add tcp port --- shyftRingWalletConn/app.go | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 shyftRingWalletConn/app.go diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go new file mode 100644 index 0000000000..6394bd2bc3 --- /dev/null +++ b/shyftRingWalletConn/app.go @@ -0,0 +1,56 @@ +package main + +//@NOTE SHYFT main func for api, sets up router and spins up a server +//to run server 'go run shyftBlockExplorerApi/*.go' +import ( +"net" +"fmt" +"os" +) + +const ( + CONN_HOST = "localhost" + CONN_PORT = "3333" + CONN_TYPE = "tcp" +) + +func main() { + + l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT) + if err != nil { + fmt.Println("Error listening:", err.Error()) + os.Exit(1) + } + defer l.Close() + + for { + // Listen for an incoming connection. + conn, err := l.Accept() + if err != nil { + fmt.Println("Error accepting: ", err.Error()) + os.Exit(1) + } + // Handle connections in a new goroutine. + go handleRequest(conn) + } +} + +// Handles incoming requests. +func handleRequest(conn net.Conn) { + // Make a buffer to hold incoming data. + buf := make([]byte, 1024) + // Read the incoming connection into the buffer. + msg, err := conn.Read(buf) + + if err == nil { + fmt.Println("Message is ", string(buf[:msg])) + fmt.Println("Message is ", msg) + } + if err != nil { + fmt.Println("Error reading:", err.Error()) + } + // Send a response back to person contacting us. + conn.Write([]byte("Message received.")) + // Close the connection when you're done with it. + conn.Close() +}