我读了一些关于使用UDP打开端口的东西.我找不到我读过的原始页面,但我确实找到了这样的答案/sf/ask/17360801/
我试过运行这段代码.也许我做错了什么?想法(在上面的链接中)是Alice监听端口5412,从5412(tcp端口)向5411发送一个UDP包到bob.Bob(不监听)使用TCP端口5411(udp端口)连接到Alice 5412.我在bob上使用命令行来提供alice IP地址.
我做错了吗?当我使用我的公共IP地址(和我的网络地址但不是127.0.0.1)在本地运行时,我得到了例外A socket operation was attempted to an unreachable network
.当我在Bob上运行它时,我得到一个连接超时异常.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; using System.Threading; namespace TcpTest { class Program { static string localIp = "127.0.0.1"; static string remoteIP = ipaddr; static void Main(string[] args) { //run remotely to connect to you using TCP if (args.Count() > 0) { var t = new TcpClient(new IPEndPoint(IPAddress.Parse(localIp), 5411)); //Force port t.Connect(args[0], 5412); return; } //Run locally //Bind TCP port var l = new TcpListener(5412); l.Start(); //Send UDP using the listening port to remote address/port var u = new UdpClient(5412); u.Connect(remoteIP, 5411); var buf = new byte[10]; u.Send(buf, buf.Length); //R new Thread(SimulateRemote).Start(); //L var c = l.AcceptTcpClient(); var af=c.Client.RemoteEndPoint; } static void SimulateRemote() { Thread.Sleep(500); var t = new TcpClient(new IPEndPoint(IPAddress.Parse(localIp), 5411)); //Force port t.Connect(myipaddr, 5412); } } }
Ross Patters.. 5
你不能这样做.
TCP端口和UDP端口无法相互连接.您发送的数据包UdpClient.Send()
的Protocol
字段设置0x11
为UDP的值,但TCP堆栈仅识别Protocol
设置0x06
为TCP的值,即TCP的值.它是IP模块最基本操作的一部分.如果需要详细信息,请参阅RFC 791.
你不能这样做.
TCP端口和UDP端口无法相互连接.您发送的数据包UdpClient.Send()
的Protocol
字段设置0x11
为UDP的值,但TCP堆栈仅识别Protocol
设置0x06
为TCP的值,即TCP的值.它是IP模块最基本操作的一部分.如果需要详细信息,请参阅RFC 791.