I want to implement a single socket listener console application on my server which gets the data from multiple socket clients.
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).
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.
I am testing a simple TCP listener console application on a specific port to get the data which is sent by my console client.
Here is the sample listener code:
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();
}
}
}
And the client code is:
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();
}
}
}
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.
For instance, I have 2 clients:
When the listener receives a data, I want to identify the client based on the calling domain name.
I want to know how I can resolve the calling domain names on the listener side. Is it possible to get the domain name or the clients should send the specific domain names inside the sent data?
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
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.