golang simple server with interactive prompt - go

I am trying to build a simple CLI interface for my go process, I need to listen and serve on TCP port and get stats when connected to the port on TCP . I came across 'terminal' package to help with Password prompt, command history and other things. I tried to write up some code to achieve this. Although my client is not receiving any prompt or server is stuck on 'ReadLine' method. Any helpers?
package main
import (
"bufio"
"fmt"
"golang.org/x/crypto/ssh/terminal"
"io"
"net"
_ "time"
)
func handleConnection(conn net.Conn) error {
fmt.Println("Handling new connection...")
defer func() {
fmt.Println("Closing connection...")
conn.Close()
}()
r := bufio.NewReader(conn)
w := bufio.NewWriter(conn)
rw := bufio.NewReadWriter(r, w)
term := terminal.NewTerminal(rw, "")
term.SetPrompt(string(term.Escape.Red) + "> " + string(term.Escape.Reset))
rePrefix := string(term.Escape.Cyan) + "Human says:" + string(term.Escape.Reset)
line := "welcome"
fmt.Fprintln(term, rePrefix, line)
for {
line, err := term.ReadLine()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
if line == "" {
continue
}
fmt.Fprintln(term, rePrefix, line)
}
}
func main() {
// Start listening to port 8888 for TCP connection
listener, err := net.Listen("tcp", ":8888")
if err != nil {
fmt.Println(err)
return
}
defer func() {
listener.Close()
fmt.Println("Listener closed")
}()
for {
// Get net.TCPConn object
conn, err := listener.Accept()
if err != nil {
fmt.Println(err)
break
}
go handleConnection(conn)
}
}

Related

How to measure RTT/latency through TCP clients (created in GoLang) from a TCP server created in GoLang?

so I am hosting a TCP server through GoLang and then I want to connect to my TCP server using multiple TCP clients and measure the RTT every time a new client is connected. I haven't found anything that allows me to measure RTT to connect to this server in Golang (like do I connect to localhost, it doesn't work) Below is my code for the TCP server.
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"
)
var counter int
const MIN = 1
const MAX = 100
func random() int {
return rand.Intn(MAX-MIN) + MIN
}
func verifyPortNo(portNo string) bool {
conn, err := net.Listen("tcp", portNo)
if err != nil {
log.Println("Connection error: ", err)
log.Println("Cannot verify port")
return false
}
log.Println("Available")
conn.Close()
return true
}
func handleConnection(con net.Conn, counter int) {
fmt.Printf("Client %d: %s\n", counter, con.LocalAddr().String())
defer con.Close()
for {
clientRequest, err := bufio.NewReader(con).ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
stop := strings.TrimSpace(clientRequest)
if stop == "STOP" {
break
}
result := strconv.Itoa(random()) + "\n"
con.Write([]byte(string(result)))
}
}
func main() {
arguments := os.Args //first element of the argument array is the program name
if len(arguments) == 1 {
fmt.Println("Please provide a port number")
return
}
PortNo := "localhost:" + arguments[1]
fmt.Println(PortNo)
if !verifyPortNo(PortNo) {
return
}
n, err := net.Listen("tcp", PortNo)
if err != nil {
fmt.Println(err)
return
}
//close the listener when the application closes
defer n.Close()
rand.Seed(time.Now().Unix())
for {
//while loop for TCP server to accept connections
conn, err := n.Accept()
if err != nil {
fmt.Println(err)
return
}
counter++
go handleConnection(conn, counter)
}
}
Below is my code for the TCP clients.
package main
import (
"bufio"
"log"
"net"
"os"
"strings"
"time"
)
var counter int
func main() {
for {
go createTCPClient()
time.Sleep(1 * time.Second)
}
// log.Println("Available")
//netstat -anp TCP | grep 9999
}
func createTCPClient() {
PortNo := "localhost:" + os.Args[1]
conn, err := net.Dial("tcp", PortNo)
if err != nil {
log.Println("Connection error: ", err)
log.Println("Cannot verify port")
return
}
defer conn.Close()
serverReader := bufio.NewReader(conn)
for {
reply, err := serverReader.ReadString('\n')
if err != nil {
println("Write to server failed:", err.Error())
os.Exit(1)
}
println("reply from server=", strings.TrimSpace(reply))
}
}
The code works (see figure below) but I cannot wrap my head around measuring the RTT for each TCP client and displaying it.
enter image description here
The only portable solution is using/designing an application protocol that lets you determine the RTT. Eg, time the difference between a request/response.
Alternatively, OS kernels often record the TCP connection latency. However:
there isn't a portable way to retrieve TCP RTT
TCP RTT isn't available on all platforms.
This cut-down example demonstrates reading the TCPInfo containing the TCP RTT under Linux:
//go:build linux
package main
import (
"fmt"
"net"
"time"
"golang.org/x/sys/unix"
)
func main() {
listener, err := net.Listen("tcp", ":0")
check(err)
fmt.Println("Listening on", listener.Addr())
for {
conn, err := listener.Accept()
check(err)
go func(conn *net.TCPConn) {
defer conn.Close()
info, err := tcpInfo(conn)
check(err)
rtt := time.Duration(info.Rtt) * time.Microsecond
fmt.Println(rtt)
}(conn.(*net.TCPConn))
}
}
func tcpInfo(conn *net.TCPConn) (*unix.TCPInfo, error) {
raw, err := conn.SyscallConn()
if err != nil {
return nil, err
}
var info *unix.TCPInfo
ctrlErr := raw.Control(func(fd uintptr) {
info, err = unix.GetsockoptTCPInfo(int(fd), unix.IPPROTO_TCP, unix.TCP_INFO)
})
switch {
case ctrlErr != nil:
return nil, ctrlErr
case err != nil:
return nil, err
}
return info, nil
}
func check(err error) {
if err != nil {
panic(err)
}
}
Example output for connections over localhost:
$ ./tcpinfo
Listening on [::]:34761
97µs
69µs
103µs
60µs
92µs

Sending binaries or strings by a client socket

I'm studying networks, and I'm doing a tcp server with Go. One of the challenges I'm studying is to send binaries or strings by a socket client to a server, save the server response to a txt, and compare it to the original data that was sent.
The problem is that the binaries do not arrive completely on the server.
Server
package main
import (
"fmt"
"io"
"log"
"net"
)
func main() {
l, err := net.Listen("tcp", ":8000")
if nil != err {
log.Println(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if nil != err {
log.Println(err)
continue
}
defer conn.Close()
go ConnHandler(conn)
}
}
func ConnHandler(conn net.Conn) {
recvBuf := make([]byte, 4096)
for {
n, err := conn.Read(recvBuf)
if nil != err {
if io.EOF == err {
log.Println(err)
return
}
log.Println(err)
return
}
if 0 < n {
data := recvBuf[:n]
fmt.Println(string(data))
}
}
}
Client
package main
import (
"fmt"
"log"
"net"
)
func main() {
conn, err := net.Dial("tcp", ":8000")
if nil != err {
log.Println(err)
}
var s string
fmt.Scanln(&s)
conn.Write([]byte(s))
conn.Close()
}
I'm generating the binaries using the command on linux:
head -c100000 /dev/urandom > binary_message.txt
I run the server:
./server > result.txt
And I send this data by the client using:
./client < binary_data.txt
In the end the file binary_data.txt have 98KB but the result .txt only has 0KB.
The problem is with scanning the binary from input. You didn't see it because the errors were ignored and not printed or otherwise handled. fmt.Scanln returns an error (so does the Write function). You should always check for possible errors happening.
I rewrote the client to load the file from disk itself as I don't think using stdin is a good fit for binary data.
package main
import (
"flag"
"io"
"log"
"net"
"os"
)
var fileName = flag.String("file", "", "file to send")
func main() {
flag.Parse()
conn, err := net.Dial("tcp", ":8000")
if nil != err {
log.Println(err)
}
defer conn.Close()
f, err := os.Open(*fileName)
if err != nil {
log.Println(err)
return
}
defer f.Close()
b := make([]byte, 1024)
for {
n, err := f.Read(b)
if err != nil {
if err == io.EOF {
log.Println("Done sending")
return
}
log.Println(err)
return
}
if _, err := conn.Write(b[:n]); err != nil {
log.Println(err)
return
}
}
}
You can use it with:
go run . -file=binary_message.txt
or if you have built the binary:
./client -file=binary_message.txt
I suggest you do the same for the server. Open a file for writing and write the binary data into that file. Use a flag to pass in the filename to write to. That will be cleaner than piping stdout to a file.

GO programming, blocking read function on a reader event

I am a beginner in Go programming and I am confused about a problem about bufio readers.
I'm programming a kind of chat client who must display and send our messages in live time. But messages that I receive are not displayed until I press enter in my terminal.
After few tests, it seems to be my "inputListener()" function being the problem because, if I put it after reading messages from server, messages from server are displaying first.
I supposed that the Read function may blocked my loop until it get a '\n' or something like that.
Here is my code:
package main
import "os"
import "strconv"
import "net"
import "bufio"
/*Recovery our input message into a buffer*/
func inputListener()([] byte){
buf := make([]byte, 512)
readerInput := bufio.NewReader(os.Stdin)
_, err := readerInput.Read(buf)
if err != nil{
panic("Error reading input.")
os.Exit(0)
}
return buf
}
func main(){
if len(os.Args) != 3{
println("Usage: ",os.Args[0], " <host> <port>\n")
os.Exit(0)
}
//Recovery the port.
port, err := strconv.Atoi(os.Args[2])
if err != nil{
panic("Error during the port recovery\n")
os.Exit(0)
}
println(port)
/*Join the adresse*/
addr := os.Args[1] + ":" + strconv.Itoa(port)
println(addr)
/* sources -- https://golang.org/pkg/net/ */
conn, err := net.Dial("tcp", addr)
if err != nil{
panic("Error connecting " + addr)
os.Exit(0)
}
buf := make([]byte, 512)
t := make([]byte, 512)
for {
/*Receive data from server*/
size, err := conn.Read(buf)
if err != nil {
panic("Error reading output.")
os.Exit(0)
}
if size >= 0{
print(string(buf[0:size]))
}
/*Data we need to send*/
t = inputListener()
if len(t) >= 0{
conn.Write(t)
}
}
conn.Close()
}
I need to press enter per messages received :/
Thank you in advance for your answers !
You be try something like this:
package main
import (
"bufio"
"io"
"net"
"os"
"strconv"
)
/*Recovery our input message into a buffer*/
func inputListener() []byte {
buf := make([]byte, 512)
readerInput := bufio.NewReader(os.Stdin)
_, err := readerInput.Read(buf)
if err != nil {
panic("Error reading input.")
}
return buf
}
func main() {
if len(os.Args) != 3 {
println("Usage: ", os.Args[0], " <host> <port>\n")
os.Exit(0)
}
//Recovery the port.
port, err := strconv.Atoi(os.Args[2])
if err != nil {
panic("Error during the port recovery\n")
}
println(port)
/*Join the adresse*/
addr := os.Args[1] + ":" + strconv.Itoa(port)
println(addr)
/* sources -- https://golang.org/pkg/net/ */
conn, err := net.Dial("tcp", addr)
if err != nil {
panic("Error connecting " + addr)
}
defer conn.Close()
go io.Copy(os.Stdout, conn)
r := bufio.NewReader(os.Stdin)
for {
p, err := r.ReadSlice('\n')
if err != nil {
panic("Error reading output.")
}
conn.Write(p)
}
}

How to listen to a client continiously using gob in Golang

In my use case I would like to continuously listen to a TCP connection and receive the value. The expected value is an object. So I am using gob decoder to receive the value from the connection. I would like to continuously listen to the connection and receive the object using go routines. I have the code snippet here[It is part of the application. code snippet does not compile]. It is getting value for the first time but not receiving for the subsequent objects.
func main() {
//...
// SOME CODE
//...
// All hosts who are connected; a map wherein
// the keys are ip addreses and the values are
//net.Conn objects
allClients := make(map[string]net.Conn)
tMaps := make(chan map[string]int64)
for {
select {
// Accept new clients
//
case conn := <-newConnections:
log.Printf("Accepted new client, #%s", hostIp)
// Constantly read incoming messages from this
// client in a goroutine and push those onto
// the tMaps channel for broadcast to others.
//
go func(conn net.Conn) {
dec := gob.NewDecoder(conn)
for {
var tMap map[string]int64
err := dec.Decode(&tMap)
if err != nil {
fmt.Println("Error in decoding ", err)
break
}
log.Printf("Received values: %+v", tMap)
//update throttle map based on the received value
tMaps <- throttleMap
}
}(conn)
}
}
Could anyone help me on this?
Let's look at the basics of a TCP server in Go.
First, there is the "listening" part. We can set that up like this:
package main
import (
"fmt"
"io"
"net"
"time"
)
func main() {
ln, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
io.WriteString(conn, fmt.Sprint("Hello World\n", time.Now(), "\n"))
conn.Close()
}
}
Notice the infinite for loop. It is always running and looping over that code. What does the code that is being looped over do? If a connection comes in on the port which is being listened on, then that connection is accepted. We then do something with that connection. In this case, we write back to it with io.WriteString. To this one connection, we are sending a response. We then close the connection. And if there are more connections, we're ready to accept them.
Now let's create a client to connect to the TCP server. This is known as "dialing" in to the TCP server.
To run all of this code on your machine, run the TCP server code above. To run the code, go to your terminal and enter: go run main.go
Now put the code directly below into another file. Launch another tab in your terminal. Run that code also by entering: go run main.go
The code below which "dials" in to your TCP server will connect to the server and the TCP server will respond, then close the connection.
Here is the code for dialing into a TCP server as a client:
package main
import (
"fmt"
"io/ioutil"
"net"
)
func main() {
conn, err := net.Dial("tcp", "localhost:9000")
if err != nil {
panic(err)
}
defer conn.Close()
bs, _ := ioutil.ReadAll(conn)
fmt.Println(string(bs))
}
We can take these basics and start having fun.
Let's create an "echo" server.
This will illustrate accepting many connections.
package main
import (
"io"
"net"
)
func main() {
ln, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
// handles unlimited connections
go func() {
io.Copy(conn, conn)
conn.Close()
}()
}
}
Run the file above the same way as before: go run main.go
If you get an error, make sure you have closed the TCP server we were running from the previous example. You close the TCP server with ctrl+c in the terminal.
Now that your new TCP server is running, let's connect to it using Telnet.
On windows you can install telnet; on Mac, it should already be there. Use this command to run telnet and connect to your TCP server: telnet localhost 9000
Now for one more example - an in-memory database like Redis:
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"strings"
)
var data = make(map[string]string)
func handle(conn net.Conn) {
defer conn.Close()
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
ln := scanner.Text()
fs := strings.Fields(ln)
if len(fs) < 2 {
io.WriteString(conn, "This is an in-memory database \n" +
"Use SET, GET, DEL like this: \n" +
"SET key value \n" +
"GET key \n" +
"DEL key \n\n" +
"For example - try these commands: \n" +
"SET fav chocolate \n" +
"GET fav \n\n\n")
continue
}
switch fs[0] {
case "GET":
key := fs[1]
value := data[key]
fmt.Fprintf(conn, "%s\n", value)
case "SET":
if len(fs) != 3 {
io.WriteString(conn, "EXPECTED VALUE\n")
continue
}
key := fs[1]
value := fs[2]
data[key] = value
case "DEL":
key := fs[1]
delete(data, key)
default:
io.WriteString(conn, "INVALID COMMAND "+fs[0]+"\n")
}
}
}
func main() {
li, err := net.Listen("tcp", ":9000")
if err != nil {
log.Fatalln(err)
}
defer li.Close()
for {
conn, err := li.Accept()
if err != nil {
log.Fatalln(err)
}
handle(conn)
}
}
And adding in concurrency:
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"strings"
)
type Command struct {
Fields []string
Result chan string
}
func redisServer(commands chan Command) {
var data = make(map[string]string)
for cmd := range commands {
if len(cmd.Fields) < 2 {
cmd.Result <- "Expected at least 2 arguments"
continue
}
fmt.Println("GOT COMMAND", cmd)
switch cmd.Fields[0] {
// GET <KEY>
case "GET":
key := cmd.Fields[1]
value := data[key]
cmd.Result <- value
// SET <KEY> <VALUE>
case "SET":
if len(cmd.Fields) != 3 {
cmd.Result <- "EXPECTED VALUE"
continue
}
key := cmd.Fields[1]
value := cmd.Fields[2]
data[key] = value
cmd.Result <- ""
// DEL <KEY>
case "DEL":
key := cmd.Fields[1]
delete(data, key)
cmd.Result <- ""
default:
cmd.Result <- "INVALID COMMAND " + cmd.Fields[0] + "\n"
}
}
}
func handle(commands chan Command, conn net.Conn) {
defer conn.Close()
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
ln := scanner.Text()
fs := strings.Fields(ln)
result := make(chan string)
commands <- Command{
Fields: fs,
Result: result,
}
io.WriteString(conn, <-result+"\n")
}
}
func main() {
li, err := net.Listen("tcp", ":9000")
if err != nil {
log.Fatalln(err)
}
defer li.Close()
commands := make(chan Command)
go redisServer(commands)
for {
conn, err := li.Accept()
if err != nil {
log.Fatalln(err)
}
go handle(commands, conn)
}
}
See my lectures from my CSUF class describing all of this here. And one more great resource.

go routine - why websocket reports the connection as closed?

I'm trying to create a client and a server using Go but for some reason the server reports the connection as "closed". As the code is trivial I can't think of anything wrong with my code. Any help is appreciated.
package main
import (
log "github.com/golang/glog"
"net/http"
"golang.org/x/net/websocket"
"time"
"flag"
)
type server struct {
payload chan string
}
// srv pushes the messages received via ws into srv.payload
func (srv *server) serve(ws *websocket.Conn) {
go func() {
var msg string
if err := websocket.Message.Receive(ws, &msg); err != nil {
log.Exit(err)
}
srv.payload <- msg
}()
return
}
// This example demonstrates a trivial client/ server.
func main() {
flag.Parse()
srv := server{payload: make(chan string, 10)}
http.Handle("/echo", websocket.Handler(srv.serve))
go func() {
err := http.ListenAndServe(":12345", nil)
if err != nil {
log.Errorf("ListenAndServe: " + err.Error())
}
}()
// give the server some time to start listening
time.Sleep(3 *time.Second)
//dial and test the response.
ws, err := websocket.Dial("ws://localhost:12345/echo", "", "http://localhost/?x=45")
if err != nil {
log.Exit(err)
}
ms := "test"
if err := websocket.Message.Send(ws, ms); err != nil {
log.Exit(err)
}
msg := <-srv.payload
if msg != ms{
log.Errorf("msg %v is not %v", ms)
}
}
Error
t.go:21] read tcp 127.0.0.1:12345->127.0.0.1:43135:
Edit:
After some try and error I've found that if I remove the go routine from the serve method it works but it doesn't make sense to me. Any idea why it doesn't work when websocket.Message.Receive is in a separate go routine?
package main
import (
log "github.com/golang/glog"
"net/http"
"golang.org/x/net/websocket"
"time"
"flag"
)
type server struct {
payload chan string
}
// srv pushes the messages received via ws into srv.payload
func (srv *server) serve(ws *websocket.Conn) {
var msg string
if err := websocket.Message.Receive(ws, &msg); err != nil {
log.Exit(err)
}
srv.payload <- msg
return
}
// This example demonstrates a trivial client/ server.
func main() {
flag.Parse()
srv := server{payload: make(chan string, 10)}
go func() {
http.Handle("/echo", websocket.Handler(srv.serve))
err := http.ListenAndServe(":12345", nil)
if err != nil {
log.Errorf("ListenAndServe: " + err.Error())
}
}()
// give the server some time to start listening
time.Sleep(3 *time.Second)
//dial and test the response.
ws, err := websocket.Dial("ws://localhost:12345/echo", "", "http://localhost/?x=45")
if err != nil {
log.Exit(err)
}
ms := "test"
if err := websocket.Message.Send(ws, ms); err != nil {
log.Exit(err)
}
msg := <-srv.payload
if msg != ms{
log.Errorf("msg %v is not %v", ms)
}
}
The websocket server closes the connection when the handler returns.
Removing the Go routine is the correct fix.

Resources