Applying remote control to teaching has become an important means of computerized teaching at present. There must be many netizens who want to understand the programming principles of this online teaching method, right? Here we will use a simple remote control program as an example to illustrate the basic principles of this network programming. This program takes Delphi programming as an example.
The working mechanism of this program is very simple. The controlled party runs a program to listen to the port and receive data packets, and the master control sends data packets to the controlled party's port through the port. According to this principle, we write two programs, one of which is the controller and the other is the controlled one, and run these two programs on two different machines. The controlled machine waits for the data sent by the controller and then Perform the corresponding operation (such as restart in this example).
There are two types of controls in Delphi that can achieve the above purpose. One type is that the controller uses ClientSocket, the controlled party uses ServerSocket (both on the Internet page), and the other type is that both parties use NMUDP (both on the FastNet page). We know that network transmission is unreliable, that is, the transmitted data may be lost. The difference between these two types of controls is that the former uses TCP (Transfer Control Protocol). The TCP protocol is connection-oriented. Each time the two parties establish a connection (or disconnect), it requires three handshakes, which is time-consuming, but the data transmission is reliable; the latter uses UDP (User Datagram Protocol), which is non-oriented. For connected data, the other party does not need to confirm it. This speed is faster than TCP, but the data may be lost, so it is unreliable. Since the amount of data required for control is not large and requires high reliability, the former is generally used, and the procedure description is as follows:
The first step is to start the controller program, add the clientsocket control, name it control, set the host attribute to the IP address of the controlled machine, and the port attribute is 1234 (the port can be set at will, but do not repeat it with some default ports, such as 80, etc. ).
The second step is to add the code control.open to the FormCreate event; //Open communication with the controlled machine.
The third step is to add a Button, set the caption to "restart", and add the code control.Socket.SendText(''reboot''); //Notify the controlled machine to restart. This completes the work of the controller.
The fourth step is to start the program of the controlled party, add the serversocket control, name it undercontrol, set the port attribute to 1234 (which is consistent with the control party's port), and the active attribute to true.
Step 5: Add the code to the OnclientRead event of the undercontrol ifSocket.ReceiveText=''reboot'' then
ExitWindowsEx(EWX_REBOOT, 2); //Restart API function
This completes the code work, and then compiles these two programs to generate two .exe files and runs them on two machines (remember to run the controlled machine program under Win98 first), and press "Restart" on the controller ” Button (button), the controlled machine restarts. The remote control machine restart was successfully implemented.