函数中存在循环问题



我有一个运行的小型TCP服务器,用户可以通过telnet进行连接。有一个";菜单";选项循环(例如触发的单个字符输入(;L";选项允许连接的用户";留言">

但是:

  1. 从连接菜单中进行选择(L(时,它会复制/重复菜单
  2. 然后,需要额外/不必要的按键才能看到回显给用户的类型

我怀疑我有循环问题,但我不知道它在哪里坏了。

有什么想法吗?

package main
import (
"bytes"
"fmt"
"log"
"net"
)
const (
port = "5555"
)
var arr []string
type Client struct {
c        net.Conn
dataType string
menuCurr string
}
func NewClient() *Client {
return &(Client{})
}
func waitForInput(didInput chan<- bool) {
// Wait for a valid input here
didInput <- true
}
func main() {
log.Printf("Hello Server!")
service := ":5555"
tcpAddr, error := net.ResolveTCPAddr("tcp", service)
if error != nil {
log.Printf("Error: Could not resolve address")
} else {
netListen, error := net.Listen(tcpAddr.Network(), tcpAddr.String())
if error != nil {
log.Fatal(error)
} else {
defer netListen.Close()
for {
log.Printf("Waiting for clients")
conn, error := netListen.Accept()
if error != nil {
log.Print("Client error: ", error)
} else {
log.Printf("Client connected %s -> %s n", conn.RemoteAddr(), conn.LocalAddr())
go handler(conn)
}
}
}
}
}
func removeClient(conn net.Conn) {
log.Printf("Client %s disconnected", conn.RemoteAddr())
conn.Close()
}
func handler(conn net.Conn) {
defer removeClient(conn)
errorChan := make(chan error)
dataChan := make(chan []byte)
// Set defaults for incoming connections
var s *Client
s = NewClient()
s.c = conn
s.dataType = "key"
s.menuCurr = "connect" // first menu every user sees
go readWrapper(conn, dataChan, errorChan)
r := bytes.NewBuffer(make([]byte, 0, 1024))
// default menu
fmt.Fprintf(conn, "Select an option:rnn[L] Leave Messagern[!] DisconnectrnnCmd? ")
for {
select {
case data := <-dataChan:
if s.menuCurr == "connect" {
fmt.Fprintf(conn, "Select an option:rnn[L] Leave Messagern[!] DisconnectrnnCmd? ")
}
if s.menuCurr == "message" {
fmt.Fprintf(conn, "Type a mesage. Escape to quit. rnn")
}
// "key" responds to single character input
if s.dataType == "key" {
switch string(data) {
default:
fmt.Println("client hit invalid key...")
continue
case "L", "l":
s.dataType = "text"
s.menuCurr = "message"
continue
case "!":
fmt.Fprintf(conn, " Bye!")
fmt.Println("client chose to exit...")
break
}
}
// "Text" allows for free typing
if s.dataType == "text" {
for {
select {
case data := <-dataChan:
fmt.Fprintf(conn, string(data))
if bytes.Equal(data, []byte("rn")) || bytes.Equal(data, []byte("r")) {
fmt.Fprintf(conn, "you typed: %qrn", r.String())
r.Reset()
s.dataType = "key"
s.menuCurr = "connect"
break
}
if bytes.Equal(data, []byte("33")) {
fmt.Fprintf(conn, "rnAborted!rn")
r.Reset()
s.dataType = "key"
s.menuCurr = "connect"
break
}
r.Write(data)
}
log.Printf("Client %s sent: %q", conn.RemoteAddr(), string(data))
if s.menuCurr == "connect" {
break
}
}
continue
}
case err := <-errorChan:
log.Println("An error occured:", err.Error())
return
}
conn.Close()
}
}
func readWrapper(conn net.Conn, dataChan chan []byte, errorChan chan error) {
for {
buf := make([]byte, 1024)
reqLen, err := conn.Read(buf)
if err != nil {
errorChan <- err
return
}
dataChan <- buf[:reqLen]
}
}

所以你几乎成功了!

  1. 需要额外按键的原因是您的选择大小写在接收到另一个字符之前不会进行处理。基本上你落后了一个角色。在收集来自用户的消息时,您还需要注意这一点

以下是我能做的工作。我的telnet客户端在我点击return之前不会发送(我在windows上,为什么\r\n而不仅仅是(,所以我需要去掉这些额外的字符。

package main
import (
"bytes"
"fmt"
"log"
"net"
"strings"
)
const (
port = "5555"
)
var arr []string
type Client struct {
c        net.Conn
dataType string
menuCurr string
}
func NewClient() *Client {
return &(Client{})
}
func waitForInput(didInput chan<- bool) {
// Wait for a valid input here
didInput <- true
}
func main() {
log.Printf("Hello Server!")
service := ":5555"
tcpAddr, error := net.ResolveTCPAddr("tcp", service)
if error != nil {
log.Printf("Error: Could not resolve address")
} else {
netListen, error := net.Listen(tcpAddr.Network(), tcpAddr.String())
if error != nil {
log.Fatal(error)
} else {
defer netListen.Close()
for {
log.Printf("Waiting for clients")
conn, error := netListen.Accept()
if error != nil {
log.Print("Client error: ", error)
} else {
log.Printf("Client connected %s -> %s n", conn.RemoteAddr(), conn.LocalAddr())
go handler(conn)
}
}
}
}
}
func removeClient(conn net.Conn) {
log.Printf("Client %s disconnected", conn.RemoteAddr())
conn.Close()
}
func handler(conn net.Conn) {
defer removeClient(conn)
errorChan := make(chan error)
dataChan := make(chan []byte)
// Set defaults for incoming connections
var s *Client
s = NewClient()
s.c = conn
s.dataType = "key"
s.menuCurr = "connect" // first menu every user sees
go readWrapper(conn, dataChan, errorChan)
r := bytes.NewBuffer(make([]byte, 0, 1024))
// default menu
fmt.Fprintf(conn, "Select an option:rnn[L] Leave Messagern[!] DisconnectrnnCmd? ")
for {
select {
case data := <-dataChan:
// notice how i removed the current menu state
// "key" responds to single character input
if s.dataType == "key" {
t := strings.TrimSuffix(strings.TrimSuffix(string(data), "rn"), "n")
switch t {
default:
fmt.Println("client hit invalid key...")
// remove the continue since the menu prints at the bottom
// continue
case "L", "l":
s.dataType = "text"
s.menuCurr = "message"
// notice the message here and the break instead of the continue.
// if we use continue instead it will wait until your user sends something
// with a break instead it will fall through and start collecting the text
fmt.Fprintf(conn, "Type a mesage. Escape to quit. rnn")
break
case "!":
fmt.Fprintf(conn, " Bye!")
fmt.Println("client chose to exit...")
// tell current menu to exit
s.menuCurr = "exit"
}
}
// "Text" allows for free typing
if s.dataType == "text" {
for {
select {
case data := <-dataChan:
fmt.Fprintf(conn, string(data))
if bytes.Equal(data, []byte("rn")) || bytes.Equal(data, []byte("r")) {
fmt.Fprintf(conn, "you typed: %qrn", r.String())
r.Reset()
s.dataType = "key"
s.menuCurr = "connect"
break
}
if bytes.Equal(data, []byte("33")) || bytes.Equal(data, []byte("33rn")) {
fmt.Fprintf(conn, "rnAborted!rn")
r.Reset()
s.dataType = "key"
s.menuCurr = "connect"
break
}
r.Write(data)
}
log.Printf("Client %s sent: %q", conn.RemoteAddr(), r.String())
if s.menuCurr == "connect" {
break
}
}
}
if s.menuCurr == "connect" {
fmt.Fprintf(conn, "Select an option:rnn[L] Leave Messagern[!] DisconnectrnnCmd? ")
}
// fall through statement to close connection
if s.menuCurr == "exit" {
break
}
// otherwise continue printing menu for invalid submissions
continue
case err := <-errorChan:
log.Println("An error occured:", err.Error())
return
}
fmt.Println("closing")
break
}
conn.Close()
}
func readWrapper(conn net.Conn, dataChan chan []byte, errorChan chan error) {
for {
buf := make([]byte, 1024)
reqLen, err := conn.Read(buf)
if err != nil {
errorChan <- err
return
}
dataChan <- buf[:reqLen]
}
}

当使用库存telnet命令行程序时,它通常会与服务器协商以在LINEMODE中运行。在这种模式下,为了节省带宽,将缓冲用户输入。当用户点击:时,数据被刷新(即发送到服务器(

  • Enter;或
  • Ctrl-D

您可以在当前状态下使用服务器,但必须键入击键:L,然后键入Ctrl-D(以强制刷新(。然后客户端将收到预期的服务器响应:type your message

您的服务器代码需要一个单一的按键笔划(L!(,因此如果您先按L,然后按Enter,那么服务器收到的消息将是Lrn,这就是您的服务器编码触发的地方:

if s.dataType == "key" {
switch string(data) {
default:
fmt.Printf("client hit invalid key... (%q)n", string(data)) // <- `Lrn`
continue
case "L", "l":
s.dataType = "text"
s.menuCurr = "message"
continue
case "!":
fmt.Fprintf(conn, " Bye!")
fmt.Println("client chose to exit...")
break
}
}