0

I'm pretty news in Golang and only use sockets in this langage for 2days. However, I'm not sure to understand something. I know in C, I used select() to know who wrote etc, but here, no one is writing until one send a message. After this message sent, my dialTCP uses it endlessly.

I think I missunderstood something about close() but I'm not sure it comes from here.. there is my code:

package dial

import (
"errors"
"encoding/json"
m "models"
"net"
"net/http"
"time"
"server"
)

type DialTCP struct {}

// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
// go away.
type tcpKeepAliveListener struct {
    *net.TCPListener
}

func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
    tc, err := ln.AcceptTCP()
    if err != nil {
            return
    }
    tc.SetKeepAlive(true)
    tc.SetKeepAlivePeriod(3 * time.Minute)
    return tc, nil
 }

 func (dialTCP *DialTCP) ListenAndServe(addr string) error {
    ln, err := net.Listen("tcp", addr)
    if err != nil {
            return err
    }
    return dialTCP.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
 }

 func (dialTCP *DialTCP) Serve(l net.Listener) error {
    defer l.Close()

    for {
            conn, e := l.Accept()
            if e != nil {
                    return e
            }

            // you want to create server_conn here with buffers, channels and stuff
            // to use async thread safe read/write from it
            go dialTCP.serve_conn(conn)
    }
 }

 func (dialTCP *DialTCP) serve_conn(conn net.Conn) error {
    // var buf [512]byte
    dec := json.NewDecoder(conn)

    //read 1st message he sent, should be token to connect
    var auth m.DialAuthentication
    dec.Decode(&auth)
    user := m.User{
    UUID: auth.UUID,
    }

    ok, sb := server.IsConnected(user)
    if ok == false {
            json.NewEncoder(conn).Encode(sb)
            return errors.New("User isn't connected.")
    } else {
            user.Conn = conn
    }
    //defer conn.Close()

    var message m.DialMessageContainer
    for {
            dec.Decode(&message)

            switch message.Type {
            case ".....":
            /*    ....(message, user)
            case "....":
                ....(message, user)
//          case "...":*/

            default:
                    json.NewEncoder(conn).Encode(m.StatusBack{Description: "Bad entry.", StatusId: http.StatusNotAcceptable})
            }
            //defer conn.Close()
    }
}

I think everything is good before serv_conn(), but the error should comes from inside the for. I tried lot of things, but this for{} in Golang... Why does it have not any params/var such as C/C++/C#/Java?

for (int i = 0; i < 10; i++) {}

I'm lost about the closing of the Conn as well, so I continue to read tutorial, post and doc about how to use it.. Days pass whitout find anything

Emixam23
  • 3,854
  • 8
  • 50
  • 107
  • 1
    what's your problem? – Jiang YD May 27 '16 at 09:58
  • Like I said, when my TCPserver get a message, it doesn't care about if a new message comes up, "my dialTCP uses it endlessly." It uses the same first message it got, so why? Am I clear? :) – Emixam23 May 27 '16 at 10:18
  • Um. The question is not clear to me... though I am still getting through the first cut of coffee for today. One thing I can answer right away `for{...}` is an infinite loop, comparable to something like `while(true)` in another C-like language. In Go `for` does the duty of various loops. See this: https://golang.org/doc/effective_go.html#for for details. – Snowman May 27 '16 at 13:06
  • Ok but why in this case it doesn't wait a new message to continue? Why it just acts like while(true) ? – Emixam23 May 27 '16 at 16:00

0 Answers0