now I have this, but still not the correct results package main
import ( "fmt" "net" "os" //"strconv" //"bytes" //"io/ioutil" //"net/http" "reflect" //"strings" ) const ( CONN_HOST = "" CONN_PORT = "5555" CONN_TYPE = "tcp" ) func main() { // Listen for incoming connections. l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT) if err != nil { fmt.Println("Error listening:", err.Error()) os.Exit(1) } // Close the listener when the application closes. defer l.Close() fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT) for { // Listen for an incoming connection. conn, err := l.Accept() if err != nil { fmt.Println("Error accepting: ", err.Error()) os.Exit(1) } // Handle connections in a new goroutine. go handleRequest(conn) } } // Handles incoming requests. func handleRequest(conn net.Conn) { // Make a buffer to hold incoming data. buf := make([]byte, 1024) // Read the incoming connection into the buffer. reqLen, err := conn.Read(buf) buf = buf[:reqLen] if err != nil { fmt.Println("Error reading:", err.Error()) } var cadena string = "" for i := 0; i < len(buf)-1; i++ { cadena += fmt.Sprintf("%x ", buf[i]) } // Send a response back to person contacting us. conn.Write([]byte( "ok" )) fmt.Println("cadena: ", cadena, reflect.TypeOf(cadena) ) fmt.Println( "\n" ) // Close the connection when you're done with it. conn.Close() } On Sunday, July 17, 2016 at 2:05:22 PM UTC-5, Egon wrote: > > On Sunday, 17 July 2016 21:42:26 UTC+3, EdgarAlejandro Vintimilla wrote: >> >> Hi, I have a GPS that sends me data through a connection TCP >> >> the data it sends me are in ASCII, and I have to convert it to HEX >> >> for example in python I'm doing this >> >> BUFFER_SIZE = 1024 >> conn, addr = s.accept() >> data = conn.recv(BUFFER_SIZE) >> data.encode("hex") >> conn.close() >> print data >> >> and it works. >> >> but in GO, >> >> buf := make([]byte, 1024) >> reqLen, err := conn.Read(buf) >> > > Note, from here you are not using reqLen, you probably should do: > buf = buf[:reqLen], although if the conn is not fast enough you may not > receive the full request. > > >> if err != nil { >> fmt.Println("Error: ", err.Error()) >> } >> >> fmt.Println("buf: ", buf) >> fmt.Println("buf str: ", string(buf)) >> > > this []byte to string conversion assumes that "buf" is encoded as UTF8, so > if you have bytes that are larger than 0x7f you might get bizarre results. > *(Although > I know you mentioned ASCII, it might also be Extended ASCII)* > > >> var str string = "" >> for i:=0; i< len(buf); i++{ >> str += strconv.FormatUint(uint64(buf[i]), 16) >> } >> fmt.Println("str: ", str) >> >> or if I use io, error2 :=ioutil.ReadAll(connection) i do not get the >> exact data that it sends me in any way >> > > data, err := ioutil.ReadAll(conn) > if err != nil { panic(err) } > enc := hex.EncodeToString(data) > fmt.Println("hex:", enc) > > > Also you should be probably reading up to the message sequence not > everything. Protocols usually define some ending character or sequence, or > have a leading length of message. I suspect you should read up-to a > line-feed instead of everything. And also that might be the reason that you > get different results from Go and Python. > > + Egon > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.