2011-08-30 89 views
1

我在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」:無效參數) 當我使用服務器和客戶端與實現(不可變更)配置,然後讓客戶端讀取密碼從配置文件中,密碼比較失敗。當我也在代碼中設置密碼(不讀取文件)時,它可以正常工作。

我真的不知道現在該做什麼......任何想法?

回答

4

轉到字節包:func NewBuffer(buf []byte) *Buffer

NewBuffer創建和使用buf作爲其 初始內容初始化一個新的Buffer。打算編寫一個Buffer來讀取 現有的數據。它也可用於調整內部緩衝區的大小以便編寫 。要做到這一點,buf應該有所需的容量,但長度爲零。

在大多數情況下,new(Buffer)(或只是聲明一個Buffer變量) 優選NewBuffer。特別是,將非空buf 轉換爲NewBuffer,然後寫入Buffer將覆蓋buf, 不附加到它。

在你ReadConfigFile功能,你寫的:

buffer := bytes.NewBuffer(make([]byte,2048)) 
buffer.Write(part) 

make([]byte,2048)函數調用創建buffer有2048個字節的長度和容量的初始切片。函數調用buffer.Write(part)通過覆蓋buffer來寫入part。至少,你應該寫make([]byte,0,2048)開始給buffer切片長度爲零,容量爲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 
+0

謝謝,我知道所有這些問題。這是我第一次直接從文件本身讀取文件......我首先想在改進程序之前解決這個問題。我不會複製你的整個代碼,因爲我想自己做這個(但謝謝)。我在「你的ReadConfig函數有其他缺陷」之後談到了這個部分......關於make funktion和緩衝區的提示:謝謝。 –

0

在調用main函數中的Sendtext函數之前插入以下語句作爲語句。

fmt.Println("\nmain:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", message, "|") 

輸出應該是這個樣子:

main: 
ip = | 192.168.2.100 | 
port = | 7896 | 
text = | test.de 
hallo 
| 

插入下面的語句作爲Sendtext函數的第一個語句。

fmt.Println("\nSendtext:", "\nip = |", ip, "| \nport = |", port, "| \ntext = |", text, "|") 

輸出應該是這個樣子:

Sendtext: 
ip = | 192.168.2.100 | 
port = | 7896 | 
text = | test.de 
hallo 
| 

正如所料,參數是按值傳遞的參數。

+0

OK,這個問題似乎是轉換TCPAddr ......我試圖修復它現在... –

0

解決了它。問題是將2048字節的字節轉換爲字符串。這使得字符串長度相等,但後面有很多NIL字符。 因此,在ReadConfig()的最後所有值上運行ip = strings.Replace(ip,string(0),"",-1)解決了問題。

+1

你沒有修復真正的bug!首先不要創建2048個零字節!詳情請參閱我的第二個答案。 – peterSO