Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bufio.NewReader(os.Stdin) outside the for loop #3547

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,41 @@ The TCP client that you will create in this section will allow you to interact w
package main

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

func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide host:port.")
return
}

CONNECT := arguments[1]
c, err := net.Dial("tcp", CONNECT)
if err != nil {
fmt.Println(err)
return
}

for {
reader := bufio.NewReader(os.Stdin)
fmt.Print(">> ")
text, _ := reader.ReadString('\n')
fmt.Fprintf(c, text+"\n")

message, _ := bufio.NewReader(c).ReadString('\n')
fmt.Print("->: " + message)
if strings.TrimSpace(string(text)) == "STOP" {
fmt.Println("TCP client exiting...")
return
}
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide host:port.")
return
}

CONNECT := arguments[1]
c, err := net.Dial("tcp", CONNECT)
if err != nil {
fmt.Println(err)
return
}

inReader := bufio.NewReader(os.Stdin)
cReader := bufio.NewReader(c)
for {
fmt.Print(">> ")
text, _ := inReader.ReadString('\n')
fmt.Fprintf(c, text+"\n")

message, _ := cReader.ReadString('\n')
fmt.Print("->: " + message)
if strings.TrimSpace(string(text)) == "STOP" {
fmt.Println("TCP client exiting...")
return
}
}
}
{{< /file >}}

Expand Down Expand Up @@ -267,55 +268,57 @@ The UDP client that you will create in this section will allow you to interact w
package main

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

func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide a host:port string")
return
}
CONNECT := arguments[1]

s, err := net.ResolveUDPAddr("udp4", CONNECT)
c, err := net.DialUDP("udp4", nil, s)
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide a host:port string")
return
}
CONNECT := arguments[1]

s, err := net.ResolveUDPAddr("udp4", CONNECT)
c, err := net.DialUDP("udp4", nil, s)
if err != nil {
fmt.Println(err)
return
}

fmt.Printf("The UDP server is %s\n", c.RemoteAddr().String())
defer c.Close()

inReader := bufio.NewReader(os.Stdin)
for {
fmt.Print(">> ")
text, _ := inReader.ReadString('\n')
fmt.Fprintf(c, text+"\n")
data := []byte(text + "\n")

_, err = c.Write(data)
if err != nil {
fmt.Println(err)
return
fmt.Println(err)
return
}

fmt.Printf("The UDP server is %s\n", c.RemoteAddr().String())
defer c.Close()

for {
reader := bufio.NewReader(os.Stdin)
fmt.Print(">> ")
text, _ := reader.ReadString('\n')
data := []byte(text + "\n")
_, err = c.Write(data)
if strings.TrimSpace(string(data)) == "STOP" {
fmt.Println("Exiting UDP client!")
return
}

if err != nil {
fmt.Println(err)
return
}
if strings.TrimSpace(string(data)) == "STOP" {
fmt.Println("Exiting UDP client!")
return
}

buffer := make([]byte, 1024)
n, _, err := c.ReadFromUDP(buffer)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Reply: %s\n", string(buffer[0:n]))
buffer := make([]byte, 1024)
n, _, err := c.ReadFromUDP(buffer)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Reply: %s\n", string(buffer[0:n]))
}
}
{{< /file >}}

Expand Down