Hi,
I am working on getting MAC address of a newly connected client using C#.
I have written server application and when client connects to my server, I want to get MAC addresses of clients and store it in database.
I could get IP address of client but could not find a way to get MAC address
Can anyone please help me?

Other thing that I am trying to do is shrink an image sent by a server to client. When an image is sent by server (picturebox in server is larger than that at client), client can not display it completely in its picturebox. I can see half image.
Is there any way I can shrink image sent by server?

Thanks

Dani AI

Generated

Quick reality check first: is right. You cannot reliably learn a remote client’s MAC from the server unless the client is on the same L2 network. Across routers/NAT or via HTTP, the MAC never leaves the client’s LAN.

If your server and client are on the same subnet, resolve the MAC from the client’s IP using ARP after you accept the connection. Example (Win32; works only on-LAN and after traffic flows, so a ping/handshake helps):

using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;

static class ArpHelper
{
    [DllImport("iphlpapi.dll", ExactSpelling = true)]
    static extern int SendARP(uint destIP, uint srcIP, byte[] macAddr, ref uint physAddrLen);

    [DllImport("ws2_32.dll", CharSet = CharSet.Ansi)]
    static extern uint inet_addr(string cp);

    public static string GetMacFromIp(string ip)
    {
        uint dest = inet_addr(ip);
        byte[] mac = new byte[6];
        uint len = (uint)mac.Length;
        int err = SendARP(dest, 0, mac, ref len);
        if (err != 0) throw new Win32Exception(err);
        return string.Join(":", mac.Take((int)len).Select(b => b.ToString("X2")));
    }
}

On PDA/Windows Mobile: is correct that Compact Framework lacks the desktop APIs. When OpenNETCF shows two Ethernet-like MACs, one is typically Wi-Fi and the other is USB/ActiveSync (RNDIS). Choose the adapter that owns the IP bound to your socket. Practical rule: take the interface that is Up and whose unicast IP equals ((IPEndPoint)socket.LocalEndPoint).Address on the PDA. That MAC is the one used to send packets.

For the image issue, you can either fit on the client or pre-scale on the server. Client quick fix: pictureBox.SizeMode = PictureBoxSizeMode.Zoom. Server-side high-quality resize that preserves aspect ratio:

static Image ResizeToFit(Image src, int maxW, int maxH)
{
    double r = Math.Min((double)maxW / src.Width, (double)maxH / src.Height);
    int w = (int)Math.Round(src.Width * r), h = (int)Math.Round(src.Height * r);
    var bmp = new Bitmap(w, h);
    using (var g = Graphics.FromImage(bmp))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.DrawImage(src, 0, 0, w, h);
    }
    return bmp;
}

For Java, the same L2 limitation applies; you cannot get a remote MAC off-LAN. Use ARP/native calls only when client and server share a subnet.

Recommended Answers

All 7 Replies

Refer these links.

PhysicalAddress Class
Get MAC address of client machine using C#

Also try this code snippet.

using System;
using System.Windows.Forms;
using System.Management; 
.
.
.

 private void button1_Click(object sender, EventArgs e)
        {
            ManagementObjectSearcher objQuery = null;
            ManagementObjectCollection queryCollection = null;

            try
            {
                objQuery = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");

                queryCollection = objQuery.Get();

                foreach (ManagementObject mgmtObject in queryCollection)
                {
                    if (mgmtObject["MacAddress"] != null)
                    {
                        MessageBox.Show(mgmtObject["MacAddress"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Source);
                MessageBox.Show(ex.Message);
            } 

        }

Are you running your software on both ends of the connection? MAC addresses are only "guaranteed valid" for layer 2 communications in a LAN. They do not traverse a router as you would think.

That being said if it is your software running on both ends then you can use the code Ramesh provided. If you just have an open socket then you can't reliably determine the remote MAC.

Thanks Ramesh.
I could use it to find a MAC address of local machine when local machine is a laptop or desktop.

But when I tried to do the same on PDA, I found that it does not have system.net.management and system.net.networkinterface namespace

Do you have any idea about how to get MAC addresses on PDA?

Thanks
Siddhesh

Hi Siddesh,

The System.Net.NetworkInformation will not be supported in Compact Framework.

You need to use OpenNETCF library to get the MAC address of a PDA.

The OpenNETCF.Net.NetworkInformation Namespace can have classes and methods to achieve this.

hi,
Got MAC address of PDA.
But now i get 2 mac addresses, both belong to same networktype "Ethernet".
If I see system configuration of WM, then I see one of the above physical addresses.
Which one does windows mobile use to send packets on network?
(one that is listed in system configuration or other physical address which is returned by opennetcf library functions?)

Thanks all.

Hi All,

Can someone help me out in getting client mac address by using java

Thanks in advance

You ask for Java and tag with C#
Which is it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.