在 C# 中侦听 tcp 套接字时解析域名 (URI) [英] Resolve domain name (URI) when listening to tcp socket in C#

查看:43
本文介绍了在 C# 中侦听 tcp 套接字时解析域名 (URI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to implement a single socket listener console application on my server which gets the data from multiple socket clients.

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

On the other hand, each client has its own domain name (without using IP address). I am running the listener on my server with a static IP address and I have defined a wildcard DNS record to the IP address (like: *.xxx.yyy.com).

当我 ping 域名时,静态 IP 地址被解析并且客户端能够使用域名发送数据.

When I ping the domain names, the static IP address is resolved and the clients are able to send the data using the domain names.

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

I am testing a simple TCP listener console application on a specific port to get the data which is sent by my console client.

这是示例侦听器代码:

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 一切正常,但问题是我想根据他们调用的域名识别我的客户.

Everything works fine using DNSEntry as well, but the issue is that I want to identify my clients based on the domain name they call.

例如,我有 2 个客户:

For instance, I have 2 clients:

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

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

When the listener receives a data, I want to identify the client based on the calling domain name.

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

推荐答案

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

If you want the textual domain name that the client used to connect, then your client will have to send that to the server as part of it's data protocol, similar to how there is a Host: header in HTTP

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

When the socket connection is made it's done so using only the IP addresses. The textual domain name doesn't exist any more at that point.

这篇关于在 C# 中侦听 tcp 套接字时解析域名 (URI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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