{"id":17156684,"url":"https://github.com/xiaonanln/pktconn","last_synced_at":"2026-06-27T04:30:20.351Z","repository":{"id":47450077,"uuid":"150373465","full_name":"xiaonanln/pktconn","owner":"xiaonanln","description":"Packet connection over net.Conn (Golang)","archived":false,"fork":false,"pushed_at":"2021-08-31T10:29:21.000Z","size":201,"stargazers_count":5,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-06-19T19:47:20.729Z","etag":null,"topics":["packet","tcp","tcp-packets"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xiaonanln.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-26T05:35:08.000Z","updated_at":"2024-04-17T01:02:43.000Z","dependencies_parsed_at":"2022-09-10T11:01:46.127Z","dependency_job_id":null,"html_url":"https://github.com/xiaonanln/pktconn","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiaonanln%2Fpktconn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiaonanln%2Fpktconn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiaonanln%2Fpktconn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiaonanln%2Fpktconn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xiaonanln","download_url":"https://codeload.github.com/xiaonanln/pktconn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219846534,"owners_count":16556432,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["packet","tcp","tcp-packets"],"created_at":"2024-10-14T22:07:07.166Z","updated_at":"2026-06-27T04:30:20.293Z","avatar_url":"https://github.com/xiaonanln.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pktconn\nA high performance TCP Packet Connection library written in Go.\n基于Go实现的高性能TCP数据封包通信库。\n\n## Brief Introduction - 简介\n\n1. A packet is a byte array of arbitrary length. \n    - 每个包都是一段任意长度的字节数组。\n2. Provide `Packet` class for flexible data packing and unpacking.\n    - 提供Packet类用于灵活的数据封包和拆包。\n3. Make the APIs as simple as possible.\n    - 让接口尽可能简单！\n\n## Performance Benchmark - 性能测试\n[tests/echo_server](https://github.com/xiaonanln/pktconn/blob/master/examples/server/server.go) can receive and send more than **54000** Packets of variable length (0 ~ 2048B).\n\n[tests/echo_server](https://github.com/xiaonanln/pktconn/blob/master/examples/server/server.go)目前可以做到单核每秒接收并发回大于**5.4万**个0~2048字节的数据包。\n\n* Testing Environment - 测试环境\n    * [Github Action Virtual Machine](https://docs.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources)\n\nThe [profile result](https://raw.githubusercontent.com/xiaonanln/pktconn/master/tests/profile.pdf) shows that system \ncalls contributed 85% of the runtime overhead, which is not optimizable. \nThere is no memory allocation or GC overhead, which is the key to achieve high performance Packet sending and receiving. \n\n从[Profile结果](https://raw.githubusercontent.com/xiaonanln/pktconn/master/tests/profile.pdf)来看，**85%** 开销都在收发包的系统调用上，这部分是无法优化的。**几乎没有任何内存分配和垃圾回收的开销**，这也是实现高性能收发数据包的关键。\n\n## Examples - 示例\n\n### Server Example - 服务端示例\n\n```go \npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"net\"\n\n\t\"github.com/xiaonanln/pktconn\"\n)\n\nfunc main() {\n\tln, err := net.Listen(\"tcp\", \"0.0.0.0:14572\")\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tdefer ln.Close()\n\n\tfor {\n\t\tconn, err := ln.Accept()\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tgo func() {\n\t\t\tpc := pktconn.NewPacketConn(context.TODO(), conn)\n\t\t\tfmt.Printf(\"client connected: %s\\n\", pc.RemoteAddr())\n\n\t\t\tfor pkt := range pc.Recv() {\n\t\t\t\tfmt.Printf(\"recv packet: %d\\n\", pkt.GetPayloadLen())\n\t\t\t\tpc.Send(pkt) // send packet back to the client\n\t\t\t\tpkt.Release()\n\t\t\t}\n\n\t\t\tfmt.Printf(\"client disconnected: %s\", pc.RemoteAddr())\n\t\t}()\n\t}\n}\n```\n\n### Client Example - 客户端示例\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"net\"\n\n\t\"github.com/xiaonanln/pktconn\"\n)\n\nfunc main() {\n\tconn, err := net.Dial(\"tcp\", \"localhost:14572\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tpc := pktconn.NewPacketConn(context.TODO(), conn)\n\tdefer pc.Close()\n\n\tpacket := pktconn.NewPacket()\n\tpayload := make([]byte, 1024)\n\tpacket.WriteBytes(payload)\n\n\tpc.Send(packet)\n\trecvPacket := \u003c-pc.Recv()\n\tfmt.Printf(\"recv packet: %d\\n\", recvPacket.GetPayloadLen())\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiaonanln%2Fpktconn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxiaonanln%2Fpktconn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiaonanln%2Fpktconn/lists"}