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(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?