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

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

问题描述

下面是我的方案。

我有我的设备(android手机)。 我知道其他设备的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,尽管互联网上的信息相对匮乏对这些类型,你仍然可以维基百科,并读了几所大学的研究论文。该类型是完全NAT,NAT受限,受限端口NAT和对称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。这是一个问题。什么是一个解决方案?如果我们指示路由器路由所有传入流量定向到一个特定的端口,在一定的计算机在我们的局域网?例如,如果三个局域网中的计算机共享1.1.1.1广域网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是这样一种有趣的技术(并不总是present中的路由器 - 或者更重要的是,几乎没有默认启用),可让您的端口转发使用code

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本身就是第一个面向连接的协议。打洞更容易,因为你可以在没有连接发送的数据包(其实是真的有这样的事,作为一个连接?)

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?)

您可以阅读维基百科等网站打洞的过程。这个概念是这样工作的,虽然。任何一台计算机发送出站数据包将期待的答复,这意味着路由器将允许入站数据包的答复。在第一,二台计算机将不期望从彼此的答复,因此,每个计算机发送第一分组将被路由器拒绝。如果我们继续发送数据包,但是,路由器将允许报文的其余部分才能通过,因为回复从各计算机预期。只要这种打孔连接保持活动状态偶尔保活报文,就会生存下去。不幸的是,打洞的这种方法是不成功的对称NAT的(下面的链接解释)。这就是为什么最终的解决办法是简单地代理通过自己的服务器的连接和通信。这需要计算机的农场,所以这是相当昂贵的。但这种终极解决方案被称为转向。 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://想-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://$c$c.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天全站免登陆