我应该在网络游戏中使用 TCP 吗?

发布于 2024-12-15 14:22:41 字数 442 浏览 3 评论 0原文

我有一个回合制游戏,其中两个玩家可以互相对抗。

它是用 C# 编写的并使用 XNA 4.0。

目前多人游戏是通过 TCP/IP 实现的。它工作得非常好,但前提是玩家位于同一网络内并且其中一个人知道另一个人的 IP。

那么问题来了:这个游戏的联机对战应该如何实现呢? TCP 是否是一种合理的方法,用于连接来自世界两端的两个随机玩家,而无需处理 IP 地址和端口(或任何其他此类技术细节)?

为了使这个问题更具挑战性,我没有用于托管游戏匹配服务的服务器。 (嗯,我可以访问虚拟 Web 服务器,我可以使用它来共享 IP。)

列出问题:

  • .NET 是否提供比 TCP 更好的通信方法选择?
  • 在我的情况下,处理 NAT 的最佳方法是什么?
  • 有没有一种便宜的方法来获得我自己的服务器并在那里运行 TCP 游戏匹配服务?

I have a turn-based game in which two players may play against each others.

It's written in C# and uses XNA 4.0.

Currently multiplayer is implemented with TCP/IP. It works pretty nicely, but only if the players are within the same network and one of them knows the IP of the other.

So the question is: How should I implemented the online play for this game? Is TCP a reasonable method for connecting two random players from the opposite sides of the world without them having to deal with IP addresses and ports(or any other such technical details)?

To make this problem more challenging, I have no server for hosting the game matching service. (Well, I have an access to a virtual web server which I could use for sharing the IPs.)

To list questions:

  • Does .NET offer better choice of communication method than TCP?
  • What would be the best way to deal with NATs in my case?
  • Is there a cheap way of getting my own server and run the TCP game matching service there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆梦 2024-12-22 14:22:41

TCP 与 UDP。

TCP 比 UDP 慢一点,但更安全。

由于你的游戏是回合制的,它可能会在客户端和服务器之间发送最少量的数据,并且它并不真正依赖于延迟,我想说你也可以选择 TCP。

为了使这个问题更具挑战性,我没有用于托管游戏匹配服务的服务器。 (嗯,我可以访问虚拟 Web 服务器,我可以用它来共享 IP。)

如果您要为玩家提供服务器浏览器或类似的服务器,您将需要一个集中式服务器,一个带有为此构建的脚本/应用程序就可以了。

  • 有没有一种便宜的方法可以让我拥有自己的服务器并在那里运行 TCP 游戏匹配服务?

Web 服务器或类似的主机就可以了,而且通常很便宜,您想要的是:

  • 服务器将自身添加到服务器列表的功能。
  • 客户端检索列表上的服务器的函数。

使用 C# 进行 Web 请求完全没有问题,请求可能类似于:

http://www.example.com/addToServerList.php?name=MyEpicServer&ip=213.0.0.ABC(将此服务器添加到列表中)

http://www.example.com/getOnlineServers.php (返回所有服务器的列表)

TCP vs UDP.

TCP is a bit slower than UDP but more failsafe.

Since your game is turn based it will probably send minimal amounts of data between the client and server and it is not really latency dependant, I would say you might aswell go for TCP.

To make this problem more challenging, I have no server for hosting the game matching service. (Well, I have an access to a virtual web server which I could use for sharing the IPs.)

If you are going to provide your players with a server browser or similar you will need to have a centralized server, a web server with a script/application built for this would do just fine.

  • Is there a cheap way of getting my own server and run the TCP game matching service there?

A web server or similar host would do just fine and is usually cheap, what you want is:

  • Function for a server to add itself to the server list.
  • Function for a client to retrieve the servers on the list.

Doing web requests with C# is no problem at all, the requests could look something like:

http://www.example.com/addToServerList.php?name=MyEpicServer&ip=213.0.0.ABC (adds this server to the list)

http://www.example.com/getOnlineServers.php (returns list of all the servers)

颜漓半夏 2024-12-22 14:22:41

您需要指定预期和容忍的负载和延迟类型。

一般答案是:

  • 对于实时游戏 - UDP。

  • 对于类似拼字游戏 - TCP。

正如你所说,使用你的服务器共享IP。

You need to specify what kind of load and latency that is expected and tolerated.

General answer is:

  • For real time games - UDP.

  • For scrabble-like-games - TCP.

Use your server to share IP's as you said.

欲拥i 2024-12-22 14:22:41

我的世界使用 TCP。这对于必须传输和接收并且可以稍微排队的流量很有用。

Minecraft uses TCP. It's good for traffic that must be transmitted and received AND can be queued a little.

眼眸里的那抹悲凉 2024-12-22 14:22:41

UDP 是一种单向错误检查。仅接收方检查错误。这是旧的慢速以太网技术所需要的,因为检查数据包的往返速度太慢。

TCP是一个非常可靠的握手协议。这样发送方就知道数据是否传输成功。但由于往返,它会给传输带来更多的开销和延迟。

TCP 也会排列数据包,而 UDP 也不这样做。

有些游戏不介意丢失数据包(例如,对象移动时“蒸汽”数据,并且无论如何它都会在下一轮数据包中更新)。在那里你可以使用UDP。但如果关键是要获取所有数据,请选择 TCP,否则您将花费​​大量时间编写代码以确保所有数据均成功传输。

网络速度足够快,并且互联网是 TCP/IP,我推荐 TCP,除非您确实需要非常低的延迟流量。

这个网站给出了很好的总结:
http://www.diffen.com/difference/TCP_vs_UDP

NAT:不应该是一个问题只要您的生存时间 (TTL) 足够大。每次获得 NAT 时,TTL 都会减一。当它为0时,它会被丢弃

UDP is a one way error checking. Only the receiving side check for error. This was needed with the older slow ethernet technology where a round trip to check packets is too slow.

TCP is a very reliable protocol with a handshake. So the sending side knows if the data is transmitted sucessfully. But due to the round trip, it puts a lot more overhead and lag on the transmission.

TCP also do arrange packets, which UDP also don't do.

Some games it does not mind losing packets (for example "steaming" data where objects moves around, and it will get updated the next round of packet anyway). There you can use UDP. But if it is critical to get all the data, rather go with TCP, otherwise you will spend a lot of time writing code to make sure that all the data is transmitted successfully.

The networks is quickly enough and the internet being TCP/IP I recommend TCP, except if you really need very low lattency traffic.

This website gives a good summary:
http://www.diffen.com/difference/TCP_vs_UDP

NAT: Should not be a problem just as long as your Time To Live (TTL) is big enough. Every time it get NAT, it's TTL get subtracted by one. When it is 0, it gets dropped

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文