work on socket comms, accidently break

This commit is contained in:
Dan Ballard 2014-12-30 09:32:11 -08:00
parent 4e370c272b
commit 02bd84893d
3 changed files with 29 additions and 9 deletions

View File

@ -34,6 +34,13 @@ func runRun(cmd *Command, args ...string) {
log.Fatal("Error connecting to warrend:", err)
}
s.Write([]byte(strings.Join(args, " ")))
s.Write([]byte(strings.Join(args, " ") + "\000"))
resp, err := warren.ReadStringz(s)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp)
}

View File

@ -11,19 +11,15 @@ import (
)
func cmdHandle(c net.Conn) {
fmt.Println("cmdHandle")
buf := make([]byte, 1024) // Max size of buff based on?
nr, err := c.Read(buf)
defer c.Close()
fmt.Println("cmdHandle")
data, err := warren.ReadStringz(c)
if err != nil {
fmt.Println("cmdHandle read error:", err)
fmt.Println(err)
return
}
data := buf[0:nr]
fmt.Println("cmdHandle got command:", string(data))
c.Write([]byte("echo: " + data))
}
func main() {

View File

@ -1,7 +1,9 @@
package warren
import (
"fmt"
"log"
"net"
"os"
"os/exec"
"path/filepath"
@ -40,3 +42,18 @@ func AppName() string {
paths, name := filepath.Split(paths)
return name
}
func ReadStringz(c net.Conn) (string, error) {
data := ""
buf := make([]byte, 1024) // Max size of buff based on?
for len(data) > 0 && data[len(data)-1] == 0 {
nr, err := c.Read(buf)
if err != nil {
fmt.Println("readStringz read error:", err)
return "", fmt.Errorf("readStringz error", err)
}
data += string(buf[0:nr])
}
return data, nil
}