开发者

Go issue with strings

I'm having some trouble with strings in Golang. It seems that they don't get handed over to another function.

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(开发者_JAVA技巧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")
}

and my config reader:

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
}

Output of this program:

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 is gateway)

I tried to os.Stdout.WriteString(targ) or os.Stdout.WriteString(ip) just at the top of Sendtext, and got no output.

The confusing thing about it: yesterday it worked xD (before I migrated ReadConfig into its own .go file)

I hope you can help me solving this...

sylar


Update:

As PeterSO said, the problem is not the handover of the strings My first guess, that it must be the conversion of String to TCPAddr, is true, but it seems to be a problem with the strings, not with the net library. I just added ip = "192.168.2.100" port = "7896" right after the call of Sendtext, and that helped... (at least until a user needs to set a custom ip/port...)

I know that the problem firs occured when I decided to switch from goconf (http://code.google.com/p/goconf/) to my own. This is why I think the problem is in the ReadProperties() function.

I also realized that strconv.Atoi(port) returns 0 (parsing "7896": invalid argument) When I used the server and client with implemented (not-changable) config, and then let the client read the password from the config file, the password comparison fails. When I also set the password right in the code (without reading a file), it works.

I really don't know what to do now... Any idea?


Go bytes package: func NewBuffer(buf []byte) *Buffer

NewBuffer creates and initializes a new Buffer using buf as its initial contents. It is intended to prepare a Buffer to read existing data. It can also be used to size the internal buffer for writing. To do that, buf should have the desired capacity but a length of zero.

In most cases, new(Buffer) (or just declaring a Buffer variable) is preferable to NewBuffer. In particular, passing a non-empty buf to NewBuffer and then writing to the Buffer will overwrite buf, not append to it.

In your ReadConfigFile function, you write:

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

The make([]byte,2048) function call creates an initial slice for buffer with a length and capacity of 2048 bytes. The buffer.Write(part) function call writes part by overwriting buffer. At the very least, you should have written make([]byte,0,2048) to initially give the buffer slice a length of zero and capacity of 2048 bytes.

Your ReadConfigFile function has other flaws. For example, the key=value format is very rigid, only keys hardcoded into the function are recognized, if a configuration file is not given it doesn't return the defaults, the configuration file is not closed, etc. Here's a basic implementation of a configuration file reader.

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)
}

Input:

[a section]
key=value
; a comment
port = 80
  password  =  hello  
 ip= 217.110.104.156
# another comment
 url =test.de
file =

Output:

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


Insert the following statement as the statement just before the call to the Sendtext function in the main function.

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

The output should look something like this:

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

Insert the following statement as the first statement in the Sendtext function.

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

The output should look something like this:

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

As expected, the arguments are passed to the parameters by value.


Solved it. The problem was the conversion of the 2048-long []byte to a string. This made the string to be equal long, but with a lot of NIL-chars after it. So running a ip = strings.Replace(ip,string(0),"",-1) over all values at the end of ReadConfig() solved the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜