Sign inSign up

cloudacademydevops/tcpapp

By cloudacademydevops

Updated about 4 years ago

TCP Echo App

Image
0

434

cloudacademydevops/tcpapp repository overview

The TCP Echo App is designed to simply echo back any received data together with the senders client IP address. The application has been developed using Go and consists of the following code:

package main

import (
	"bufio"
	"fmt"
	"io"
	"net"
	"os"
)

func echo(conn net.Conn) {
	r := bufio.NewReader(conn)
	for {
		line, err := r.ReadBytes(byte('\n'))
		switch err {
		case nil:
			break
		case io.EOF:
		default:
			fmt.Println("ERROR", err)
		}

		var remoteIP = ""
		if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
			remoteIP = addr.IP.String()
		}

		conn.Write([]byte(remoteIP + ": " + string(line[:]) + "\n"))
	}
}

func main() {
	var hostport = os.Getenv("HOSTPORT")

	l, err := net.Listen("tcp", hostport)
	if err != nil {
		fmt.Println("ERROR", err)
		os.Exit(1)
	}

	for {
		conn, err := l.Accept()

		if err != nil {
			fmt.Println("ERROR", err)
			continue
		}
		go echo(conn)
	}
}

Getting Started

These instructions will cover usage information and for the docker container

Source

https://github.com/cloudacademy/tcp-echo-app

Container Parameters
docker run --name tcpapp -d -p 9091:9091 cloudacademydevops/tcpapp:v1

Tag summary

Content type

Image

Digest

sha256:bc14fa85e

Size

6.8 MB

Last updated

about 4 years ago

docker pull cloudacademydevops/tcpapp:v1