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

xiaoxiao2021-03-06  42

Message Tips Source: Server: Msg.h / * * Create Date: 2004-12-01 * Create Date: 2004-12-01 * Create Date: 2004-12-01 * Create by: Li Chunlei * PURPOSE: Agreement Custom Message Structure: * MessageInfo: Task Message * MSGTYPE Message Head: 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 Creatime [20]; // Time};

Struct loginInfo {// login information char PWD [20]; // User password char userid [30]; // user ID}; // ------------------- -------------------------------------------------- ---------

Unit1.cpp // --------------------------------------------- ------------------------------

#include #pragma hdrstop # include "unit1.h" #include "unit2.h" #include "msg.h" #include // ---------- -------------------------------------------------- --------------- # pragma package (smart_init) #pragma resource "* .dfm" TFORM1 * FORM1; String myProvider; // Database connection character int port; // port; // -------------------------------------------------- -------------------------__ fastcall tform1 :: tform1 (tcomponent * oowner): tform (ooWer) {isserver = false; server = "localhost" } // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----------------------------

Void __fastcall tform1 :: formcreate (TOBJECT * Sender) {// char dir [max_path]; // ---- Get the runtime directory start char * tfile = "/config.ini";// will now Enter this character number :: getCurrentDirectory (max_path, dir); // Plus the current directory with the specified file name STRCAT (DIR, TFILE); // ---- Get the directory of the running program Tinifile * ini = new Tinifile (DIR) ); try {myProvider = ini-> ReadString ( "servercfg", "Provider", "Provider = OraOLEDB.Oracle.1; Password = pwsc; Persist Security Info = True; User ID = developer; Data Source = dl; Extended Properties = "" "); Port = INI-> Readinteger (" Servercfg "," Port ", 1555);} catch (...) {messagebox (Application-> Handle," Read the configuration file config.ini failed! " , "information presentation!", MB_OK MB_ICONINFORMATION MB_SYSTEMMODAL); delete ini;} delete ini; ADOConnection1-> ConnectionString = myProvider; ADOConnection1-> LoginPrompt = false; try {ADOConnection1-> Open ();} catch (... {ShowMessage ("Unable to connect to the database"); return;} btndisconnect-> enabled = false;} // -------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------

Void __fastcall tform1 :: btnlistenclick (TOBJECT * sender) {// After running, if the user presses the "listening" button, then set the program to the server side, // At this time, TSERVERSOCKET should be set to True, // Enables the server to automatically enter the listening status. Serversocket1-> port = port; serversocket1-> Active = true; statusbar1-> simpletext = "Listening ..."; btnlisten-> enabled = false; btndisconnect-> enabled = true;

} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Void __fastcall tform1 :: clientsocket1connect (TOBJECT * SENDER, TCUSTOMWINSOCKET * SOCKET) {// When the user is connected After the request, the client triggers an oncreate event, Statusbar1-> SimpleText = "Connect to:" Server;} // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- -void __fastcall1accept (TOBJECT * SENDER, TCUSTOMWINSOCKET * SOCKET) {// The onaccept event is triggered after the server accepts the customer's request, // sets the logo server-side variable isserver in this event handler to true, ISSERVER = true; statusbar1-> simpletext = "Connect to:" Socket-> RemoteaddRESS;

} // ----------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------- // ---- Non-blocking transmission mode, when one of them is written, // OnRead other trigger event (client) or OnClientRead event (server side), void __fastcall TForm1 :: ServerSocket1ClientRead (TObject * Sender, TCustomWinSocket * Socket) {LoginInfo * loginfo = new LoginInfo; Socket-> receiveBuf (loginfo, 50) String mysql = "SELECT YHXHWHX = '"; mysql = loginfo-> userid; mysql = "' and pwd = '"; mysql = loginfo-> PWD; mysql = "'"; adoquery2-> close AdoQuery2-> Connection = adoconnection1; adoquery2-> sql-> clear (); adoQuery2-> sql-> add (mysql); adoquery2-> open (); int Num = adoquery2-> recordcount; messageInfo * loginmsg = new MessageInfo; INT msgtype; if (num> 0) {msgtype = 0xa; // ok loginmsg-> msgtype = msgtype;} else {msgtype = 0XB; // nobody loginmsg-> msgtype = msgtype;} for (int i = 0 i socket-> activeConnections; i ) {if (ServerSocket1-> Socket-> Connections [i] -> remoteaddress == Socket-> remoteaddress) Servers Ocket1-> Socket-> Connections [i] -> Sendbuf (LoginMSG, 244);} adoquery2-> close ()

} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- void __fastcall tform1 :: btndisconnectclick (tobject * sender) {serversocket1-> close (); btnlisten-> Enabled = true; btndisconnect-> enabled = false;} // ------------------------------------ --------------------------------------- Void __fastcall tform1 :: serversocket1clientdisconnect (TOBJECT * SENDER, Tcustomwinsocket * Socket) {statusbar1-> simpletext = "Listening ...";} //--------------------------- --------------------------------------------- void __fastcall tform1: : ClientSocket1disconnect (Tobject * Sender, tcustomwinsocket * socket) {btnlisten-> enabled = true; btndisconnect-> enabled = false; statusbar1-> simpletext = "";} // ------------- -------------------------------------------------- ------------ Void __fastcall tform1 :: clientsocket1error (Tobject * Sender, tcustomwinsocket * socket, terrorevent erroorEvent, int & errorcode) {// ---- Also add error capture mechanism should also be added to the client // When the user input is invalid, it is possible to give user feedback information in time when the server is not // in the listening state. Statusbar1-> SimpleText = "Unable to connect to:" Socket-> Remotehost; ErrorCode = 0;} // -------------------------------------------------------------------------------------------------------------------- -------------------------------------------------

void __fastcall TForm1 :: Timer1Timer (TObject * Sender) {try {if (ADOConnection1-> Connected) {String strmessage = ""; ADOQuery1-> Close (); ADOQuery1-> Connection = ADOConnection1; ADOQuery1-> SQL-> Clear ( AdoQuery1-> SQL-> add ("SELECT T.MESSAGE_ID AS serial number, T.user_code as responsible person, T.Message_title AS task name, to_char (t.create_time, 'YYYY-MM-DD HH24: MI') AS Time, t.is_read as reading from user_message t where t.is_read = 'f' and t.task_state = 'assign' order by t.Message_ID "); adoquery1-> open (); messageinfo * msg = new messageinfo;

String messageID; // Task Sequence String USRcode; // User Account String Tastname; // Task Name String Creattime; // Time While (! AdoQuery1-> EOF) {messageId = adoquery1-> FieldByname -> asstring USRCODE = AdoQuery1-> FIELDBYNAME ("responsible person") -> asstring; tastname = adoQuery1-> FieldByname ("task name") -> asstring; creattime = adoquery1-> FieldByname ("time") -> asstring; msg- > Msgtype = 0xc; memcpy (msg-> messageid, messageid.c_str (), messageid.Length () 1); memcpy (msg-> usrcode, usrcode.c_str (), usrcode.length () 1); memcpy (Msg-> tastname, tastname.c_str (), tastname.length () 1); memcpy (msg-> creattime, creatime.c_str (), creattime.length () 1); // Duty to the client : For (int i = 0; i socket-> activeconnections; i ) {// serversocket1-> Socket-> Connections [i] -> sendbuf (MSG, 94); Serversocket1-> Socket-> Connections [ I] -> Sendbuf (MSG, 244); Sleep (1);} adoquery1-> next ();} msg-> msgtype = 0xD; // End symbol for (int i = 0; i Socket-> ActiveConnections; i ) {// serversocket1-> Socket-> Connections [i] -> Sendbuf (MSG, 94); Serversocket1-> Socket-> Connections [i] -> Sendbuf (MSG, 244) Sleep (1); // Stop 1ms is critical} delete msg;} else {Timer1-> enabled = false;}} catch (...) {Application-> MessageBox ("Access Database Error! Please restart the program. ", NULL, MB_OK); Application-> Terminate ();}} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------

Void __fastcall tform1 :: serversocket1clientr (Tobject * sender, tcustomwinsocket * socket, terrorEvent errorevent, int & errorcode) {erroorcode = 0;} // --------------------- -------------------------------------------------- ---- Void __fastcall tform1 :: n2click (TOBJECT * Sender) {Application-> Terminate ();} // ---------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -

Void __fastcall tform1 :: n3click (TOBJECT * Sender) {formabout-> showmodal ();} // -------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------

Unit1.h

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

#ifndef unit1H # Define unit1H // ------------------------------------------------------------------------------------------------------------------------------------ --------------------------------- # include #include #include < STDCTRLS.HPP> #include #include #include #include #include #include #include #include #include // ----------------------------- ------------------------------------------ Class TFORM1: PUBLIC TFORM { __published: // IDE-managed Components TServerSocket * ServerSocket1; TPanel * Panel1; TButton * btnlisten; TButton * btndisconnect; TStatusBar * StatusBar1; TADOConnection * ADOConnection1; TADOQuery * ADOQuery1; TDBGrid * DBGrid1; TDataSource * DataSource1; TTimer * Timer1; TADOQuery * AdoQuery2; TGroupBox * GroupBox1; TMAINMENU * MainMenu1; TMenuItem * N1; TMENUITEM * N2; TMENUIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIITEM * N5; TMenuItem * N6; void __fastcall FormCreate (TObject * Sender); void __fastcall btnlistenClick (TObject * Sender); void __fastcall ClientSocket1Connect (TObject * Sender, TCustomWinSocket * Socket); void __fastcall ServerSocket1Accept (TObject * Sender, TCustomWinSocket * Socket); void __fastcall ServerSocket1ClientRead (TObject * Sender, TCustomWinSocket * Socket); void __fastcall btndisconnectClick (TObject * Sender); void __fastcall ServerSocket1ClientDisconnect (TObject * Sender, TCustomWinSocket * Socket);

void __fastcall ClientSocket1Disconnect (TObject * Sender, TCustomWinSocket * Socket); void __fastcall ClientSocket1Error (TObject * Sender, TCustomWinSocket * Socket, TErrorEvent ErrorEvent, int & ErrorCode); void __fastcall Timer1Timer (TObject * Sender); void __fastcall ServerSocket1ClientError (TObject * Sender, TCustomWinSocket * Socket, TErrorEvent ErrorEvent, int & ErrorCode); void __fastcall N2Click (TObject * Sender); void __fastcall N3Click (TObject * Sender); private: // User declarations bool IsServer; // server identifier String server; // server IPpublic: / / User declarations __fastcall tform1 (tComponent * Owner);}; // ----------------------------------- --------------------------------------- EXTERN PACKAGE TFORM1 * FORM1; // - -------------------------------------------------- ----------------------- # endifUnit2.h // -------------------- -------------------------------------------------- ----

#include #pragma HDRSTOP

#include "unit2.h" // --------------------------------------- ---------------------------------- # Pragma package (smart_init) #pragma resource "* .dfm" TFORMABOUT * FormaBout; // ---------------------------------------------- -----------------------------__ fastcall tformat :: tFormaBout (tComponent * Owner): TFORM (OWNER) {} // - -------------------------------------------------- ----------------------- void __fastcall tformast :: Button1click (TOBJECT * Sender) {modalResult = mrok;} // -------- -------------------------------------------------- -----------------

Unit2.cpp // --------------------------------------------- ------------------------------

#include #pragma HDRSTOP

#include "unit2.h" // --------------------------------------- ---------------------------------- # Pragma package (smart_init) #pragma resource "* .dfm" TFORMABOUT * FormaBout; // ---------------------------------------------- -----------------------------__ fastcall tformat :: tFormaBout (tComponent * Owner): TFORM (OWNER) {} // - -------------------------------------------------- ----------------------- void __fastcall tformast :: Button1click (TOBJECT * Sender) {modalResult = mrok;} // -------- -------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------- PWSC; persist security info = true; user ID = weveloper; data source = DL; extended proties = "" Port = 1555

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

New Post(0)