#tcp server mixing up connections

34 messages · Page 1 of 1 (latest)

silk idol
#

I am trying to learn about p2p networks by making a simple chatapp and I searched someways of node discovery and decided to try to make some kind of central server that peers can discover each other from I noticed a weird issue when doing that when I send a command "get_peers" the server sometimes responds to the client that actually sent that command and sometimes sends it to another client which didn't even send anything I tried debugging to see and I saw that sometimes when I send from one client it actually thinks it has got it from another client which is very odd to me at this point. I am still a beginner though I am not sure if I am doing this the correct way I am not following something just playing around also the code doesn't need to make alot of sense to u I just need help to fix this bug and would like to hear if I that way of node discovery is actually effective or should I find something else ?

code: https://pastebin.com/Ns0fv1F5

wooden pebble
#

at a very quick glance, you are returning the port instead of the IP? or is there something i dont get about it

silk idol
#

to really see what my issue is try opening up two telnet clients telnet localhost 1337 now type get_peers the data gets sent to u fine, try again u will see it gets sent to the other client

#

I am identifying nodes with their port but that

#

because I am testing on local host

#

but that is not used for connection

#

I am just storing them into a map

#

I never faced this issue before actually

#

not sure what I am doing wrong

wooden pebble
#

when a peer sent "get_peers" command
the server simply tell the peer all the connected peer's port, including the requesting peer's port

silk idol
#

yeah

wooden pebble
#

that's what i am judging will happen base on reading the code

#

'try again u will see it gets sent to the other client' i see

#

i see the problem now

silk idol
#

I am only used the peer's object that sent the message because I am already creating a goroutine for each client

silk idol
wooden pebble
#

in peer.readMsg
the read messages are simply sent into a channel
there's no association with which peer sent it
so every single peer's message will be sent into p.server.msgchan
which then a random peer in peer.writeMsg will pick it up from p.server.msgchan
and process it as if it's associated with the peer

silk idol
#

oh I see

wooden pebble
#
        fmt.Println(conn.RemoteAddr().String())
 
        msg, err := bufio.NewReader(conn).ReadString('\n')
 
        if err != nil {
            fmt.Println(err)
            continue
        }
 
        msg = strings.TrimSuffix(msg, "\r\n")
 
        //removed p.server.msgchan <- []byte(msg)
        //joined writemsg into readmsg
        msg := string(m)
 
        fmt.Printf("msg: %s recieved from: %s\n", msg, p.conn.RemoteAddr().String())
 
        switch msg {
        case "get_peers":
            p.handleGetPeers()
        }
    }
#

this way all peers will process their own commands

#

if you want to use a channel, you need to also associate the command with the peer that sent it

silk idol
#

yeah yeah right

wooden pebble
#

and instead the server would have a handle command routine
that listen to the channel where commands are sent
and the commands would include a string + the peer that sent it

#

this would be a worker approach, a worker processes all peer's command
you can also just gone with the first example, which is that each peer processes it's own command

silk idol
#

thank u

#

fixed it

#

I just putted the channel in the client struct

#

and sent the message there

#

I am not sure why tf I putted the channel in the server lmao xD

#

I don't like not being concentrated like that but anyways 😂

#

works fine now

#

@wooden pebble apart from the bug do u think my apporach is good ?

#

i mean making a centralized point for peer discovery