Read another computer screen in Delphi

zhaozj2021-02-11  219

Read the screen road of another computer in Delphi 2000-10-25 09:50:30 [Summary] Teachers who have written WINSOCK applications know that writing Winsock applications is never a light. You must not deal directly with the API in the complex Winsock. Fortunately, the TclientSocket and TServersocket in Delphi4 encapsulate the API in Windows, which is great to simplify the Winsock access, so we can write Winsock applications very easy. . This article describes how to write a Winsock application with Delphi through an example of another computer screen within a local area network. People who have done the network management can have such an experience. Through phone "remote control" to guide others how upset, and I am a lazy person, I don't want to be a little small from the top of the building every day. ,How to do it? How about reading a program reading another computer screen? Not intuitive. Communication in the LAN, the best choice is of course used by Winsock, programmers who have written WINSOCK applications know that writing Winsock applications is never a light, you can't deal with the API in complex Winsock, Fortunately, the TclientSocket and TServersocket in Delphi4 encapsulates the API in Windows, which greatly simplifies Winsock access, making us very easily write Winsock applications. Despite this, it is best to have some understanding of Winsock. Here I will not go to it later, you can find some books yourself. Transferring data through network, you need at least one pair of sockets, one of which is on the client, the other on the server, once the client is connected to the server's socket, you can communicate with each other, and establish a connection with the Socket is built in TCP / On the basis of IP, IPX / SPX and other related protocols are also supported. In Delphi, use TclientSocket and TSERVERSOCKET to manipulate the connection and communication of the client and the server socket. To explain, these two components are used to manage the connection between the server and the customer. It is not a socket object, and the stell object is Tcustomwinsocket, such as TclientWinsocket, TServerClientwinsocket, TSERVERWInsocket. First, TclientSocket Element: Add a TCLIENTSOCKET to FORM, the application becomes a TCP / IP client, you can use TclientSocket to manipulate the client's socket object. To establish a connection with the server, you should first specify the server to connect. There are two ways to specify the server, one is to set the host name of the host specified by the host, such as http://www.inprise.com or the machine name in the LAN, this way is intuitive, but to perform domain name resolution, speed will be slightly Slowly; another method is to set the AdRess property to specify the IP address of the host, such as 130.0.1.1. These two methods are equivalent, but if Host and AdRess are set up, Delphi will only use the host property.

Then specify the port number of the connection server, there are two ways, one is to set the service using the default port number, one is to set the port number directly, many of the port numbers below 1024, have been allocated, such as FTP The ports of 20 and 21, the port of SMTP is 25, the port of the web server is 80, etc., in order to prevent unintentional conflicts, it is recommended to set the port to 1024 or more when preparing your own application. If Service and Port, Delphi will use the service default port. After specifying the server and port number, call the Open method or set the Active property to true. The client's socket will make a connection request to the server's socket. If the server is in a listening state, it will automatically accept the request to establish a connection, establish When connected, its ONCONNET event is triggered. When you need to disconnect, just call the Close method or set the Active property to false, which is triggered. Second, TSERVERSOCKET components: Like TClientSocket, build a server, just need to put a TSERVERSOCK component in Form. The server's Socket object is more complicated. When the server is in a listening state, the server's socket object is manipulated with TSERVERSOCKET; when the customer makes a request, the server responds to request and establishes the connection, at which point the server socket and the client's socket connection with the TServerClientWinsocket. To make the server in listening, you must first specify the port number, of course, should be the same as the client's port number. Then call the Open method or the Active property to True. Third, communication: Once the client is established and the server is established, it can be communicated with each other. Delphi provides several communication methods for TSERVERSOCKET and TCLIENTSocket, sending this information with SendText, sending flows with SendStream, and transmits data for specified lengths with SendBuf. It should be noted that since the Windows default buffer size is 4K, when sending less than 4K information, for example, a binary stream sent from the server to the client, only Socket.sendStream () is only available in the server. And on the client, it will trigger an OnRead event multiple times, and Delphi does not define an event such as "OnReadend", so the programmer must assemble the data when receiving. The method taken here is to send the flow length to the client, then send a stream, the client writes the received data into a stream, when the flow length is equal to the length of the server, indicates that the client has received it. . For the server, as the stream of the SendStream parameter, it will end when the Socket is "owned". It will also end, not to release it, otherwise, it will trigger an exception. Similarly, when the sending text is less than 4K, such as the following calls, ClientSocket1.Socket.sendText ('gets'); clientsocket1.socket.sendtext ('gets'); clientsocket1.socket.sendtext ('gets' ); When the server receives, the phenomenon such as GetSgets is possible, which may be because the data within the buffer has not been sent, and the new text is placed in the buffer, the computer also processes it as the same batch of data. The reason is.

To avoid this phenomenon, you can use a "throwing" practice in the program: Client server clientsocket1.socket.sendtext ('data1') socket.receirtExt; socket.sendtext ('ok') (socket.ReceiveText; ClientSocket1.socket.sendtext ('data2') socket.ReceirtExt; socket.sendtext ('end'); socket.receirtExt; After running the server program on another computer, in your client program Enter the computer name in the above box, pick up "connection", press "Take pictures", how, the other party's screen is unbelieving. The following is all the source code of the program, this program runs in NT4.0, Win95, Win98, and the LAN. Of course, Windows must be loaded with TCP / IP protocol, and must have dynamically assigned or specified IP addresses. If you see the "command", "Command" is still more trouble, you can also analyze the keyboard, mouse event on Image1, then send it to the server, after receiving, then do the same operation, so you can do not need trouble Operator. With Delphi's TClientSocket and TServersocket, you can also complete the development of applications such as file replication, online chat, ICQ, and it is very simple. You can freely play your imagination, write a more charming program.

The client program: unit cmain; interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ScktComp, StdCtrls, ExtCtrls, jpeg; typeTForm1 = class (TForm) Panel1: TPanel; ScrollBox1: TScrollBox; Image1: TImage; Button1 : TButton; Edit1: TEdit; Button2: TButton; ClientSocket1: TClientSocket; Label1: TLabel; procedure Button1Click (Sender: TObject); procedure Button2Click (Sender: TObject); procedure ClientSocket1Connect (Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Error ( Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); procedure ClientSocket1Read (Sender: TObject; Socket: TCustomWinSocket); procedure FormCreate (Sender: TObject); procedure FormClose (Sender: TObject; var Action: TCloseAction) ; private {Private declarations} public {public declarations} end; varForm1: TForm1; c: longint; m: tmemorystream; implementation {$ R * .DFM} procedure TForm1.Button1Click (Sender: TObject); begintryclientsocket1.Close; clientsocket1.Host : = Edit1.Text; ClientSocket 1.Open; // Connection server ExcePTSHOWMESSAGE (Edit1.Text # 13 # 10 'is not turned on or unstably installed "); end; end; procedure tform1.button2click (sender: TOBJECT); beginclientsocket1.socket.sendText 'gets'); // Send an application, notify the server to need screen image End; procedure tform1.clientSocket1connect (Sender: Tobject; socket: tcustomwinsocket); begincaption: = 'Connect to' Edit1.Text; End; Procedure TFORM1. ClientSocket1Error (Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); begincaption: = 'connect' edit1.text 'fail'; showmessage (edit1.text # 13 # 10 'is not turned on or is not installed Server '); ErrorCode: = 0; End; Procedure TFORM1.ClientSocket1read (Sender: Tobject; Socket: Tcustomwinsocket); Varbuffer: Array [0..10000] of byte; // Setting the receiving buffer LEN: Integer; LL: string; b: tbitmap;

J: TJPEGIMAGE; Beginif C = 0 THEN / / C is the number of bytes sent by the server. If 0 is represented as a not started image reception beginll: = Socket.ReceText; c: = strtoint (ll); // Setting The received byte clientsocket1.socket.sendtext ('okok'); // Notifying the server Start sending image ELSEBEGIN / / The following is the image data receiving section LEN: = socket.recelength; // Read the package length SOCKET .ReceiveBuf (Buffer, Len); // Receive packets and read into m.write (buffer, len) in the buffer; // Additional flow M = c then // If the flow length is greater than receiving The number of bytes is received, and the received beginm.position: = 0; b: = tbitmap.create; j: = tjpegimage.create; tryj.loadfromstream (m); // Read data in the stream M to JPG image object J China B.Assign (j); // converts JPG to bmpimage1.picture.bitmap.assign (b); // Assignment to Image1 Components Finally // The following is to clear the work B.free; J.Free; ClientSocket1.active: = false; clientsocket1.Active: = true; m.Clear; c: = 0; end; end; end; end; procedure TForm1.FormCreate (Sender: TObject); beginm: = tmemorystream.Create; end; procedure TForm1.FormClose (Sender: TObject; var Action: TCloseAction); beginm.free; ClientSocket1.Close; end; end server program:. unit smain; interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ScktComp, jpeg; Typetform1 = Class (TFORM) ServerSocket1: TSERVERSOCKET; Procedure SE rverSocket1ClientRead (Sender: TObject; Socket: TCustomWinSocket); procedure FormCreate (Sender: TObject); private {Private declarations} public {Public declarations} end; varForm1: TForm1; m1: tmemorystream; implementation {$ R * .DFM} procedure TForm1. ServerSocket1ClientRead (Sender: TObject; Socket: TCustomWinSocket); vars, s1: string; desk: tcanvas; bitmap: tbitmap; jpg: tjpegimage; begins: = socket.ReceiveText; if s = 'gets' then // client issues a request beginbitmap : = tbitmap.create; jpg: = tjpegimage.create; desk: = tcanvas.create; // The following code is the current screen image Desk.Handle: = getDC (hwnd_desktop); m1: = tmemorystream.create; // Initialization Flow M1, after sending flow with SendStream (M1), // It will remain until the Socket dialog end, // cannot be removed by manual FREE,

转载请注明原文地址:https://www.9cbs.com/read-5144.html

New Post(0)