分享
 
 
 

一个消息提示托盘程序的开发历程(采用socket技术,附源代码)二---服务端源代码

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

消息提示工具源码:

服务端:

Msg.h

/*

* Create Date: 2004-12-01

* Create By: 李春雷

* purpose: 协议用自定义消息结构:

* MESSAGEINFO:任务消息

* MsgType消息头:0xA登陆成功,0xB登陆失败,0xC工作消息,0xD任务结束标识

* LoginInfo:登陆消息

*/

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

struct MESSAGEINFO{ //消息结构

int MsgType; //消息头:0xA登陆成功,0xB登陆失败,0xC工作消息,0xD任务结束标识

char MessageID[10]; //任务序号

char UsrCode[10]; //用户帐号

//char TastName[50]; //任务名称

char TastName[200]; //任务名称

char CreatTime[20]; //时间

};

struct LoginInfo{ //登陆信息

char pwd[20]; //用户密码

char userID[30]; //用户ID

};

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

Unit1.cpp

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

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

#include "Unit2.h"

#include "Msg.h"

#include <iniFiles.hpp>

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

String myProvider;//数据库连接字符

int Port; //端口;

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

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

IsServer=false;

Server="localhost";

}

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

void __fastcall TForm1::FormCreate(TObject *Sender)

{

//

char dir[MAX_PATH];

//----获取运行程序的目录开始

char *tfile = "/Config.ini";

//将程序现在目录所在输入此字符数组

::GetCurrentDirectory(MAX_PATH,dir);

//将现在目录加上指定文件名

strcat(dir,tfile);

//----获取运行程序的目录结束

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,"读取配置文件config.ini失败!","信息提示!",MB_OK+MB_ICONINFORMATION+MB_SYSTEMMODAL);

delete ini;

}

delete ini;

ADOConnection1->ConnectionString = myProvider;

ADOConnection1->LoginPrompt = false;

try

{

ADOConnection1->Open();

}

catch(...)

{

ShowMessage("无法连接数据库");

return;

}

btndisconnect->Enabled=false;

}

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

void __fastcall TForm1::btnlistenClick(TObject *Sender)

{

//程序运行后,如果用户按下"监听"钮,

//则将该程序设为服务器端,

//这时应将TServerSocket的Active属性设为True,

//使服务器自动进入监听状态。

ServerSocket1->Port = Port;

ServerSocket1->Active=true;

StatusBar1->SimpleText="正在监听...";

btnlisten->Enabled=false;

btndisconnect->Enabled=true;

}

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

void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,

TCustomWinSocket *Socket)

{

//当用户提出连接请求后,客户端会触发OnCreate事件,

StatusBar1->SimpleText="连接到:"+Server;

}

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

void __fastcall TForm1::ServerSocket1Accept(TObject *Sender,

TCustomWinSocket *Socket)

{

//在服务器接受了客户的请求后会触发OnAccept事件,

//在这个事件处理程序中将标志服务器端的变量IsServer设为True,

IsServer=true;

StatusBar1->SimpleText="连接到:"+Socket->RemoteAddress;

}

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

//---- 非阻塞传输方式,当其中的一方进行写操作时,

//另一方会触发OnRead事件(客户端)或OnClientRead事件(服务器端),

void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,

TCustomWinSocket *Socket)

{

LoginInfo *loginfo = new LoginInfo;

Socket->ReceiveBuf(loginfo,50);

String mySql = "select yhxh from xt_yh where yhbh='";

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<ServerSocket1->Socket->ActiveConnections;i++){

if(ServerSocket1->Socket->Connections[i]->RemoteAddress==Socket->RemoteAddress)

ServerSocket1->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="正在监听...";

}

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

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 ErrorEvent, int &ErrorCode)

{

//---- 此外在客户端还应该增加错误捕获机制,

//当用户输入无效的服务器名或服务器端没有

//处于监听状态时能够及时给用户反馈信息。

StatusBar1->SimpleText="无法连接到:"+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 序号,t.user_code as 责任人, t.message_title as 任务名称, to_char(t.create_time,'yyyy-mm-dd hh24:mi') as 时间, t.is_read as 是否阅读 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; //任务序号

String UsrCode; //用户帐号

String TastName; //任务名称

String CreatTime; //时间

while(!ADOQuery1->Eof){

MessageID=ADOQuery1->FieldByName("序号")->AsString;

UsrCode=ADOQuery1->FieldByName("责任人")->AsString;

TastName=ADOQuery1->FieldByName("任务名称")->AsString;

CreatTime=ADOQuery1->FieldByName("时间")->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,CreatTime.c_str(),CreatTime.Length()+1);

//对客户端发消息:

for(int i=0;i<ServerSocket1->Socket->ActiveConnections;i++){

//ServerSocket1->Socket->Connections[i]->SendBuf(Msg,94);

ServerSocket1->Socket->Connections[i]->SendBuf(Msg,244);

Sleep(1);

}

ADOQuery1->Next();

}

Msg->MsgType=0xD; //结束符号

for(int i=0;i<ServerSocket1->Socket->ActiveConnections;i++){

//ServerSocket1->Socket->Connections[i]->SendBuf(Msg,94);

ServerSocket1->Socket->Connections[i]->SendBuf(Msg,244);

Sleep(1); //停1ms很关键

}

delete Msg;

}else{

Timer1->Enabled=false;

}

}

catch(...){

Application->MessageBox("访问数据库错误!请重新启动程序。",NULL,MB_OK);

Application->Terminate();

}

}

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

void __fastcall TForm1::ServerSocket1ClientError(TObject *Sender,

TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)

{

ErrorCode=0;

}

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

void __fastcall TForm1::N2Click(TObject *Sender)

{

Application->Terminate();

}

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

void __fastcall TForm1::N3Click(TObject *Sender)

{

FormAbout->ShowModal();

}

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

Unit1.h

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

#ifndef Unit1H

#define Unit1H

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

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include <ComCtrls.hpp>

#include <ExtCtrls.hpp>

#include <ScktComp.hpp>

#include <ADODB.hpp>

#include <Db.hpp>

#include <DBGrids.hpp>

#include <Grids.hpp>

#include <Menus.hpp>

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

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;

TMenuItem *N3;

TMenuItem *N4;

TMenuItem *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; //服务器标识

String Server; //服务器IP

public: // User declarations

__fastcall TForm1(TComponent* Owner);

};

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

extern PACKAGE TForm1 *Form1;

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

#endif

Unit2.h

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

#include <vcl.h>

#pragma hdrstop

#include "Unit2.h"

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TFormAbout *FormAbout;

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

__fastcall TFormAbout::TFormAbout(TComponent* Owner)

: TForm(Owner)

{

}

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

void __fastcall TFormAbout::Button1Click(TObject *Sender)

{

ModalResult = mrOk;

}

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

Unit2.cpp

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

#include <vcl.h>

#pragma hdrstop

#include "Unit2.h"

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TFormAbout *FormAbout;

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

__fastcall TFormAbout::TFormAbout(TComponent* Owner)

: TForm(Owner)

{

}

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

void __fastcall TFormAbout::Button1Click(TObject *Sender)

{

ModalResult = mrOk;

}

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

以上是服务端socket程序

另外,服务端的配置文件Config.ini内容如下:

[servercfg]

Provider=Provider=OraOLEDB.Oracle.1;Password=pwsc;Persist Security Info=True;User ID=developer;Data Source=dl;Extended Properties=""

Port=1555

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有