Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Andy Hall
Yep I did this in the end and it works fine... var user string for rows_users.Next() { rows_users.Scan() if conn, ok := m[user]; ok { conn.Write([]byte(string(username + " has entered the room\n# "))) } } I think I will back off the project and go back to

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Brian Candler
On Saturday, 12 September 2020 19:36:18 UTC+1, Andy Hall wrote: > > to handle the map not returning a result ( so I don't attempt to > write to a non-existent connection ) I need to know the zero value of > type net.Conn > > net.Conn is an interface , and

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Andy Hall
So far this is working... // create map var m = make(map[string]net.Conn) // populate map with user connection m[username] = c // get user connection from map conn := m[username] // write to user connection conn.Write([]byte(string(username + " has entered the room\n# "))) but to handle the map

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Stephan Lukits
On 9/12/20 8:27 PM, Andy Hall wrote: Thanks Martin...I'll be sure to add a mutex lock and unlock either side of the map call...probably shouldn't have chosen a networked multi-player game as my first project but I sure am learning a lot !! It's a very fun language too. There is also *sync.Map*

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Andy Hall
thanks brian...I have looked at sync / channels but I think the simplicity of mutex should work fine for me. On Sat, 12 Sep 2020 at 18:13, Brian Candler wrote: > > On Saturday, 12 September 2020 17:04:19 UTC+1, mb0 wrote: >> >> a global map is the way to go, however writing an reading the map

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Andy Hall
Thanks Martin...I'll be sure to add a mutex lock and unlock either side of the map call...probably shouldn't have chosen a networked multi-player game as my first project but I sure am learning a lot !! It's a very fun language too. On Sat, 12 Sep 2020 at 17:03, Martin Schnabel wrote: > > Hi

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Brian Candler
On Saturday, 12 September 2020 17:04:19 UTC+1, mb0 wrote: > > a global map is the way to go, however writing an reading the map from > multiple go routines will fail (in your case different handle calls for > connections) because the map data structure is not safe for concurrent use > and must

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Martin Schnabel
Hi Andy, a global map is the way to go, however writing an reading the map from multiple go routines will fail (in your case different handle calls for connections) because the map data structure is not safe for concurrent use and must be coordinated. the way it is usually solved is by adding

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Andy Hall
OK so I just moved the declaration of the map to the package itself which makes it universal...all working as expected. Thanks. On Saturday, September 12, 2020 at 1:35:49 PM UTC+1 Andy Hall wrote: > So this works fine... > > func handleConnection(c net.Conn) { > // get user details > username

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Andy Hall
So this works fine... func handleConnection(c net.Conn) { // get user details username := createUser(c, "Please enter you username (new users will be created / existing users will be loaded): ") // map username to connection var m = make(map[string]net.Conn) m[username] = c n := len(m)

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-12 Thread Andy Hall
I think this is exactly what I need to do...thanks very much. I'm looking forward to implementing it. On Saturday, September 12, 2020 at 5:43:09 AM UTC+1 Shulhan wrote: > > > Pada tanggal Sab, 12 Sep 2020 02.54, Andy Hall > menulis: > >> if I have multiple clients connected to a tcp server

Re: [go-nuts] writing to net.Conn tcp socket

2020-09-11 Thread Mhd Shulhan
Pada tanggal Sab, 12 Sep 2020 02.54, Andy Hall menulis: > if I have multiple clients connected to a tcp server and I wish to write > back to specific connections I can record the net.Conn value and then use > the Write method on it...but when using Println I get the following for two >

[go-nuts] writing to net.Conn tcp socket

2020-09-11 Thread Andy Hall
if I have multiple clients connected to a tcp server and I wish to write back to specific connections I can record the net.Conn value and then use the Write method on it...but when using Println I get the following for two clients... &{{0xc94000}} &{{0xc94080}} which when testing with