来一个udp版本的吧:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "net/http"
  7. "net/rpc"
  8. "os"
  9. )
  10. type UDPPush struct {
  11. }
  12. type PushRequest struct {
  13. A string
  14. }
  15. type PushResponse struct {
  16. Data string
  17. }
  18. var socket *net.UDPConn
  19. var remoteAddr *net.UDPAddr
  20. var err error
  21. func main() {
  22. // 创建UDP监听
  23. socket, err = net.ListenUDP("udp4", &net.UDPAddr{
  24. IP: net.IPv4(0, 0, 0, 0),
  25. Port: 8080,
  26. })
  27. if err != nil {
  28. fmt.Println("监听失败!", err)
  29. return
  30. }
  31. fmt.Println("start UDP server!")
  32. defer socket.Close()
  33. go doUDP() //通过协程来使主进程进行rpc的创建
  34. // 以下是创建rpc服务器
  35. rpc.Register(new(UDPPush)) // 注册rpc服务
  36. rpc.HandleHTTP() // 采用http协议作为rpc载体
  37. lis, err := net.Listen("tcp", "127.0.0.1:8095")
  38. if err != nil {
  39. log.Fatalln("fatal error: ", err)
  40. }
  41. fmt.Fprintf(os.Stdout, "%s", "start rpc connection!\n")
  42. http.Serve(lis, nil)
  43. }
  44. func (*UDPPush) PushData(req PushRequest, res *PushResponse) error {
  45. doPush(req)
  46. res.Data = "success!"
  47. return nil
  48. }
  49. func doPush(req PushRequest) {
  50. // 发送数据
  51. sendData := []byte(req.A)
  52. fmt.Println("remoteAddr:", remoteAddr, "sendData:", sendData)
  53. _, err = socket.WriteToUDP(sendData, remoteAddr)
  54. if err != nil {
  55. return
  56. fmt.Println("发送数据失败!", err)
  57. }
  58. }
  59. func doUDP() {
  60. for {
  61. // 读取数据
  62. data := make([]byte, 4096)
  63. var read int
  64. read, remoteAddr, err = socket.ReadFromUDP(data)
  65. if err != nil {
  66. fmt.Println("读取数据失败!", err)
  67. continue
  68. }
  69. fmt.Println(read, remoteAddr)
  70. fmt.Printf("%s\n\n", data)
  71. sendData := []byte("this is from server")
  72. _, err = socket.WriteToUDP(sendData, remoteAddr)
  73. if err != nil {
  74. return
  75. fmt.Println("发送数据失败!", err)
  76. }
  77. }
  78. }

udp客户端

  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. func main() {
  7. // 创建连接
  8. socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
  9. IP: net.IPv4(192, 168, 2, 104),
  10. Port: 8080,
  11. })
  12. if err != nil {
  13. fmt.Println("连接失败!", err)
  14. return
  15. }
  16. defer socket.Close()
  17. // 发送数据
  18. sendData := []byte("hello server!")
  19. _, err = socket.Write(sendData)
  20. if err != nil {
  21. fmt.Println("发送数据失败!", err)
  22. return
  23. }
  24. for {
  25. // 接收数据
  26. data := make([]byte, 4096)
  27. read, remoteAddr, err := socket.ReadFromUDP(data)
  28. if err != nil {
  29. fmt.Println("读取数据失败!", err)
  30. return
  31. }
  32. fmt.Println(read, remoteAddr)
  33. fmt.Printf("%s\n", data)
  34. }
  35. }

rpc客户端:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/rpc"
  6. )
  7. // 算数运算请求结构体
  8. type PushRequestClient struct {
  9. A string
  10. }
  11. type PushResponseClient struct {
  12. Data string
  13. }
  14. func main() {
  15. conn, err := rpc.DialHTTP("tcp", "127.0.0.1:8095")
  16. if err != nil {
  17. log.Fatalln("dialing error: ", err)
  18. }
  19. req := PushRequestClient{"this data is from rpc client....\n"}
  20. var res PushResponseClient
  21. err = conn.Call("UDPPush.PushData", req, &res) // 推送消息,TCPPush和rpcServer里面的是一样的,并且PushData在rpcServer里面是可导出的
  22. if err != nil {
  23. log.Fatalln("push error: ", err)
  24. }
  25. fmt.Println(res.Data)
  26. }