下面给出客户端程序,客户端相对复杂些,需要首先登陆,把登陆信息传给服务端,等待服务端的验证,如果验证通过,才
进入任务消息接受状态:
登陆:
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);