我在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.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")
}
和我的配置讀者:
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是網關)
我試圖os.Stdout.WriteString:這個程序的
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
}
輸出(targ)或os.Stdout.WriteString(ip)就在Sendtext的頂部,並且沒有輸出。
關於它的令人困惑的事情:昨天它的工作的xD(之前我遷移ReadConfig到自己。去文件)
我希望你能幫助我解決這個...
塞勒
更新:
正如PeterSO說,這個問題是不是字符串 我的第一個猜測的切換,它必須是convers String到TCPAddr的離子是真的,但它似乎是字符串的問題,而不是網絡庫。 我剛剛在Sendtext調用後立即添加了 ip =「192.168.2.100」 port =「7896」 並且幫助...(至少直到用戶需要設置自定義IP /端口...)
我知道問題出現在我決定從goconf(http://code.google.com/p/goconf/)切換到我自己的時候。這就是爲什麼我認爲問題出在ReadProperties()函數中。
我也意識到strconv.Atoi(port)返回0(解析「7896」:無效參數) 當我使用服務器和客戶端與實現(不可變更)配置,然後讓客戶端讀取密碼從配置文件中,密碼比較失敗。當我也在代碼中設置密碼(不讀取文件)時,它可以正常工作。
我真的不知道現在該做什麼......任何想法?
謝謝,我知道所有這些問題。這是我第一次直接從文件本身讀取文件......我首先想在改進程序之前解決這個問題。我不會複製你的整個代碼,因爲我想自己做這個(但謝謝)。我在「你的ReadConfig函數有其他缺陷」之後談到了這個部分......關於make funktion和緩衝區的提示:謝謝。 –