Pada tanggal Sab, 12 Sep 2020 02.54, Andy Hall <andyjohnh...@gmail.com>
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
> clients...
>
> &{{0xc000094000}}
> &{{0xc000094080}}
>
> which when testing with a simple write doesn't work...
>
> package main
> import "net"
> var c net.Conn = "&{{0xc000094000}}"
> func writeConn(c net.Conn) {
> c.Write([]byte(string("Hello\n")))
> }
> func main() {
> writeConn(c)
> }
>
> ...and results in the following...
>
> cannot use "&{{0xc000094000}}" (type string) as type net.Conn in assignment
>
> clearly using Println to output the net.Conn is not a viable var to use so
> how could I do this ? I intend to record each users net.Conn in a database
> which can then be queried as required.
>
> any help would be most greatly appreciated.
>


Either I miss something or Go has different socket concept, but last time I
learn this is not how the network socket works in general.

First, &{{0xc000094000}} is the address of a variable. You can't convert an
address from string back to variable, because that would be security issue.
Usually socket connection is signed integer, in C you can assign integer
value to variable let other process write into it. But in Go, connection is
an interface/structure.

If you want to record each users, you have two options:

1) Let the user send unique ID (for example their user ID or email or
username) on first accept.

2) Get unique ID from connection IP address (beware that two or more
connection may come from the same IP address).

You then must have a map that store unique ID as key and net.Conn as value.
So, if you want to send some value to specific user, you query the map
first and if exist then you can proceeds.

I hope that helps.

>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAMh9%2BYA1t4ii3QgjMitYKPygQmPCxAJSSaJnctFy5i1t8zKjzA%40mail.gmail.com.

Reply via email to