rpc_client.go 1006 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // rpc_client.go
  2. // if the server is not started:
  3. // can't get the server to start, so client stops immediately with error:
  4. // 2011/08/01 16:08:05 Error dialing:dial tcp :1234:
  5. // The requested address is not valid in its context.
  6. // with serverAddress = localhost:
  7. // 2011/08/01 16:09:23 Error dialing:dial tcp 127.0.0.1:1234:
  8. // No connection could be made because the target machine actively refused it.
  9. package main
  10. import (
  11. "fmt"
  12. "log"
  13. "net/rpc"
  14. "./rpc_objects"
  15. )
  16. const serverAddress = "localhost"
  17. func main() {
  18. client, err := rpc.DialHTTP("tcp", serverAddress + ":1234")
  19. if err != nil {
  20. log.Fatal("Error dialing:", err)
  21. }
  22. // Synchronous call
  23. args := &rpc_objects.Args{7, 8}
  24. var reply int
  25. err = client.Call("Args.Multiply", args, &reply)
  26. if err != nil {
  27. log.Fatal("Args error:", err)
  28. }
  29. fmt.Printf("Args: %d * %d = %d", args.N, args.M, reply)
  30. }
  31. /* Output:
  32. Starting Process E:/Go/GoBoek/code_examples/chapter_14/rpc_client.exe ...
  33. Args: 7 * 8 = 56
  34. End Process exit status 0
  35. */