使用UDP打开TCP端口?

 ooleysciacca 发布于 2023-01-18 11:35

我读了一些关于使用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.

1 个回答
  • 你不能这样做.

    TCP端口和UDP端口无法相互连接.您发送的数据包UdpClient.Send()Protocol字段设置0x11为UDP的值,但TCP堆栈仅识别Protocol设置0x06为TCP的值,即TCP的值.它是IP模块最基本操作的一部分.如果需要详细信息,请参阅RFC 791.

    2023-01-18 11:38 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有