I agree with what Brian said and would like to suggest the following two way RPC library with multiplexing over a single connection of any type:
https://github.com/aperturerobotics/starpc On Thu, Dec 28, 2023, 10:06 AM 'Brian Candler' via golang-nuts < golang-nuts@googlegroups.com> wrote: > What do you mean by "Real IP" and in particular how does it differ from > the TCP source address? If the client is behind a NAT, then (a) you won't > know the client's real IP unless they tell it to you (and you trust what > they say), and (b) you won't be able to connect to that address anyway. > > If your actual requirement is to issue RPC calls in the 'reverse' > direction down the same TCP connection, then you can look at how it's done > here: > https://github.com/cenkalti/rpc2 > > On Thursday 28 December 2023 at 15:31:02 UTC York gao wrote: > >> I have a requirement that requires the rpc server to get the client’s >> **real IP** and current connection `net.Conn`. >> >> The reason for needing **real IP** and `net.Conn` is roughly for >> **two-way communication**. I hope to be able to send RPC requests to the >> client of a specific IP to perform certain operations. For example, issue >> RPC commands to certain Agent services to execute a certain script. >> >> I can get the remoteip through the following RemoteAddr method, but this >> is not necessarily the real IP of the client. >> ```go >> // example_server.go >> type MathService struct{} >> type Args struct { >> A, B int >> } >> type Reply struct { >> Result int >> } >> func (m *MathService) Multiply(args *Args, reply *Reply) error { >> reply.Result = args.A * args.B >> return nil >> } >> func main() { >> // Create an instance of the MathService >> mathService := new(MathService) >> // Register MathService for RPC >> rpc.Register(mathService) >> // Create a TCP listener >> listener, err := net.Listen("tcp", "0.0.0.0:1234") >> if err != nil { >> fmt.Println("Error starting server:", err) >> return >> } >> defer listener.Close() >> fmt.Println("Server listening on :1234") >> for { >> // Accept incoming connections >> conn, err := listener.Accept() >> if err != nil { >> fmt.Println("Error accepting connection:", err) >> continue >> } >> fmt.Printf("remote ip addr: %s\n", conn.RemoteAddr().String()) >> // Serve the connection in a new goroutine >> go rpc.ServeConn(conn) >> } >> } >> >> // example_client.go >> type Args struct { >> A, B int >> } >> type Reply struct { >> Result int >> } >> func main() { >> // Connect to the server >> client, err := rpc.Dial("tcp", "127.0.0.1:1234") >> if err != nil { >> fmt.Println("Error connecting to server:", err) >> return >> } >> defer client.Close() >> // Prepare the arguments for the RPC call >> args := &Args{A: 5, B: 3} >> // Call the Multiply method remotely >> var reply Reply >> err = client.Call("MathService.Multiply", args, &reply) >> if err != nil { >> fmt.Println("Error calling Multiply:", err) >> return >> } >> // Print the result >> fmt.Printf("Result: %d\n", reply.Result) >> } >> ``` >> >> Therefore, I hope to manually call the`ReportRealIP` function to report >> the **real IP**, but I cannot get the **current** connected `net.Conn` in >> the **rpc function**. >> >> But I searched some information and found no way to achieve my needs. Is >> there a way to get the two variables I want? Looking forward to your reply >> >> -- > 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/1734f6df-b8c9-4466-8e53-2243a88bb991n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/1734f6df-b8c9-4466-8e53-2243a88bb991n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CA%2Bh8R2qgMmychLJWJzrcasEMruam4i6uEP5kJMvr8_E9E9mg8A%40mail.gmail.com.