【问题标题】:Resolve domain name (URI) when listening to tcp socket in C#在 C# 中侦听 tcp 套接字时解析域名(URI)
【发布时间】:2019-11-09 22:34:04
【问题描述】:

我想在我的服务器上实现一个单一的套接字侦听器控制台应用程序,该应用程序从多个套接字客户端获取数据。

另一方面,每个客户端都有自己的域名(不使用 IP 地址)。我正在使用静态 IP 地址在我的服务器上运行侦听器,并且我已经为 IP 地址定义了通配符 DNS 记录(例如:*.xxx.yyy.com)。

当我 ping 域名时,静态 IP 地址被解析,客户端可以使用域名发送数据。

我正在特定端口上测试一个简单的 TCP 侦听器控制台应用程序,以获取我的控制台客户端发送的数据。

这里是示例监听器代码:

using System;
using System.Net;
using System.Text;
using System.Net.Sockets;

namespace SocketExample
{
    public class SocketListener
    {
        public static int Main(String[] args)
        {
            StartServer();
            return 0;
        }


        public static void StartServer()
        {
            var ipAddress = IPAddress.Parse("xxx.xxx.xxx.xxx");
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

            try
            {   
                Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                listener.Bind(localEndPoint);
                listener.Listen(10);
                Socket handler = listener.Accept();

                string data = null;
                byte[] bytes = null;

                while (true)
                {
                    bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);
                    data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                    if (data.IndexOf("<EOF>") > -1)
                    {
                        break;
                    }
                }

                Console.WriteLine("Text received : {0}", data);

                byte[] msg = Encoding.ASCII.GetBytes(data);
                handler.Send(msg);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("\n Press any key to continue...");
            Console.ReadKey();
        }
    }
}

而客户端代码是:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Client
{
    public class SocketClient
    {
        public static void Main(String[] args)
        {
            byte[] bytes = new byte[1024];


            var ipAddress = IPAddress.Parse("xxx.xxx.xxx.xxx");
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

            Socket sender = new Socket(ipAddress.AddressFamily,
                SocketType.Stream, ProtocolType.Tcp);


            sender.Connect(remoteEP);

            Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString());

            byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");

            int bytesSent = sender.Send(msg);

            int bytesRec = sender.Receive(bytes);
            Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));

            sender.Shutdown(SocketShutdown.Both);
            sender.Close();
        }
    }
}

使用 DNSEntry 一切正常,但问题是我想根据他们调用的域名来识别我的客户。

例如,我有 2 个客户:

  • client1.xxx.yyy.com
  • client2.xxx.yyy.com

当监听器接收到数据时,我想根据调用域名来识别客户端。

我想知道如何在侦听器端解析调用域名。 是否可以获取域名或者客户端应该在发送的数据中发送特定的域名?

【问题讨论】:

  • TCP总是基于 IP 地址。您可以对客户端 IP 地址进行反向 DNS 查找。

标签: c# sockets tcp tcpclient tcplistener


【解决方案1】:

如果您想要客户端用来连接的文本域名,那么您的客户端必须将其作为其数据协议的一部分发送到服务器,类似于 HTTP 中的 Host: 标头

当建立套接字连接时,它只使用 IP 地址。那时文本域名已不存在。

【讨论】:

  • 有没有办法在网络层的某个地方注入文本域名?我在边缘有 pfSense 作为防火墙/路由器。
  • 没有。 TCP 和 IP 都不关心文本域名。这些是您的应用程序实现的任何数据协议层之上的唯一层。
猜你喜欢
  • 2011-04-20
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 2021-11-22
  • 1970-01-01
相关资源
最近更新 更多