String 用字符串发出问题
我在Golang的弦上有点问题。似乎他们没有被移交给另一个职能部门String 用字符串发出问题,string,go,String,Go,我在Golang的弦上有点问题。似乎他们没有被移交给另一个职能部门 func Sendtext(ip string, port string, text string) (err int) { targ := ip + ":" + port raddr,e := net.ResolveTCPAddr("tcp",targ) if e != nil { os.Stdout.WriteString(e.String()+"\n") return 1 } conn,e := net.D
func Sendtext(ip string, port string, text string) (err int) {
targ := ip + ":" + port
raddr,e := net.ResolveTCPAddr("tcp",targ)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn,e := net.DialTCP("tcp",nil,raddr)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn.Write([]byte(text))
mess := make([]byte,1024)
conn.Read(mess)
message := string(mess)
conn.Close()
if message[0] == 'a' {
return 0
} else {
return 1
}
return 0
}
func main() {
os.Stdout.WriteString("Will send URL: ")
url := GetURL()
os.Stdout.WriteString(url + "\n\n")
_, port, pass, ip := browserbridge_config.ReadPropertiesFile()
os.Stdout.WriteString("sending this url to " + ip + ":" + port + "\n")
message := url + "\n" + pass + "\n"
os.Stdout.WriteString("\nsending... ")
e := Sendtext(ip, port, message)
if e != 0 {
os.Stdout.WriteString("ERROR\n")
os.Exit(e);
}
os.Stdout.WriteString("DONE\n")
}
fmt.Println("\nmain:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", message, "|")
fmt.Println("\nSendtext:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", text, "|")
和我的配置阅读器:
func ReadConfigFile(filename string) (browsercommand string, port string, pass string, ip string) {
// set defaults
browsercommand = "%u"
port = "7896"
pass = "hallo"
ip = "127.0.0.1"
// open file
file, err := os.Open(filename)
if err != nil {
os.Stdout.WriteString("Error opening config file. proceeding with standard config...")
return
}
// Get reader and buffer
reader := bufio.NewReader(file)
for {
part,_,err := reader.ReadLine()
if err != nil {
break
}
buffer := bytes.NewBuffer(make([]byte,2048))
buffer.Write(part)
s := strings.ToLower(buffer.String())
if strings.Contains(s,"browsercommand=") {
browsercommand = strings.Replace(s,"browsercommand=","",1)
} else {
if strings.Contains(s,"port=") {
port = strings.Replace(s,"port=","",1)
} else {
if strings.Contains(s,"password=") {
pass = strings.Replace(s,"password=","",1)
} else {
if strings.Contains(s,"ip=") {
ip = strings.Replace(s,"ip=","",1)
}
}
}
}
}
return
}
此程序的输出:
Will send URL: test.de
sending this url to 192.168.2.100:7896
sending...
dial tcp 192.168.2.1:0: connection refused
ERROR
192.168.2.1是网关
我试图在Sendtext的顶部添加os.Stdout.WriteStringtarg或os.Stdout.WriteStringip,但没有得到任何输出
令人困惑的是:昨天在我将ReadConfig迁移到它自己的.go文件之前,它已经工作xD了
我希望你能帮我解决这个问题
塞拉
更新:
正如彼得索所说,问题不在于字符串的移交
我的第一个猜测是,这一定是字符串到TCPAddr的转换,这是真的,但这似乎是字符串的问题,而不是网络库的问题。
我刚才补充说
ip=192.168.2.100
端口=7896
就在Sendtext呼叫之后,这有助于。。。至少在用户需要设置自定义ip/端口之前
我知道当我决定从goconf切换时,问题就出现了http://code.google.com/p/goconf/ 给我自己。这就是为什么我认为ReadProperties函数存在问题
func Sendtext(ip string, port string, text string) (err int) {
targ := ip + ":" + port
raddr,e := net.ResolveTCPAddr("tcp",targ)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn,e := net.DialTCP("tcp",nil,raddr)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn.Write([]byte(text))
mess := make([]byte,1024)
conn.Read(mess)
message := string(mess)
conn.Close()
if message[0] == 'a' {
return 0
} else {
return 1
}
return 0
}
func main() {
os.Stdout.WriteString("Will send URL: ")
url := GetURL()
os.Stdout.WriteString(url + "\n\n")
_, port, pass, ip := browserbridge_config.ReadPropertiesFile()
os.Stdout.WriteString("sending this url to " + ip + ":" + port + "\n")
message := url + "\n" + pass + "\n"
os.Stdout.WriteString("\nsending... ")
e := Sendtext(ip, port, message)
if e != 0 {
os.Stdout.WriteString("ERROR\n")
os.Exit(e);
}
os.Stdout.WriteString("DONE\n")
}
fmt.Println("\nmain:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", message, "|")
fmt.Println("\nSendtext:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", text, "|")
我还意识到strconv.Atoiport返回0:7896:无效参数
当我将服务器和客户端与实现的不可更改配置一起使用,然后让客户端从配置文件中读取密码时,密码比较失败。当我在没有读取文件的情况下在代码中正确设置密码时,它就工作了
我真的不知道现在该怎么办。。。有什么想法吗?在main函数中调用Sendtext函数之前插入以下语句作为语句
func Sendtext(ip string, port string, text string) (err int) {
targ := ip + ":" + port
raddr,e := net.ResolveTCPAddr("tcp",targ)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn,e := net.DialTCP("tcp",nil,raddr)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn.Write([]byte(text))
mess := make([]byte,1024)
conn.Read(mess)
message := string(mess)
conn.Close()
if message[0] == 'a' {
return 0
} else {
return 1
}
return 0
}
func main() {
os.Stdout.WriteString("Will send URL: ")
url := GetURL()
os.Stdout.WriteString(url + "\n\n")
_, port, pass, ip := browserbridge_config.ReadPropertiesFile()
os.Stdout.WriteString("sending this url to " + ip + ":" + port + "\n")
message := url + "\n" + pass + "\n"
os.Stdout.WriteString("\nsending... ")
e := Sendtext(ip, port, message)
if e != 0 {
os.Stdout.WriteString("ERROR\n")
os.Exit(e);
}
os.Stdout.WriteString("DONE\n")
}
fmt.Println("\nmain:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", message, "|")
fmt.Println("\nSendtext:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", text, "|")
输出应如下所示:
main:
ip = | 192.168.2.100 |
port = | 7896 |
text = | test.de
hallo
|
Sendtext:
ip = | 192.168.2.100 |
port = | 7896 |
text = | test.de
hallo
|
在Sendtext函数中插入以下语句作为第一条语句
func Sendtext(ip string, port string, text string) (err int) {
targ := ip + ":" + port
raddr,e := net.ResolveTCPAddr("tcp",targ)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn,e := net.DialTCP("tcp",nil,raddr)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn.Write([]byte(text))
mess := make([]byte,1024)
conn.Read(mess)
message := string(mess)
conn.Close()
if message[0] == 'a' {
return 0
} else {
return 1
}
return 0
}
func main() {
os.Stdout.WriteString("Will send URL: ")
url := GetURL()
os.Stdout.WriteString(url + "\n\n")
_, port, pass, ip := browserbridge_config.ReadPropertiesFile()
os.Stdout.WriteString("sending this url to " + ip + ":" + port + "\n")
message := url + "\n" + pass + "\n"
os.Stdout.WriteString("\nsending... ")
e := Sendtext(ip, port, message)
if e != 0 {
os.Stdout.WriteString("ERROR\n")
os.Exit(e);
}
os.Stdout.WriteString("DONE\n")
}
fmt.Println("\nmain:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", message, "|")
fmt.Println("\nSendtext:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", text, "|")
输出应如下所示:
main:
ip = | 192.168.2.100 |
port = | 7896 |
text = | test.de
hallo
|
Sendtext:
ip = | 192.168.2.100 |
port = | 7896 |
text = | test.de
hallo
|
正如预期的那样,参数通过值传递给参数。在main函数中调用Sendtext函数之前插入以下语句作为语句
func Sendtext(ip string, port string, text string) (err int) {
targ := ip + ":" + port
raddr,e := net.ResolveTCPAddr("tcp",targ)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn,e := net.DialTCP("tcp",nil,raddr)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn.Write([]byte(text))
mess := make([]byte,1024)
conn.Read(mess)
message := string(mess)
conn.Close()
if message[0] == 'a' {
return 0
} else {
return 1
}
return 0
}
func main() {
os.Stdout.WriteString("Will send URL: ")
url := GetURL()
os.Stdout.WriteString(url + "\n\n")
_, port, pass, ip := browserbridge_config.ReadPropertiesFile()
os.Stdout.WriteString("sending this url to " + ip + ":" + port + "\n")
message := url + "\n" + pass + "\n"
os.Stdout.WriteString("\nsending... ")
e := Sendtext(ip, port, message)
if e != 0 {
os.Stdout.WriteString("ERROR\n")
os.Exit(e);
}
os.Stdout.WriteString("DONE\n")
}
fmt.Println("\nmain:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", message, "|")
fmt.Println("\nSendtext:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", text, "|")
输出应如下所示:
main:
ip = | 192.168.2.100 |
port = | 7896 |
text = | test.de
hallo
|
Sendtext:
ip = | 192.168.2.100 |
port = | 7896 |
text = | test.de
hallo
|
在Sendtext函数中插入以下语句作为第一条语句
func Sendtext(ip string, port string, text string) (err int) {
targ := ip + ":" + port
raddr,e := net.ResolveTCPAddr("tcp",targ)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn,e := net.DialTCP("tcp",nil,raddr)
if e != nil {
os.Stdout.WriteString(e.String()+"\n")
return 1
}
conn.Write([]byte(text))
mess := make([]byte,1024)
conn.Read(mess)
message := string(mess)
conn.Close()
if message[0] == 'a' {
return 0
} else {
return 1
}
return 0
}
func main() {
os.Stdout.WriteString("Will send URL: ")
url := GetURL()
os.Stdout.WriteString(url + "\n\n")
_, port, pass, ip := browserbridge_config.ReadPropertiesFile()
os.Stdout.WriteString("sending this url to " + ip + ":" + port + "\n")
message := url + "\n" + pass + "\n"
os.Stdout.WriteString("\nsending... ")
e := Sendtext(ip, port, message)
if e != 0 {
os.Stdout.WriteString("ERROR\n")
os.Exit(e);
}
os.Stdout.WriteString("DONE\n")
}
fmt.Println("\nmain:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", message, "|")
fmt.Println("\nSendtext:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", text, "|")
输出应如下所示:
main:
ip = | 192.168.2.100 |
port = | 7896 |
text = | test.de
hallo
|
Sendtext:
ip = | 192.168.2.100 |
port = | 7896 |
text = | test.de
hallo
|
正如预期的那样,参数通过值传递给参数。解决了它。问题是将2048长[]字节转换为字符串。这使得字符串长度相等,但后面有很多零字符。
因此,在ReadConfig末尾对所有值运行ip=strings.Replaceip、string0、-1解决了这个问题。解决了这个问题。问题是将2048长[]字节转换为字符串。这使得字符串长度相等,但后面有很多零字符。 因此,在ReadConfig末尾对所有值运行ip=strings.Replaceip、string0、-1解决了问题。Go bytes包: NewBuffer创建并初始化一个新的缓冲区,使用buf作为其 初始内容。其目的是准备一个要读取的缓冲区 现有数据。它还可以用于调整内部缓冲区的大小 写为此,buf应具有所需的容量,但 长度为零 在大多数情况下,newBuffer或只是声明一个缓冲区变量 比纽伯弗更好。特别是,传递非空buf 到新缓冲区,然后写入缓冲区将覆盖buf, 不是附加到它上面 在ReadConfigFile函数中,您可以编写:
buffer := bytes.NewBuffer(make([]byte,2048))
buffer.Write(part)
make[]byte,2048函数调用为缓冲区创建长度和容量为2048字节的初始片。Writepart函数调用通过覆盖缓冲区来写入部分。至少,您应该编写make[]byte,02048,以便最初将缓冲区片的长度设置为零,容量设置为2048字节
ReadConfigFile函数还有其他缺陷。例如,key=value格式非常严格,只识别硬编码到函数中的键,如果未提供配置文件,则不会返回默认值,配置文件未关闭,等等。下面是配置文件读取器的基本实现
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Config map[string]string
func ReadConfig(filename string) (Config, os.Error) {
config := Config{
"browsercommand": "%u",
"port": "7896",
"password": "hallo",
"ip": "127.0.0.1",
}
if len(filename) == 0 {
return config, nil
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
rdr := bufio.NewReader(file)
for {
line, err := rdr.ReadString('\n')
if eq := strings.Index(line, "="); eq >= 0 {
if key := strings.TrimSpace(line[:eq]); len(key) > 0 {
value := ""
if len(line) > eq {
value = strings.TrimSpace(line[eq+1:])
}
config[key] = value
}
}
if err == os.EOF {
break
}
if err != nil {
return nil, err
}
}
return config, nil
}
func main() {
config, err := ReadConfig(`netconfig.txt`)
if err != nil {
fmt.Println(err)
}
fmt.Println("config:", config)
ip := config["ip"]
pass := config["password"]
port := config["port"]
fmt.Println("values:", ip, port, pass)
}
输入:
[a section]
key=value
; a comment
port = 80
password = hello
ip= 217.110.104.156
# another comment
url =test.de
file =
输出:
config: map[browsercommand:%u key:value port:80 ip:217.110.104.156 url:test.de
file: password:hello]
values: 217.110.104.156 80 hello
Go字节包:
NewBuffer创建并初始化一个新的缓冲区,使用buf作为其
初始内容。其目的是准备一个要读取的缓冲区
现有数据。它还可以用于调整内部缓冲区的大小
写为此,buf应具有所需的容量,但
长度为零
在大多数情况下,newBuffer或只是声明一个缓冲区变量
比纽伯弗更好。特别是,传递非空buf
到新缓冲区,然后写入缓冲区将覆盖buf,
不是附加到它上面
在ReadConfigFile函数中,您可以编写:
buffer := bytes.NewBuffer(make([]byte,2048))
buffer.Write(part)
make[]byte,2048函数调用为缓冲区创建长度和容量为2048字节的初始片。Writepart函数调用通过覆盖缓冲区来写入部分。至少,您应该编写make[]字节02048,以便最初为缓冲区片提供零长度和容量
2048字节
ReadConfigFile函数还有其他缺陷。例如,key=value格式非常严格,只识别硬编码到函数中的键,如果未提供配置文件,则不会返回默认值,配置文件未关闭,等等。下面是配置文件读取器的基本实现
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Config map[string]string
func ReadConfig(filename string) (Config, os.Error) {
config := Config{
"browsercommand": "%u",
"port": "7896",
"password": "hallo",
"ip": "127.0.0.1",
}
if len(filename) == 0 {
return config, nil
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
rdr := bufio.NewReader(file)
for {
line, err := rdr.ReadString('\n')
if eq := strings.Index(line, "="); eq >= 0 {
if key := strings.TrimSpace(line[:eq]); len(key) > 0 {
value := ""
if len(line) > eq {
value = strings.TrimSpace(line[eq+1:])
}
config[key] = value
}
}
if err == os.EOF {
break
}
if err != nil {
return nil, err
}
}
return config, nil
}
func main() {
config, err := ReadConfig(`netconfig.txt`)
if err != nil {
fmt.Println(err)
}
fmt.Println("config:", config)
ip := config["ip"]
pass := config["password"]
port := config["port"]
fmt.Println("values:", ip, port, pass)
}
输入:
[a section]
key=value
; a comment
port = 80
password = hello
ip= 217.110.104.156
# another comment
url =test.de
file =
输出:
config: map[browsercommand:%u key:value port:80 ip:217.110.104.156 url:test.de
file: password:hello]
values: 217.110.104.156 80 hello
好的,问题似乎是转换到TCPAddr。。。我正在尝试修复它…好的,问题似乎是转换到TCPADD。。。我现在正试图修复它…你没有修复真正的错误!首先不要创建2048个零字节!有关详细信息,请参阅我的第二个答案。您没有修复真正的错误!首先不要创建2048个零字节!有关详细信息,请参阅我的第二个答案。谢谢,我知道所有这些问题。这是我第一次玩直接从文件本身读取文件的游戏。。。在改进程序之前,我首先想解决这个问题。我不会复制你的全部代码,因为我想自己做这件事,但谢谢你。我谈到了ReadConfig函数有其他缺陷后的部分。。。关于制作功能和缓冲区的提示:谢谢。谢谢,我知道所有这些问题。这是我第一次玩直接从文件本身读取文件的游戏。。。在改进程序之前,我首先想解决这个问题。我不会复制你的全部代码,因为我想自己做这件事,但谢谢你。我谈到了ReadConfig函数有其他缺陷后的部分。。。关于制作功能和缓冲区的提示:谢谢。