Remote screenshot display and Control Technology Based on socket communication

Source: Internet
Author: User
Tags bmp image set socket

Abstract: Socket communication and Mixed Programming Technology of MFC and SDK are used to remotely capture the screen of the target machine and implement the control function of the local machine on the target machine. The local machine interface uses MFC and the target machine uses SDK. This implementation scheme facilitates the implementation of the local machine as the client control interface, at the same time, it meets the requirements of the target machine as the server without interface and only implement Socket communication.

Key words: WinSocket; socket communication; remote control; screenshot

Display and Control of Remote Obtaining Screen Based on Socket Communication
ZHONG Wen, YU Xinsheng
(Embedded System Dept., East China Institute of Computer Technology, Shanghai 200233)
【Abstract】 Based on the Socket communication, this paper uses the program technology of MFC and SDK to obtain the remote machine's screen and realize the control function. in the paper, the interface of client use MFC, and the server use SDK, this design can realize the interface of client implements diently, and adapt for the requirement that the server not need interface but socket communication.
[Key words] WinSocket; Socket communication; Remote control; Obtaining screen

Remote control technology has broad application prospects in maintenance, monitoring, and fault diagnosis of remote equipment (software), and mostly uses the Client/Server mode. This structure includes multiple computers connected to the network. The computers that process applications and request services from another computer are clients (local machines ), the computer that responds to and processes the request is called the server (target machine ).
The principle of remote control [1] is: when a user connects to the network, the customer program sends authentication information and requests to connect to the remote host. The remote host's server program verifies the customer's identity, if the verification is successful, a connection is established with the customer and information about the authentication and established connection is sent to the user. In this case, the user can send the commands to be executed to the remote host through the client program, while the server program executes these commands and refresh the keyboard, mouse, and screen to the client program, the client program displays the host screen and other information through processing, so that the user can operate on the remote host in person. This method is called remote control over remote service ).
To sum up, to achieve Remote Control of the client on the server, we need to discuss three issues, including dual-machine Socket network communication, remote screen capture display, and screen data transfer control.

1 dual-host Socket network communication[2]

Initialize the socket on the server: Initialize the socket before calling the socket, that is, load the DLL of the corresponding version. by calling the wsastartup function, fill in the information about the successfully loaded socket library version in the lpwsadata structure;
Wsadata lpwsadata;
Wsastartup (makeword (1, 1), & lpwsadata );
Create server socket: After initialization, call the socket function to create a socket and return the socket handle. It is always used to identify the socket in subsequent communication. If the call fails, invalid_socket is returned;
Socket sktconnect = socket (af_inet, sock_stream, 0 );
BIND server address: after creating a socket for a specific protocol, bind the socket to a local address. The socket type is sockaddr, which is used to specify the socket binding address, including IP addresses and port numbers;
Bind (sktConnect, (struct sockaddr far *) & sockaddrin, sizeof (sockaddrin ));
Server listening network: the socket uses the listen function to set the status bit to detect whether there are incoming connection requests, and then calls the accept function to prepare to receive client connection signals. When there is no connection request, service Process is blocked;

Listen (sktConnect, 1 );
SktClient = accept (sktConnect, (struct sockaddr far *) & sockaddrin, & sockaddrlen );

Initialize and create client Winsock: first, use the AfxSocketInit function to determine whether the lpwsaData parameter is null, so as to determine whether to call the WSAStartup function to fill in the WSADATA structure, and then call the socket function to create the client socket, assign values to the Sockaddr_in structure of the client. The address type and port number are the same as those of the server;
Socket option settings: Use the setsockopt function to set socket options, such as sending or receiving timeout settings, buffer capacity settings, and ioctlsocket function to set socket I/O modes;

Int ret = ioctlsocket (sktClient, FIONBIO, (unsigned long *) & ul );

Establish a connection between the two parties: the client calls the connect function to send a connection request to the server. When the connection request arrives, the accept function of the blocked server generates a new byte stream socket and returns the Sockaddr_in structure variable of the client, connect to the client using a socket with the client address, and then return the receiving signal to the client;
Connect (sktClient, (const struct sockaddr *) & sockaddrin, sizeof (sockaddrin ));
Send and receive data: Once the client socket receives a signal from the server, it indicates that the two sides have connected, either party can use the Send/Write function and the Recv/Read function to Send or receive data to or from the other party;
Send (sktClient, chrSend, 10, 0); recv (sktClient, chrReceive, 10, 0 );

Disable socket and winsock logout: the server and client can call the closesocket function to disable all sending and receiving operations on the socket, cancel the socket, and interrupt the connection. At the same time, after the dynamic link library of the winsock service is used, the application must call the WSACleanup function to unregister it and release the allocated resources.
Winsock socket main workflow 1, ① ~ The sequence of network data exchange.

Figure 1 main workflow of Winsock socket

2 Remote screenshot Screen Display

After receiving the screen data request from the client, the server uses the handle of the current screen device to copy the screen data to the opened memory area to obtain the device-related GDI bitmap; then, the device-independent DIB bitmap is obtained by setting the bitmap information header and palette.

2.1 obtain the device-related bitmap of the current screen [3] (DDB)

The device-related Bitmap (DDB) is also called the graphical device interface (GDI) bitmap. It is stored in the MFC library using the CBitmap class. This object contains the data structure of the device-related GDI module. When the application intercepts the data displayed on the screen, it fills the data in the open-up compatibility memory area and establishes an association with the CBitmap object handle to back up the GDI bitmap data. However, because the bit arrangement in the GDI bitmap is completely dependent on the display device, it is meaningless to transmit the GDI bitmap between different types of computers. Therefore, further conversion is required to obtain the device-independent bitmap DIB.

(1) obtain the resolution of the current screen to determine the screenshot range;
ScreenX = GetSystemMetrics (SM_CXSCREEN );
ScreenY = GetSystemMetrics (SM_CYSCREEN );

(2) obtain the screen HDC, open the compatibility memory area, and establish a compatible HBITMAP;
HDC hdcmy = CreateDC ("DISPLAY", NULL );
HDC hbufdc = CreateCompatibleDC (hdcmy );
HBITMAP hBit = CreateCompatibleBitmap (hdcmy, ScreenX, ScreenY );

(3) copy the current screen content to the previously opened memory area to obtain the GDI bitmap of the current screen;
HBITMAP hOldBitmap = (HBITMAP) SelectObject (hbufdc, hBit );
StretchBlt (hbufdc, 0, 0, ScreenX, ScreenY, hdcmy, 0, 0, ScreenX, ScreenY, SRCCOPY );
HBit = (HBITMAP) SelectObject (hbufdc, hOldBitmap );

2.2 convert device-related Bitmap (DDB) to device-independent bitmap [3] (DIB)

DIB comes with color information to manage the palette. Any computer running Windows can process this standard bitmap format. The BMP file contains a DIB, it consists of four parts: the bitmap file header, the bitmap information header, the color palette, And the DIB image data. The conversion from DDB to DIB actually uses the image information contained in DDB, fill in the remaining three parts of DIB in addition to the bitmap file header to obtain bitmap data unrelated to the device. Finally, you can add a bitmap file header to form a standard BMP image.

(1) Use the BITMAP handle hBit to obtain the BITMAP information, then fill in the BITMAPINFOHEADER structure, calculate the InfoHeader length, initialize the color palette, and finally allocate a storage space to store the information header and color palette data;
GetObject (hBit, sizeof (bitmap), (LPSTR) & bitmap );
Int ncolors = 1 <(bitmap. bmPlanes * bitmap. bmBitsPixel );
DWORD dwLen = sizeof (BITMAPINFOHEADER) + ncolors * sizeof (RGBQUAD );
HANDLE hDib = GlobalAlloc (GMEM_FIXED, dwLen );

(2) Calculate the actual number of bytes occupied by the bitmap data so that its width is greater than or equal to the integer multiple closest to 4, and correct the original biSizeImage value, then, the system recalculates and allocates space for storing information headers, color palette, and actual image data;
Bi. biSizeImage = (bi. biWidth * bi. biBitCount) + 31 )&~ 31)/8) * bi. biHeight;
DwLen + = bi. biSizeImage;
If (handle = GlobalReAlloc (hDib, dwLen, GMEM_MOVEABLE) hDib = handle;
(3) copy the above information header, color palette, and actual image information to the specified storage area, and return the handle of the storage area to obtain the final DIB bitmap;
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER) hDib;
GetDIBits (hdc, bitmap, 0L, (DWORD) bi. biHeight, (LPBYTE) lpbi + (bi. biSize + ncolors
* Sizeof (RGBQUAD), (LPBITMAPINFO) lpbi, (DWORD) DIB_RGB_COLORS );

3. Screen data transfer control

The screen data transmission control mainly determines the connection between the server and the client, how the server regularly sends screen data in blocks, and how the client concatenates screen data and displays images, how does the server respond to client mouse events.

3.1 confirm the connection between the server and the client
The client can specify the IP address of the server to connect to the client, or send the ID of the Communication peer to the client within the subnet segment. After receiving the ID, the server sends a confirmation ID to the client. After receiving the confirmation information, the client, indicates that both parties achieve connection.
The following code is a piece of client program. The server program matches the order of the sending and receiving functions.

Char cFlag [8] = "CopyScr/0 ";
Send (sktClient, cFlag, 8, 0); // send the client flag
Recv (sktClient, cFlag, 8, 0); // receiving server flag

3.2 server periodically sends screen data in multiple parts
The client sends a request to the server for screen data at an interval set by a timer. After receiving the request, the server obtains the GDI bitmap data of the current screen and converts it to DIB bitmap data, next, the screen bitmap data is sent to the client through multipart transmission. The multipart process is as follows:

(1) send information related to screen bitmap data, such as size, length, and height, to the client;
Send (sktClient, (char *) & ScrInfo, sizeof (ScrInfo) + 1, 0 );

(2) Send DIB bitmap data in multiple parts. Use SENDBLOCK as the Part Size and adjust the current Data Pointer position;
LPBYTE plmagePoint = (LPBYTE) hDib;
For (WORD I = 0; I <(ScrInfo. dwSize/SENDBLOCK); I ++ ){
Send (sktClient, (char *) plmagePoint, sizeof (BYTE) * SENDBLOCK, 0 );
PlmagePoint = plmagePoint + SENDBLOCK;
Recv (sktClient, (char *) & StopFlag, sizeof (int) + 1, 0 );}

(3) When the screen bitmap data is not exactly equal to the block size multiple, it is used to process the remaining data transmission;
If (ScrInfo. dwSize % SENDBLOCK)
Send (sktclient, (char *) plmagepoint, scrinfo. dwsize % sendblock, 0 );

3.3 clients splice screen data and display images
The client's screen data splicing program corresponds to the server's screen data splitting program. First, it receives information about the screen bitmap and receives screen data according to the specified part size, finally, the screen data smaller than the block size is received and processed separately to obtain the complete screen data of the server. The bitmap is displayed using the stretchdibits function.
Stretchdibits (DC, 0, 0, rect. Right, rect. Bottom, 0, 0,
(Lpbitmapinfoheader) svrdata)-> biwidth,
(Lpbitmapinfoheader) svrdata)-> biheight,
(Lpbyte) svrdata + (sizeof (bitmapinfoheader) + color * sizeof (rgbquad )),
(Lpbitmapinfo) svrdata, dib_rgb_colors, srccopy );

3.4 The server responds to client mouse events
When you click the client to display the current screen area of the server, the client program will record the specific left/right-click, single/double-click, x/y coordinates and other information, the server sends a mouse event to the server. The server then parses the event and responds accordingly. This allows the client to obtain and control the server screen.
Mouse_event (mouseeventf_leftdown, 0, 0, getmessageextrainfo ());

4 Conclusion

Through the above method, the client can regularly receive the current screen information of the server, and the server can also respond to the mouse events of the client to achieve remote control of the local machine on the target machine. This display control technology has been successfully applied to the remote monitoring system of electronic measuring instruments.
References
1 Chang yongchang, Feng Xinxi, Wang Fang. Design and Implementation of a remote control software [J]. Computer Applications, 2003, 23 (3 ).
2 Zheng lingxiang, Hong jingxin. Programming and application of the original socket in Windows 2000/XP [J]. minicomputers and applications, 2002, 21 (6 ).
3 He bin, Ma Tianyu, and Wang yunjian. Visual C ++ Digital Image Processing (version 2nd). Beijing: People's post and telecommunications Publishing House, 2002-12.

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.