C # remote boot (LAN test pass)

Source: Internet
Author: User

First introduce the relevant knowledge.
UDP Protocol
UDP is the abbreviation of User Datagram protocol, the Chinese name is a Subscriber datagram protocol, is a connectionless transport layer protocol in the OSI (Open System interconnection, open Systems Interconnect) Reference Model, Provides transaction-oriented, simple, unreliable information delivery services.
Here are some of the differences between UDP and TCP:
**a) **UDP is not connected. Compared to the TCP protocol, the UDP protocol does not need to establish a connection before transmitting the data, and of course it does not release the connection.
**B) **UDP is doing its best to deliver. This means that the UDP protocol cannot guarantee that the data can be delivered to the destination host accurately. There is no need to verify the UDP packets received.
**C) **UDP is message-oriented. That is, the UDP protocol encapsulates the data transmitted by the application layer in a UDP packet without splitting or merging. Therefore, the transport layer after receiving the other side of the UDP packet, will be removed after the header, the data intact to the application process.
**D) **udp no congestion control. Therefore, the transmission rate of the UDP protocol does not affect the congestion of the network.
**E) **UDP supports a pair of one or one-to-many, many-to-one, and many-to-many interactive communications.
**F) **UDP's head occupies a smaller size and occupies only 8 bytes.

blocking and non-blocking
Simply understood as the need to do one thing can immediately get a return response, if not immediately get back, need to wait, then blocked, otherwise it can be understood as non-blocking.
Blocking and non-blocking concerns the state of the program when it waits for the call result (message, return value).
A blocking call means that the current thread is suspended until the call results are returned. The calling thread will not return until the result is obtained.
A non-blocking call means that the call does not block the current thread until the result is not immediately available.
Give me a chestnut:
You ask the bookstore owner there is no "in the way MFC" This book, if you are blocking calls, you will always put yourself "hang" until the book has no results; If the call is non-blocking, you don't care if the boss tells you that you're going to play first, Of course, you'll have to check the boss for a few minutes occasionally.

IPAddress Class
Internet Protocol address available
ipaddress.broadcast Field
Provides an IP broadcast address, which is read-only.
Syntax: public static readonly IPAddress broadcast
The broadcast field is equivalent to a dot-delimited, four-part notation format of 255.255.255.255

UdpClient class
Providing User datagram (UDP) network services
Syntax: public class udpclient:idisposable
The UdpClient class provides some simple ways to send and accept a non-connected UDP datagram in block mode.
You can choose to use one of the following two methods to establish a default remote host:
1. Create an instance of the UdpClient class with the remote host name and port number as parameters.
2. Create an instance of the UdpClient class, and then call the Connect method.

Udpclient.connect Method (IPAddress, Int32)
Establishes a default remote host with the specified IP address and port number.
Namespaces: System.Net.Sockets
Assembly: System (in System.dll)
Syntax: public void Connect (IPAddress addr, int port)
Parameters:
Addr The IPAddress of the remote host to which you want to send data
Port number to which the port will send the data.

If you specify ipaddress.broadcast when you call the Send method, you can broadcast the data to the default broadcast address: 255.255.255.255.
Udpclient.send Method (byte[], Int32)
Sends a UDP datagram to a remote host.
Syntax: public int Send (byte[] dgram, int bytes)
Parameters:
An array of type Dgram byte that specifies the UDP datagram that you intend to send as byte data
Number of bytes in the bytes datagram
Return value: The number of bytes sent.
Call this method to send datagrams to the remote host established in the Connect method and return the number of bytes sent. If connect is not called before this overload is called, the Send method throws an exception (Sockeexception).

MAC address
MAC (Media access control or medium access control) address, which is a translation of media access controls, or physical addresses, hardware addresses, to define the location of network devices. In the OSI model, the third layer of the network layer is responsible for the IP address, and the second layer of data link is responsible for the MAC address. So a host will have a MAC address, and each network location will have an IP address dedicated to it. The MAC address is determined by the NIC and is fixed.
Talk about the MAC address, have to say the IP address. The IP address works in the third layer of the network layer of the OSI Reference Model. Between the two clear division of labor, tacit cooperation, complete the communication process. The IP address focuses on the network layer, forwarding the packet from one network to another, while the MAC address focuses on the data link layer, transferring one data frame from one node to another node of the same link.

get the native MAC address
Run cmd, type command ipconfig/all

remote wake-up
Remote boot Wake Onlan (WOL), commonly known as remote wakeup, is now supported by many network cards. and the realization of remote wake, mainly to the target host to send a special format of the packet, is produced by AMD Magicpacket this software to generate a network wake-up required special data packets, commonly known as Magic Packet. Although the Magicpacket format is an AMD-developed technology that is not universally accepted, it is still supported by many network card manufacturers, so many NICs with Wake-on-LAN capabilities are compatible.

Principle we do not need to go deep, the implementation is to send a broadcast package, the contents of the package includes the following data on it. FF FF FF FF ff,6 FF is the beginning of the data, followed by 16 times the MAC address is ready.

With the above basic knowledge, we will begin our project. Use the computer (PC-A) to wake up the computer (pc-b) in the local area network.
Set Pc-b
First, the BIOS and network card settings are required to start the computer and enter the BIOS parameter settings. Select the Power Management setup option for the Wake up on LAN entry and the Wake on PCI Card entry to enable the remote wake feature for this computer.
get the MAC address of Pc-b
Enter Ipconfig/all by command line to get pc-b address 0B 2F 9E

Encoding implementation

Using System;Using System.Collections.Generic;Using System.Linq;Using System.Text;Using System.Windows;Using System.Windows.Controls;Using System.Windows.Data;Using System.Windows.Documents;Using System.Windows.Input;Using System.Windows.Media;Using System.Windows.Media.Imaging;Using System.Windows.Navigation;Using System.Windows.Shapes;Using System.Net.Sockets;Using System.net;namespace remote wake on {///The interactive logic of MainWindow.xaml///PublicPartialClass Mainwindow:window {PublicMainWindow () {InitializeComponent ();}PublicStaticvoidWakeUp (Byte[] (MAC) {UdpClient client =New UdpClient (); Client. Connect (Ipaddress.broadcast,9090);MessageBox.Show (IPAddress.Broadcast.ToString ());byte[] Packet =Newbyte[17 *6];for (int i =0; I <6; i++) Packet[i] =0xFF;for (int i =1; I <=16; i++)for (Int J =0; J <6; J + +) Packet[i *6 + j] = Mac[j]; int result = client. Send (packet, packet. Length); } private void button1_Click (object sender, RoutedEventArgs e) {byte[] mac = new byte[6"; Mac[0] = 0x00; Mac[1] = 0x0B; Mac[2] = 0x2f; Mac[3] =  0x70 Mac[4] = 0x40; Mac[5] = 0X9E; WakeUp (MAC); } }} 

C # remote boot (LAN test pass)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.