使用 Tcp/Udp 发送数据 [英] Send data using Tcp/Udp

查看:27
本文介绍了使用 Tcp/Udp 发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的场景.

我有我的设备(安卓手机).而且我知道另一台设备的 IP 地址(这是互联网上的某个远程设备)

I have my device (android phone). And I know the IP address of another device (which is some remote device in the Internet)

那么如何将数据发送到其他设备?我使用 Tcp 或 Udp 等协议吗?

So how do I send data to the other device? Do I use protocols like Tcp or Udp?

而且我知道有公共 IP 地址和私有 IP 地址(例如当设备通过 WiFi 连接时).在这种情况下,甚至可以使用 Tcp 或 Udp 吗???

EDIT : And I know there are public IP addresses and private IP addresses (like when a device connects via WiFi). In such a case is it even possible to use Tcp or Udp???

推荐答案

你要做的是一个有共同问题的共同任务(但也有一个共同的解决方案).您想将数据从一台设备(计算机/电话/等)发送到另一台设备.以下是它在幕后的运作方式:

What you're looking to do is a common task with a common problem (but also with a common solution). You want to send data from one device (computer/phone/etc..) to another. Here's how it works behind the scenes:

世界上有很多计算机.每台计算机都被赋予了一个身份——一个名字,如果你愿意的话,它被称为计算机的 IP 地址.IP 地址有许多版本/标准,当前标准将 IP 地址命名为:111.222.333.444".12 位数字的排列只有这么多 - 并且整个 IP 地址范围都被保留.换句话说,没有足够的 IP 地址供计算机使用.

There are a lot of computers in the world. Each computer is given an identity - a name, if you will, which is called the computer's IP address. There are many versions/standards of the IP address, and the current standard names an IP address as such: "111.222.333.444". There are only so many permutations with 12 digits of numbers - and entire ranges of IP addresses are reserved. In other words, there aren't enough IP addresses to go around for computers.

现在,除了其他原因外,还发明了路由器来解决这个问题.路由器为其局域网 (LAN) 内的所有计算机提供一个公共 (WAN) IP 地址.每个家庭平均使用一台路由器,这样可以节省许多 IP 地址.不幸的是,它也破坏了计算机之间的直接通信,因为一台计算机如何与使用相同名称的三台计算机通信?当计算机 A 和计算机 B 和计算机 C 共享相同的 WAN IP 地址时,如何识别它们?

Now, routers were invented, among other reasons, to combat this problem. Routers give all computers within its local area network (LAN) one public (WAN) IP address. With an average of one router per household, this saves many IP addresses. Unfortunately, it also breaks direct communication between computers, because how does one communicate with three computers all using the same name? How does one identify computer A from computer B from computer C when they all share the same WAN IP address?

首先,让我们澄清一下 - 就像 IP 地址有几个版本和标准一样,路由器也有几种类型.顺便说一句,导致计算机之间直接通信问题的原因并不是路由器,而是路由器的底层网络地址转换 (NAT) 技术.无论如何,NAT 有几种类型,虽然互联网上关于这些类型的信息相对稀少,但您仍然可以 Wikipedia 并阅读一些大学研究论文.类型有 Full Cone NAT、Restricted NAT、Port Restricted NAT 和 Symmetric NAT.

First, let's clarify that - just like there are a few versions and standards of IP addresses, so there are a few types of routers. By the way, it's not the router that is to blame for the problems of direct communication between computers - it's actually the router's underlying technology of network address translation (NAT). Anyways, there are a few types of NAT, and although information on the internet is relatively scarce on these types, you can still Wikipedia it and read a few university research papers. The types are Full Cone NAT, Restricted NAT, Port Restricted NAT, and Symmetric NAT.

因此,局域网中的计算机共享相同的 WAN IP.这是个问题.一种解决方案是什么?如果我们指示路由器将定向到某个端口的所有传入流量路由到我们局域网中的某台计算机会怎样?例如,如果三台 LAN 计算机共享 1.1.1.1 的 WAN IP,我们可以设置一个称为端口转发的过程来指示路由器将所有到达端口 6000 的数据包路由到 192.168.1.101:3500.

So, computers in a LAN share the same WAN IP. This is a problem. What's one solution? What if we instructed the router to route all incoming traffic directed to a certain port, to a certain computer in our local area network? For example, if three LAN computers share the WAN IP of 1.1.1.1, we can setup a procedure called port forwarding to instruct the router to route all packets arriving on port 6000 to 192.168.1.101:3500.

uPnP 只是一种奇特的技术(并不总是出现在路由器中 - 或者更重要的是,默认情况下几乎从未启用)它允许您使用代码进行端口转发.

uPnP is just a fancy technology (not always present in routers - or more importantly, hardly ever enabled by default) that allows you to port forward using code.

如果我们不能使用 uPnP 进行端口转发,因为并非所有设备都启用了它,那么必须有另一种解决方案——那就是:打孔.

If we can't use uPnP to port forward because not all devices have it enabled, there must be another solution - and there is: hole punching.

打孔有两种形式:UDP 和 TCP,尽管 TCP 打孔有点复杂,因为 TCP 本质上首先是一个面向连接的协议.UDP打洞更容易,因为你可以在没有连接的情况下发送数据包(事实上,真的有连接这种东西吗?)

Hole punching comes in two flavors: UDP and TCP, although TCP hole punching is a bit more complicated because TCP is inherently first a connection-oriented protocol. UDP hole punching is easier because you can send packets without a connection (in fact, is there really such a thing as a connection?)

您可以在 Wikipedia 和其他网站上阅读有关 UDP 打孔的过程.不过,这个概念是这样工作的.任何发送出站数据包的计算机都会收到回复,这意味着路由器将允许入站数据包的回复.起初,两台计算机不会期待对方的回复,因此每台计算机发送的第一个数据包将被路由器拒绝.但是,如果我们继续发送数据包,路由器将允许其余数据包通过,因为每台计算机都需要回复.只要这个打孔连接通过偶尔的保持活动数据包保持活动状态,它就会保持活动状态.不幸的是,这种 UDP 打洞方法对于对称 NAT 来说是不成功的(在下面的链接中进行了解释).这就是为什么最终的解决方案是简单地通过您自己的服务器代理连接和流量.这需要大量计算机,因此成本很高.但是这个最终的解决方案被称为 TURN.STUN 是一个 Web 服务,您可以查询以确定您的 NAT 类型和公共 IP(而不是解析 cmyip.com).只是您在个人研究中会遇到的一些术语.

You can read the procedure on UDP hole punching on Wikipedia and other sites. The concept works like this, though. Any computer sending an outbound packet will expect a reply, meaning the router will allow the inbound packet's reply. At first, two computers will not expect a reply from each other, so the first packet each computer sends will be rejected by the router. If we continue sending packets, however, the router will allow the rest of the packets to come through, because a reply is expected from each computer. As long as this hole-punched connection is maintained active by occasional keep-alive packets, it will stay alive. Unfortunately, this method of UDP hole punching is unsuccessful for Symmetric NATs (explained in the links below). That's why the end-all solution is to simply proxy the connection and traffic through your own server. This requires a farm of computers, so it's quite costly. But this end-all solution is called TURN. STUN is a web service you can query to determine your NAT type and public IP (as opposed to parsing cmyip.com). Just some terms you'll encounter along your personal research.

玩弄这些概念会有所帮助.以下是一些帮助您入门的资源:

Playing around with these concepts help. Here are some resources to get your started:

NAT 类型:http://think-like-a-computer.com/2011/09/16/types-of-nat/

一种对称 NAT 穿越方法(基本上是通过向更多端口发送更多数据包):http://www.goto.info.waseda.ac.jp/~wei/file/wei-apan-v10.pdf

A Method for Symmetric NAT Traversal (basically by spamming a lot more packets to a lot more ports): http://www.goto.info.waseda.ac.jp/~wei/file/wei-apan-v10.pdf

Lidgren 网络库(内置 NAT 遍历,这是一个 C# 库):http://code.google.com/p/lidgren-network-gen3/w/list

Lidgren Networking Library (which has NAT traversal built-in, this is a C# library): http://code.google.com/p/lidgren-network-gen3/w/list

这篇关于使用 Tcp/Udp 发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆