下面给出客户端程序,客户端相对复杂些,需要首先登陆,把登陆信息传给服务端,等待服务端的验证,如果验证通过,才
进入任务消息接受状态:
登陆:
Unit2.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include "Msg.h"
#include <iniFiles.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TLoginForm *LoginForm;
String userName;
String serverIP;
String misurl;
int port;
//---------------------------------------------------------------------------
__fastcall TLoginForm::TLoginForm(TComponent* Owner)
: TForm(Owner)
{
isPass=false;
}
//---------------------------------------------------------------------------
void __fastcall TLoginForm::Button1Click(TObject *Sender)
{
if(EditName->Text.Trim().Length()>=30){
Application->MessageBox("用户名过长,请重新输入.",NULL,MB_OK);
}
else if(EditPwd->Text.Trim().Length()>=20){
Application->MessageBox("密码过过长,请重新输入.",NULL,MB_OK);
}
else if(inet_addr(EditServer->Text.c_str())==0xffffffff){
Application->MessageBox("IP地址设置有错,请重新设置.",NULL,MB_OK);
}
else{
ClientSocket1->Active=false;
ClientSocket1->Host=EditServer->Text;
ClientSocket1->Port=port;
ClientSocket1->Active=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TLoginForm::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
Application->MessageBox("无法连接到服务器,请确认服务端程序是否开启.",NULL,MB_OK);
ErrorCode=0;
}
//---------------------------------------------------------------------------
void __fastcall TLoginForm::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
userName = EditName->Text.Trim();
serverIP = EditServer->Text.Trim();
LoginInfo *loginfo = new LoginInfo;
memcpy(loginfo->userID,userName.c_str(),userName.Length()+1);
memcpy(loginfo->pwd,EditPwd->Text.Trim().c_str(),EditPwd->Text.Trim().Length()+1);
ClientSocket1->Socket->SendBuf(loginfo,50);
}
//---------------------------------------------------------------------------
void __fastcall TLoginForm::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
MESSAGEINFO *Msg = new MESSAGEINFO;
Socket->ReceiveBuf(Msg,244);
if(Msg->MsgType==0xA){
isPass=true;
//写如INI
char dir[MAX_PATH];
char *tfile = "/Config.ini";
//将程序现在目录所在输入此字符数组
::GetCurrentDirectory(MAX_PATH,dir);
//将现在目录加上指定文件名
strcat(dir,tfile);
TIniFile *ini = new TIniFile(dir);
try{
ini->WriteString("clientcfg","server",serverIP);
ini->WriteString("clientcfg","user",userName);
delete ini;
}
catch(...){
Application->MessageBoxA("写入配置文件config.ini失败!",NULL,MB_OK+16);
delete ini;
}
Close();
}
if(Msg->MsgType==0xB){
Application->MessageBox("用户名或密码错误!",NULL,MB_OK);
}
}
//---------------------------------------------------------------------------
void __fastcall TLoginForm::FormClose(TObject *Sender,
TCloseAction &Action)
{
ClientSocket1->Active=false;
}
//---------------------------------------------------------------------------
bool __fastcall TLoginForm::GetisPass()
{
//TODO: Add your source code here
return isPass;
}
void __fastcall TLoginForm::FormCreate(TObject *Sender)
{
char dir[MAX_PATH];
//----获取运行程序的目录开始
char *tfile = "/Config.ini";
//将程序现在目录所在输入此字符数组
::GetCurrentDirectory(MAX_PATH,dir);
//将现在目录加上指定文件名
strcat(dir,tfile);
//----获取运行程序的目录结束
TIniFile *ini = new TIniFile(dir);
try
{
EditName->Text = ini->ReadString("clientcfg","user","");
EditServer->Text = ini->ReadString("clientcfg","server","");
port = ini->ReadInteger("clientcfg","port",1555);
misurl = ini->ReadString("clientcfg","misurl","http://192.168.1.101:7001/webmis/Login/loginController.jpf");
delete ini;
}
catch(...)
{
MessageBox(Application->Handle,"读取配置文件config.ini失败!","信息提示!",MB_OK+MB_ICONINFORMATION+MB_SYSTEMMODAL);
delete ini;
}
}
//---------------------------------------------------------------------------
void __fastcall TLoginForm::EditServerDblClick(TObject *Sender)
{
EditServer->ReadOnly = false;
EditServer->Font->Color = clBlack;
}
//---------------------------------------------------------------------------
Unit2.h
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ScktComp.hpp>
#include <Mask.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TLoginForm : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TGroupBox *GroupBox1;
TLabel *Label1;
TLabel *Label2;
TEdit *EditName;
TEdit *EditPwd;
TLabel *Label3;
TEdit *EditServer;
TClientSocket *ClientSocket1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent,
int &ErrorCode);
void __fastcall ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall FormCreate(TObject *Sender);
void __fastcall EditServerDblClick(TObject *Sender);
private:
bool isPass;
public:
bool __fastcall GetisPass(); // User declarations
__fastcall TLoginForm(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TLoginForm *LoginForm;
extern String userName; //用户
extern String serverIP;
extern int port;
extern String misurl;
//---------------------------------------------------------------------------
#endif
登陆成功后,进入任务监听:
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
#include "Msg.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Trayicon"
#pragma resource "*.dfm"
TForm1 *Form1;
TList *MsgList = new TList;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
isCall=false;
if(serverIP.Length()>0){
Server=serverIP;
ClientSocket1->Host=Server;
ClientSocket1->Port=port;
ClientSocket1->Active=true;
btnconnect->Enabled=false;
btndisconnect->Enabled=true;
}else{
Application->Terminate();
}
StringGrid1->DefaultColWidth=50;
StringGrid1->ColWidths[2]=600;
StringGrid1->ColWidths[3]=100;
GroupBox1->Caption=userName+"的任务列表";
StringGrid1->Cells[0][0]="消息ID";
StringGrid1->Cells[1][0]="负责人";
StringGrid1->Cells[2][0]="任务名称";
StringGrid1->Cells[3][0]="时间";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N2Click(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrayIcon1Click(TObject *Sender)
{
if(isCall==true){
isCall=false;
TrayIcon1->IconIndex=0;
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
if((isCall==true)){
TrayIcon1->IconIndex=(GetTickCount()/500)%2;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnconnectClick(TObject *Sender)
{
if(InputQuery("连接到服务器","输入服务器地址:",serverIP)){
if(inet_addr(serverIP.c_str())==0xffffffff){
Application->MessageBox("IP地址设置有错,请重新设置.",NULL,MB_OK);
}
else{
ClientSocket1->Host=serverIP;
ClientSocket1->Active=true;
btnconnect->Enabled=false;
btndisconnect->Enabled=true;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText="连接到:"+Server;
TrayIcon1->IconIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
//接受服务器端传来的MESSAGE;
MESSAGEINFO *Msg = new MESSAGEINFO;
Socket->ReceiveBuf(Msg,244);
if((Msg->MsgType==0xC)&&((String)Msg->UsrCode==userName)){
//bool isAdd=true;
MsgList->Add(Msg);
/*
for(int i=1;i<=StringGrid1->RowCount;i++){
if(StringGrid1->Cells[0][i]==(String)Msg->MessageID){
isAdd=false;
}
}
if(isAdd==true){
isCall=true;
}
*/
}
if(Msg->MsgType==0xD){ //一次轮询结束
//Label1->Caption = MsgList->Count+1;
if((MsgList->Count+1)>StringGrid1->RowCount) isCall = true;
if((MsgList->Count+1)!=StringGrid1->RowCount){
StringGrid1->RowCount=0;
for (int i = 0; i < MsgList->Count; i++) {
int rowcount=StringGrid1->RowCount;
StringGrid1->Cells[0][rowcount]=((MESSAGEINFO *)MsgList->Items[i])->MessageID;
StringGrid1->Cells[1][rowcount]=((MESSAGEINFO *)MsgList->Items[i])->UsrCode;
StringGrid1->Cells[2][rowcount]=((MESSAGEINFO *)MsgList->Items[i])->TastName;
StringGrid1->Cells[3][rowcount]=((MESSAGEINFO *)MsgList->Items[i])->CreatTime;
StringGrid1->RowCount++;
}
}
MsgList=NULL;
MsgList = new TList;
Label1->Caption = "您的任务数:"+IntToStr(StringGrid1->RowCount-1);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
btnconnect->Enabled=true;
btndisconnect->Enabled=false;
StatusBar1->SimpleText="无法连接到服务端!";
TrayIcon1->IconIndex = 2;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btndisconnectClick(TObject *Sender)
{
ClientSocket1->Close();
btnconnect->Enabled=true;
btndisconnect->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
StatusBar1->SimpleText="无法连接到:"+serverIP;
ErrorCode=0;
btndisconnect->Enabled=true;
btnconnect->Enabled=false;
}
//---------------------------------------------------------------------------
//判断某人的任务数
TForm1::StrCount(String substing, String SourceStr)
{
//TODO: Add your source code here
int sp = substing.Length();
int pos = SourceStr.Pos(substing);
String tmp;
int capa=0;
while( pos > 0 )
{
tmp=SourceStr.SubString(1,pos-1);
capa++;
SourceStr=SourceStr.SubString(pos+sp,SourceStr.Length()-pos-sp+1);
pos=SourceStr.Pos(substing);
}
return capa;
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
ClientSocket1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClick(TObject *Sender)
{
delete MsgList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N3Click(TObject *Sender)
{
N3->Enabled=false;
FormAbout->ShowModal();
N3->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1Click(TObject *Sender)
{
ShellExecute(Handle,NULL,misurl.c_str(),NULL,NULL,SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
if(!TrayIcon1->Visible)
TrayIcon1->Visible=true;
TrayIcon1->Minimize();
}
//---------------------------------------------------------------------------
Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "Trayicon.h"
#include <ImgList.hpp>
#include <Menus.hpp>
#include <ADODB.hpp>
#include <Db.hpp>
#include <DBGrids.hpp>
#include <ExtCtrls.hpp>
#include <Grids.hpp>
#include <ScktComp.hpp>
#include <ComCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TTrayIcon *TrayIcon1;
TPopupMenu *PopupMenu1;
TMenuItem *N2;
TMenuItem *N3;
TImageList *ImageList1;
TTimer *Timer2;
TGroupBox *GroupBox1;
TClientSocket *ClientSocket1;
TButton *btnconnect;
TButton *btndisconnect;
TStatusBar *StatusBar1;
TStringGrid *StringGrid1;
TImage *Image1;
TLabel *Label1;
void __fastcall N2Click(TObject *Sender);
void __fastcall TrayIcon1Click(TObject *Sender);
void __fastcall Timer2Timer(TObject *Sender);
void __fastcall btnconnectClick(TObject *Sender);
void __fastcall ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall ClientSocket1Disconnect(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall btndisconnectClick(TObject *Sender);
void __fastcall ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent,
int &ErrorCode);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall FormClick(TObject *Sender);
void __fastcall N3Click(TObject *Sender);
void __fastcall Image1Click(TObject *Sender);
void __fastcall FormShow(TObject *Sender);
private: // User declarations
bool isCall;
int capa; //任务数
String Server;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
StrCount(String substing, String SourceStr);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
然后是about界面:
Unit3.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit3.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;
}
//---------------------------------------------------------------------------
Unit3.h
//---------------------------------------------------------------------------
#ifndef Unit3H
#define Unit3H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TFormAbout : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TImage *Image1;
TLabel *Label2;
TLabel *Label3;
TLabel *Label1;
TLabel *Label4;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TFormAbout(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormAbout *FormAbout;
//---------------------------------------------------------------------------
#endif
另外,登陆界面和任务监听主界面如何出现,需要在project中做如下修改(模式对话框)
project2.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#include "Unit2.h"
#pragma hdrstop
USERES("Project2.res");
USEFORM("Unit1.cpp", Form1);
USEFORM("Unit2.cpp", LoginForm);
USEFORM("Unit3.cpp", FormAbout);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
TLoginForm *f = new TLoginForm(Application);
f->ShowModal();
if(!f->GetisPass()){
delete f;
return 0;
}
Application->CreateForm(__classid(TForm1), &Form1);
// Application->CreateForm(__classid(TLoginForm), &LoginForm);
Application->CreateForm(__classid(TFormAbout), &FormAbout);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
//---------------------------------------------------------------------------
另外,客户端的配置文件Config.ini内容如下:
[clientcfg]
user=yinhm
server=192.168.1.55
port=1555
misurl=http://192.168.1.101:7001/webmis/Login/loginController.jpf
以上程序在bcb5编译通过。
以上的东东一是给自己存个档,二是希望对各位有所帮助,并希望牛人批评指正。
马上就要过年了,顺祝大家新年快乐,幸福吉祥