Guest User

Client Code

a guest
Mar 18th, 2015
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Management;
  10.  
  11. namespace Client
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             const int Port = 4800;
  18.             string serverIp = "?";
  19.  
  20.             //Get all addresses
  21.             string hostname = System.Net.Dns.GetHostName();
  22.             IPHostEntry allLocalNetworkAddresses = Dns.Resolve(hostname);
  23.  
  24.             //Walk thru all network interfaces.
  25.             foreach (IPAddress ip in allLocalNetworkAddresses.AddressList)
  26.             {
  27.                 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  28.  
  29.                 //Bind on port 0. The OS will give some port between 1025 and 5000.
  30.                 client.Bind(new IPEndPoint(ip, 0));
  31.  
  32.                 //Create endpoint, broadcast.
  33.                 IPEndPoint AllEndPoint = new IPEndPoint(IPAddress.Broadcast, Port);
  34.  
  35.                 //Allow sending broadcast messages
  36.                 client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  37.  
  38.                 //Send message to everyone on this network
  39.                 client.SendTo(new byte[] { 1 }, AllEndPoint);
  40.                 Console.Write("Client send '1' to " + AllEndPoint.ToString() + Environment.NewLine);
  41.  
  42.                 try
  43.                 {
  44.                     //Create object for the server.
  45.                     IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
  46.                     EndPoint tempRemoteEP = (EndPoint)sender;
  47.                     byte[] buffer = new byte[1000];
  48.  
  49.                     //Recieve from server. Don't wait more than 3000 milliseconds.
  50.                     client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
  51.  
  52.                     //Recive message, save wherefrom in tempRemoteIp
  53.                     client.ReceiveFrom(buffer, ref tempRemoteEP);
  54.                     Console.Write("Client got '" + buffer[0] + "' from " + tempRemoteEP.ToString() + Environment.NewLine);
  55.  
  56.                     //Get server IP (ugly)
  57.                     serverIp = tempRemoteEP.ToString().Split(":".ToCharArray(), 2)[0];
  58.  
  59.                     //Don't try any more networkss
  60.                     break;
  61.                 }
  62.                 catch
  63.                 {
  64.                     //Timout. No server answered. Try next network.
  65.                 }
  66.             }
  67.             Console.Write("Service IP: " + serverIp + Environment.NewLine);
  68.  
  69.             try
  70.             {
  71.  
  72.                 ManagementObjectSearcher searcher =  new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_Processor");
  73.  
  74.                 foreach (ManagementObject queryObj in searcher.Get())
  75.                 {
  76.                     Console.WriteLine("-----------------------------------");
  77.                     Console.WriteLine("Win32_Processor instance");
  78.                     Console.WriteLine("-----------------------------------");
  79.                     Console.WriteLine("AddressWidth: {0}", queryObj["AddressWidth"]);
  80.                     Console.WriteLine("Architecture: {0}", queryObj["Architecture"]);
  81.                     Console.WriteLine("Availability: {0}", queryObj["Availability"]);
  82.                     Console.WriteLine("Caption: {0}", queryObj["Caption"]);
  83.                     Console.WriteLine("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]);
  84.                     Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);
  85.                     Console.WriteLine("CpuStatus: {0}", queryObj["CpuStatus"]);
  86.                     Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);
  87.                     Console.WriteLine("CurrentClockSpeed: {0}", queryObj["CurrentClockSpeed"]);
  88.                     Console.WriteLine("CurrentVoltage: {0}", queryObj["CurrentVoltage"]);
  89.                     Console.WriteLine("DataWidth: {0}", queryObj["DataWidth"]);
  90.                     Console.WriteLine("Description: {0}", queryObj["Description"]);
  91.                     Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
  92.                     Console.WriteLine("ErrorCleared: {0}", queryObj["ErrorCleared"]);
  93.                     Console.WriteLine("ErrorDescription: {0}", queryObj["ErrorDescription"]);
  94.                     Console.WriteLine("ExtClock: {0}", queryObj["ExtClock"]);
  95.                     Console.WriteLine("Family: {0}", queryObj["Family"]);
  96.                     Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);
  97.                     Console.WriteLine("L2CacheSize: {0}", queryObj["L2CacheSize"]);
  98.                     Console.WriteLine("L2CacheSpeed: {0}", queryObj["L2CacheSpeed"]);
  99.                     Console.WriteLine("L3CacheSize: {0}", queryObj["L3CacheSize"]);
  100.                     Console.WriteLine("L3CacheSpeed: {0}", queryObj["L3CacheSpeed"]);
  101.                     Console.WriteLine("LastErrorCode: {0}", queryObj["LastErrorCode"]);
  102.                     Console.WriteLine("Level: {0}", queryObj["Level"]);
  103.                     Console.WriteLine("LoadPercentage: {0}", queryObj["LoadPercentage"]);
  104.                     Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
  105.                     Console.WriteLine("MaxClockSpeed: {0}", queryObj["MaxClockSpeed"]);
  106.                     Console.WriteLine("Name: {0}", queryObj["Name"]);
  107.                     Console.WriteLine("NumberOfCores: {0}", queryObj["NumberOfCores"]);
  108.                     Console.WriteLine("NumberOfLogicalProcessors: {0}", queryObj["NumberOfLogicalProcessors"]);
  109.                     Console.WriteLine("OtherFamilyDescription: {0}", queryObj["OtherFamilyDescription"]);
  110.                     Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
  111.  
  112.                     if (queryObj["PowerManagementCapabilities"] == null)
  113.                         Console.WriteLine("PowerManagementCapabilities: {0}", queryObj["PowerManagementCapabilities"]);
  114.                     else
  115.                     {
  116.                         UInt16[] arrPowerManagementCapabilities = (UInt16[])(queryObj["PowerManagementCapabilities"]);
  117.                         foreach (UInt16 arrValue in arrPowerManagementCapabilities)
  118.                         {
  119.                             Console.WriteLine("PowerManagementCapabilities: {0}", arrValue);
  120.                         }
  121.                     }
  122.                     Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]);
  123.                     Console.WriteLine("ProcessorId: {0}", queryObj["ProcessorId"]);
  124.                     Console.WriteLine("ProcessorType: {0}", queryObj["ProcessorType"]);
  125.                     Console.WriteLine("Revision: {0}", queryObj["Revision"]);
  126.                     Console.WriteLine("Role: {0}", queryObj["Role"]);
  127.                     Console.WriteLine("SecondLevelAddressTranslationExtensions: {0}", queryObj["SecondLevelAddressTranslationExtensions"]);
  128.                     Console.WriteLine("SocketDesignation: {0}", queryObj["SocketDesignation"]);
  129.                     Console.WriteLine("Status: {0}", queryObj["Status"]);
  130.                     Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);
  131.                     Console.WriteLine("Stepping: {0}", queryObj["Stepping"]);
  132.                     Console.WriteLine("SystemCreationClassName: {0}", queryObj["SystemCreationClassName"]);
  133.                     Console.WriteLine("SystemName: {0}", queryObj["SystemName"]);
  134.                     Console.WriteLine("UniqueId: {0}", queryObj["UniqueId"]);
  135.                     Console.WriteLine("UpgradeMethod: {0}", queryObj["UpgradeMethod"]);
  136.                     Console.WriteLine("Version: {0}", queryObj["Version"]);
  137.                     Console.WriteLine("VirtualizationFirmwareEnabled: {0}", queryObj["VirtualizationFirmwareEnabled"]);
  138.                     Console.WriteLine("VMMonitorModeExtensions: {0}", queryObj["VMMonitorModeExtensions"]);
  139.                     Console.WriteLine("VoltageCaps: {0}", queryObj["VoltageCaps"]);
  140.                 }
  141.  
  142.             }
  143.             catch (ManagementException e)
  144.             {
  145.                 Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
  146.             }
  147.  
  148.             Console.WriteLine();
  149.             Console.ReadKey();
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment