A message prompts the development of the tray program (using Socket technology, attached code)

xiaoxiao2021-03-06  54

In my J2EE project, when a person's task arrives, we show this arrival task in a JSP page, prompting the user to handle the task. We are real-time monitoring of tasks by polling databases. Table USER_MESSAGE stores the user's task. Table structure is as follows: create table USER_MESSAGE (MESSAGE_ID NUMBER (9) not null, TASK_ID VARCHAR2 (100), USER_CODE VARCHAR2 (10), TASK_NAME VARCHAR2 (50), IS_READ VARCHAR2 (1), CREATE_TIME DATE, TASK_STATE VARCHAR2 (10), CREATE_MAN VARCHAR2 (10), Send_man varcha2 (10), pre_task_name varcha2 (50), serial_code number (11), Message_Title Varchar2 (200)) When IS_READ = 'f', a new task is a new task. This scenario has the following ills: 1: You must open the page to see the task; 2: Each client needs to poll, when the user has multiple database loads; then I have a auxiliary news prompt tool, when the task arrives When prompted, prompt information is issued in the system tray area, similar to QQ. The scheme is as follows: Use the socket technology to write clients, server program. The server is used as a message server that monitors the user_message table in real time in the way in which the database is polled every other time. Send the new task message to the client; the client identifies whether the message is "My" message, if it is "My message", then the system tray is flashed, the user can click the message title, click The arrow pattern under the message header can open the page to enter the J2EE system. Ok, now take this step by step by c builder to do this small Dongdong: The knowledge that needs to be prepared is as follows: 1: Adopt your own message communication protocol 2: Socket Program 3: How to implement system tray 4: a record in the database, How to pack it to the client? We solve one by one: 1: Message communication protocols MSG.H / * * Create Date: 2004-12-01 * Create by: Li Chunlei * Purpose: Agreement Custom Message Structure: * MessageInfo: Task News * MSGTYPE message header: 0xa login success, 0xB login failed, 0xc work message, 0xD task end identification * loginInfo: login message * /

/ / -------------------------------------------------------------------------------------------- ------------------------------

Struct MessageInfo {// message structure int msgtype; // message header: 0xA login success, 0xB login failed, 0xc work message, 0xD task end identifier char messageID [10]; // Task serial number char usrcode [10]; // User Account // Char Tastname [50]; // Task Name Char Tastname [200]; // Task Name Char Creattime [20]; // Time}; Struct LoginInfo {// Login Information Char PWD [20]; // User Password Char Userid [30]; // User ID}; // ----------------------------------- ------------------------------------------ The news sent by the server is only Is a structural messageInfo (confirmed whether to log in successful and task messages), the client only sends a loginfo to the server, other time just accepts the news from the server.

2: Socket programming If there is no Socket programming, it doesn't matter. Now you have learned: C Builder provides the Internet kit, where the TclientSocket and TServersocket components encapsulate the Windows related API, which greatly simplifies Winsock programming. To transfer data over Internet, at least a pair of Sockets, a socket on the client, another socket on the server. In fact, TClientSocket, TSERVERSOCKET components are not Socket objects, and its properties socket will return their respective socket objects. TclientSocket is used to handle the socket connection between the client to the server side. TSERVERSOCKET is used to process the socket connection from the client. Once the client and the server are connected to the socket, the client, and the server can communicate with each other. . We do a small example: ---- Establish a new project, create an application's user interface: ---- 1. Switch the component page to the Internet page, put a TSERVERSOCKET component and a TClientSocket component to the form, so The application can be either a TCP / IP server or a TCP / IP client. Set the port attribute to the same value (such as 1000), determine the connection type between the socket is Nonblocking. ---- 2. Put two TMEMO components to the form to display both parties' conversation, set the memo2's ReadOnly property to True. ---- 3. Put a panel component at the top of the form, putting three buttons on it: Listening (BTNConnect), disconnected (btndisconnect), is used to initiate the corresponding operation. ---- 4. Place a STATUSBAR component at the bottom of the form, set its SimplePanel property to true, change the status strip information in the corresponding event handler, so that the user knows the connection status at any time. ---- Open the header file, add two private members to the private member: BOOL ISSERVER; STRING Server. When both parties are in communication, ISSERVER is used to determine which CHAT program is in the server side, Server is used to store the host name of the server. The constructor for establishing the form is as follows: __fastcall tform1 :: tForm1 (tComponent * Owner): TFORM (OWNER) {isserver = false; server = "localhost";} ---- SERVER here Server is set to localhost, so The program can debug on a single machine that is not connected to the Internet. In the Windows subdirectory you can find the hosts.sam file, you have defined the host name: localhost in this file: Localhost. Void __fastcall tform1 :: formCall (TOBJECT * Sender) {btndisconnect-> enabled = false;} - After the program is running, if the user presses the "listening" button, set the program to the server side, then it should The Active property of the TSERVERSOCKET is set to TRUE, allowing the server to automatically enter the listening state.

void __fastcall TForm1 :: btnlistenClick (TObject * Sender) {ClientSocket1-> Active = false; ServerSocket1-> Active = true; StatusBar1-> SimpleText = "listening ..."; btnlisten-> Enabled = false; btnconnect-> Enabled = false;} ---- When the user presses the "Connection" button, the program pops up, requiring the user to enter the host name of the server to connect, and then establish a connection. Void __fastcall tform1 :: btnConnectClick (IF ("Connect to Server", Enter Server Address: (IF) {if (Server.Length ()> 0) {ClientSocket1-> Host = Server ClientSocket1-> Active = true; btnlisten-> enabled = false; btnconnect-> enabled = false; btndisconnect-> enabled = true;}}}

---- After the user proposes the connection request, the client triggers an oncreate event, the program first displays the connection information in the status bar, and then the Memo2 empties to the other party's conversation content is ready to start talking. Void __fastcall tform1 :: clientsocket1connect (Tobject * sender, tcustomwinsocket * socket) {statusbar1-> simpletext = "Connect to:" server; memo2-> lines-> clear ();} ---- In the server accepts customers The onaccept event will be triggered after the request, and the variable ISSERVER of the flag server side is set to TRUE in this event handler, and is ready to start talk. void __fastcall TForm1 :: ServerSocket1Accept (TObject * Sender, TCustomWinSocket * Socket) {Memo2-> Lines-> Clear (); IsServer = true; StatusBar1-> SimpleText = "connection to:" Socket-> RemoteAddress;} --- - After establishing a connection, the two sides can enter the conversation content in MEMO1 to start talking. After pressing the Enter key, the text you are sending out. The Connections property of the server-side Socket returns an array that consists of a server currently active. void __fastcall TForm1 :: Memo1KeyDown (TObject * Sender, WORD & Key, TShiftState Shift) {if (Key == VK_RETURN) {if (IsServer) ServerSocket1-> Socket-> Connections [0] -> SendText (Memo1-> Lines-> Strings [Memo1-> Lines-> count-1]); else clientsocket1-> socket-> sendtext (memo1-> lines-> strings [memo1-> lines-> count-1]);}} - In this example, we use a non-blocking transmission mode, and the other party triggers an OnRead event (client) or an onclientRead event (server side), and the handler of these two events is only received. Add to MEMO2. Memo2-> lines-> add (socket-> receivetext ());

---- If you click the "Dispenser" after the user is established, disconnect the client and the server, the server side will trigger the onclientdisconnect event, and the client will trigger the onDisconnect event, then the server should be back. To the listening state, wait for the user's connection; and the client will return to the state before the connection, wait for the user to establish a connection again, if there is more than one server, you can choose to connect to other servers. void __fastcall TForm1 :: btndisconnectClick (TObject * Sender) {ClientSocket1-> Close ();} void __fastcall TForm1 :: ServerSocket1ClientDisconnect (TObject * Sender, TCustomWinSocket * Socket) {StatusBar1-> SimpleText = "listening ...";} void __fastcall TForm1 :: ClientSocket1Disconnect (TObject * Sender, TCustomWinSocket * Socket) {btnlisten-> Enabled = true; btnconnect-> Enabled = true; btndisconnect-> Enabled = false; StatusBar1-> SimpleText = "";} ---- In addition, the client should also increase the error capture mechanism, which can feedback information in time when the user enters invalid server name or server terminal is not in the listening state. void __fastcall TForm1 :: ClientSocket1Error (TObject * Sender, TCustomWinSocket * Socket, TErrorEvent ErrorEvent, int & ErrorCode) {StatusBar1-> SimpleText = "Could not connect to:" Socket-> RemoteHost; ErrorCode = 0;} After the above step is completed, socket You can run. Is it difficulty in imagination?

Three: About the tray, BCB gives a simple implementation method: Let us compile a simple Tary program: 1. New project, add a TRAYICON component, a popupMenu component, and an ImageList component. Their Name attribute with the default name: TrayIcon1, PopupMenu1, ImageList1.2, set TrayIcon1's properties as follows: Property Value Animate true AnimateInterva 1000 Hide true Hint Tary demo IconIndex 0 Icons ImageList1 Name TrayIcon1 PopupMenu PopupMenu1 PopupMenuOn imRightClickUp RestoreOn imDoubleClick Visible TRUE 3, double-click PopupMenu1, pop up the menu designer, add several menu items to join. 4. Double-click ImageList1 to join the supported pictures (* .ico, *. Bmp). At this point, you don't have to write a program code, a simple tary program is done. Press F9 to compile operation, move the mouse to Tary will appear prompt information; click the mouse button on Tary Popmenu1; press the minimization button of the program window, the program minimizes the hidden taskbar The title bar; Double-click Tary will resume the program minimize; and the Tary icon transforms at a speed of 1000 milliseconds (1 second). Is it simple enough? ! 4: A record in the database, how to pack it to the client? We put a record in the database, placed in a structure, and then the structure SEND is out. Here is a simple example: .hstruct tusefo {int No; // User Group Char usrid [20]; // User account};

.cppvoid __fastcall tster1 :: Button1click (Tobject * sender) {tusefo * tst = new tusefo [2]; TST [0] .NO = 1; TST [1] .no = 2; string tmp = "Normal"; MEMCPY TST [0] .usrid, tmp.c_str (), tmp.Length () 1); TMP = "abnormal"; Memcpy (TST [1] .usrid, tmp.c_str (), tmp.length () 1); ClientSocket1-> Socket-> sendBuf (tst, 24); delete tst;} void __fastcall TForm1 :: ServerSocket1ClientRead (TObject * Sender, TCustomWinSocket * Socket) {TUSERINFO * tst = new TUSERINFO [2]; Socket-> receiveBuf (TST, 24); ShowMessage (TST [0] .usrid); showMessage (TST [1] .usrid);} The above example is simple, you can implement Struct transmission, but there is a small problem: if you want to pass 2 Record, must sendBUF (TST, 24 * 2); then acceptable times must receiveBUF (TST, 24 * 2); and the number of roses is uncertain, how much is it, what is the number, customers How much is it? In fact, it is also simple to solve: I only send a struct every time, I can use for loop. Through the technical preparation of the above one two three four, the specific implementation of the message prompt tool is very clear, just the problem of workload. In the message prompt tool, I use ADO to access the database. In addition, in order to go to the customer, it is convenient, I use the configuration file, the program first read the configuration file, and then run.

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

New Post(0)