CARVIEW |
Select Language
HTTP/2 200
date: Wed, 30 Jul 2025 18:40:55 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
x-robots-tag: none
etag: W/"91ec96346ed7ad9523ebeea61c578a05"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com wss://alive-staging.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=K2cwhvkqWABw44D6ZXbhXSWEa1SruOB%2BANedgPqmXahRAOk3rSH%2Bixg5Py3Hb4FYtRrX0Ug9RNKRHbnzXFxwnmZiM%2BYSYQ9IiMsnt4Ol4Hp%2BMMLUtR%2ByLgiTcTa%2Fdbgs5R5hYt2gr0vQXrh3U%2Bls6IX0viqVAXWeOEiDGqPk%2B7VsRP4CiHMPMpGhIteAs%2FSlzbPBdWvmeQL9L5A0N3EflMLXQXAiQNGnf8zaytrTLWsPTjrLX%2BJY2M9Lu83suwz30SRA3HGU2Ms7vCQR9ar90Q%3D%3D--P70RfOIBzBXndPOI--THo0uck2jCSCqdWzdIvgOQ%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.530676589.1753900854; Path=/; Domain=github.com; Expires=Thu, 30 Jul 2026 18:40:54 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Thu, 30 Jul 2026 18:40:54 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: D1F6:3A3C19:20EB76:275327:688A6736
Cooperate With Tcp Server · skywind3000/kcp Wiki · GitHub
Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Cooperate With Tcp Server
Jim edited this page Jun 15, 2021
·
12 revisions
在使用 KCP时,你可以用在你 TCP的基础上,在登陆时服务端返回 UDP端口和密钥,客户端通过 TCP收到以后,向服务端的 UDP端口每隔一秒重复发送包含握手信息,直到服务端返回成功或者失败。服务端通过 UDP传上来的密钥得知该客户端 sockaddr对应的 TCP连接,这样就建立 TCP连接到 UDP连接的映射关系。为了保持连接和 NAT出口映射,客户端一般需要每 60秒就发送一个 UDP心跳,服务端收到后回复客户端,再在这个 UDP连接的基础上增加调用 KCP的逻辑,实现快速可靠传输,这样一套 TCP/UDP两用的传输系统就建立了。
可以参考下述例子:
- 客户端链接tcp,服务端为该tcp链接分配一个整数id作为标识。
- 登录后服务端给客户端发送udp握手信息,包括:自己的udp端口,用户的tcp标识id,32位随机数key
- 客户端给服务端udp地址发送握手信息,把刚才服务端发过来的(id, key) 发送给服务端。
- 服务端确认udp握手,并且记录该用户udp远端地址
- 以后客户端和服务端udp通信,每个包都包含(id, key),
- 服务端用客户端发上来的(id,key),确认用户身份,并对比远端地址confirm是一个合法用户。
注意:为了保持 NAT映射关系,UDP需要每隔 60秒就向服务器 ping一次。同时为了防止出口地址改变(NAT映射改变,或者移动设备切换基站),可以使用重连,或者UDP重绑定(但是在 3G,2G,EDGE下面,出口改变,TCP也就断了,所以简单重连也没问题)。
客户端和原有的TCP连接对象稍微封装一下,就得到一个类似:
connection.send(channel, data, size)
的接口,channel=0时,使用原有tcp发送,channel=1时使用kcp发送,channel=2时使用裸的udp发送。三个channel同时工作,适配不同情况。服务端接口也类似。
中国的网络情况比较特殊,会存在有些网络 UDP连接不上的情况,因此都是先连接 TCP,然后试图 UDP,UDP不通的情况下,退回 TCP也能正常游戏,一旦 TCP断开,则认为 UDP也断开了。
You can’t perform that action at this time.