I am unable to call into the TCP server I created in Visual Studio!


(ReuvenA) #1

The problem: I am unable to call into the TCP server I created in Visual Studio!

TCP server - write to connection

We can create our own tcp server using the net package from the standard library. There are three main steps: (1) Listen (2) Accept (3) Write or Read to the connection. We will use telnet to call into the TCP server we created. Telnet provides bidirectional interactive text-oriented communication using a virtual terminal connection over the Transmission Control Protocol (TCP).

My OS is Macbook running macOS Sierra Version 10:12.6

My text editor is Visual Studio Code

My terminal on my mac Terminal 2.7.5

Visual Studio

package main

import (

"fmt"

"io"

"log"

"net"

)

func main() {

li, err := net.Lis

ten("tcp", ":8080")

if err != nil {

log.Fatalln(err)s

}

defer li.Close()

for {

conn, err := li.Accept()

if err != nil {

log.Println(err)

continue

}

io.WriteString(conn, "\nHello from TCP server\n")

fmt.Fprintln(conn, "How is your day?")

fmt.Fprintf(conn, "%v", "Well, I hope!")

conn.Close()

}

}

/Users/reuvenabramson/Documents/goworkspace/src/GoesToEleven/golang-web-dev-master/015_understanding-TCP-servers/01_write

Reuvens-Air:01_write reuvenabramson$ go run main.go

command-line-arguments

./main.go:11:13: undefined: net.Lis

./main.go:13:2: undefined: ten

Reuvens-Air:01_write reuvenabramson$

My terminal

Reuvens-Air:01_write reuvenabramson$ go run main.go

command-line-arguments

./main.go:11:13: undefined: net.Lis

./main.go:13:2: undefined: ten

Reuvens-Air:01_write reuvenabramson$ nc localhost 8080

Reuvens-Air:01_write reuvenabramson$ localhost 8080

-bash: localhost: command not found

Reuvens-Air:01_write reuvenabramson$

Chrome Browser

Localhost: 8080

This site can’t be reached

localhost refused to connect.

ERR_CONNECTION_REFUSED


(Norbert Melzer) #2

Just try to read the error message and check your code.

net.Lis and ten are both unknown to the compiler. Remove the linebreak inbetween to make it net.Listen.


(ReuvenA) #3

Hello Norbert from Reuven! Thank you for your help it worked.

Greatly appreciated!!

Regards