项目作者: joseluisq

项目描述 :
A simple Unix IPC Socket client for Go.
高级语言: Go
项目地址: git://github.com/joseluisq/goipcc.git
创建时间: 2020-11-17T13:03:48Z
项目社区:https://github.com/joseluisq/goipcc

开源协议:Other

下载


goipcc Build Status codecov Go Report Card PkgGoDev

A simple Unix IPC Socket client for Go.

NOTE: For more flexibility try joseluisq/gonetc instead.

Usage

  1. package main
  2. import (
  3. "log"
  4. "strings"
  5. "github.com/joseluisq/goipcc"
  6. )
  7. func main() {
  8. // Code for example purposes only
  9. // 1. Create a simple listening Unix socket with echo functionality
  10. // using the `socat` tool -> http://www.dest-unreach.org/socat/
  11. // Then execute the following commands on your terminal:
  12. // rm -f /tmp/mysocket && socat UNIX-LISTEN:/tmp/mysocket,fork exec:'/bin/cat'
  13. // 2. Now just run this client code example in order to exchange data with current socket.
  14. // go run examples/main.go
  15. // 2.1 Connect to the listening socket
  16. sock := goipcc.New("/tmp/mysocket")
  17. err := sock.Connect()
  18. if err != nil {
  19. log.Fatalln("unable to communicate with socket:", err)
  20. }
  21. // 2.2 Send some sequential data to current socket (example only)
  22. pangram := strings.Split("The quick brown fox jumps over the lazy dog", " ")
  23. for _, word := range pangram {
  24. log.Println("client data sent:", word)
  25. _, err := sock.Write([]byte(word), func(resp []byte, err error, done func()) {
  26. log.Println("client data received:", string(resp))
  27. // Finish the current write handling response if we are done
  28. done()
  29. })
  30. if err != nil {
  31. log.Fatalln("unable to write to socket:", err)
  32. }
  33. }
  34. sock.Close()
  35. // 3. Finally after running the client you'll see a similar output like:
  36. //
  37. // 2020/11/24 00:39:27 client data sent: The
  38. // 2020/11/24 00:39:27 client data received: The
  39. // 2020/11/24 00:39:28 client data sent: quick
  40. // 2020/11/24 00:39:28 client data received: quick
  41. // 2020/11/24 00:39:29 client data sent: brown
  42. // 2020/11/24 00:39:29 client data received: brown
  43. // 2020/11/24 00:39:30 client data sent: fox
  44. // 2020/11/24 00:39:30 client data received: fox
  45. // 2020/11/24 00:39:31 client data sent: jumps
  46. // 2020/11/24 00:39:31 client data received: jumps
  47. // 2020/11/24 00:39:32 client data sent: over
  48. // 2020/11/24 00:39:32 client data received: over
  49. // 2020/11/24 00:39:33 client data sent: the
  50. // 2020/11/24 00:39:33 client data received: the
  51. // 2020/11/24 00:39:34 client data sent: lazy
  52. // 2020/11/24 00:39:34 client data received: lazy
  53. // 2020/11/24 00:39:35 client data sent: dog
  54. // 2020/11/24 00:39:35 client data received: dog
  55. }

Contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in current work by you, as defined in the Apache-2.0 license, shall be dual licensed as described below, without any additional terms or conditions.

Feel free to send some Pull request or issue.

License

This work is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

© 2020-present Jose Quintana