5
Answers

How to get the connected client's machine name in C#?

haan jae

haan jae

12y
8.2k
1
hello, i had been searching and trying many codes, but all the code return the local machine name, not the connected client machine name. how to get the client machine name??
Please help, thanks a lot.

here is the code:

void UpdateClientList()
{
            listView1.Items.Clear();
            
   for(int i = 0; i < m_workerSocketList.Count; i++)
{
        string clientKey = Convert.ToString(i+1);
Socket workerSocket = (Socket)m_workerSocketList[i];

                 ListViewItem result = new ListViewItem();
                 string clientinfo = Dns.GetHostName();
                 IPHostEntry he = Dns.GetHostEntry(clientinfo);

                 string sIP = (workerSocket.RemoteEndPoint.ToString().Split(':'))[0];
                 IPAddress tempIP = IPAddress.Parse(sIP);

if(workerSocket != null)
{
if(workerSocket.Connected)
{
                        result.SubItems.Add(he.HostName);
                        result.SubItems.Add(tempIP.ToString());

                        listView1.Items.Add(clientKey);
                        listView1.Items.Add(result);
}
 }
}
}
Answers (5)