//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ADOQuery1->Close(); //退出
ServerSocket1->Close();
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString clientAddr = Socket->RemoteAddress;//记录发来信息的客户机地址
AnsiString as = Socket->ReceiveText(); //从客户端获取Text
if(as == "exit")
{
Memo1->Lines->Add(Socket->RemoteHost+"退出");
return;
}
std::string str = as.c_str(); //将AnsiString转换为std::string
std::vector<std::string> v;
split(v, str, "+");
if(v[0]=="query") //如果标识位是 "query",表示客户机要求查询数据库
{
AnsiString result = "";
AnsiString sql = v[1].c_str(); //保存SQL语句主句
int unsigned i;
for(i=2; i<v.size();++i)
{
std::string ss = v[i];
AnsiString astr = ss.c_str(); //std::string转换为AnsiString
result += queryResuit(sql,astr);
}
int n;
for(n=0; n<ServerSocket1->Socket->ActiveConnections;n++)
{
AnsiString crrentAddr = ServerSocket1->Socket->Connections[n]->RemoteAddress;
if(crrentAddr == clientAddr)
ServerSocket1->Socket->Connections[n]->SendText(result);
}
}
}
//---------------------------------------------------------------------------
void TForm1::split(std::vector<std::string>& vec,const std::string& str,const std::string& spe)
{
std::string::size_type pos1,pos2;
pos1 = 0; //起始
pos2 = str.find(spe); //从0位置开始发现分隔符位置
while(pos2!=std::string::npos) //pos2到达最末位置跳出循环
{
vec.push_back(str.substr(pos1,pos2-pos1)); //截取从pos1到pos2之间的字符串
pos1 = pos2 + spe.size(); //更新pos ,向右移动
pos2 = str.find(spe,pos1); //从新的pos1位置开始找
}
if(pos1!=str.length())
vec.push_back(str.substr(pos1));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1Accept(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo1->Lines->Add("客户机"+Socket->RemoteHost+"加入");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnStartClick(TObject *Sender)
{
if(ServerSocket1->Active == true)
{
ServerSocket1->Active = false;
BtnStart->Caption = "开始";
StatusBar1->Panels->Items[0]->Text = "";
}
else
{
ServerSocket1->Active = true; //开启服务
StatusBar1->Panels->Items[0]->Text = "开始监听...";
BtnStart->Caption = "关闭";
}
}
//---------------------------------------------------------------------------
AnsiString TForm1::queryResuit(AnsiString sql, AnsiString field)
{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(sql);
ADOQuery1->Open(); //打开数据流
return ADOQuery1->FieldByName(field)->AsString; //将字段保存到变量
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Socket.rar (33个子文件)
ServerProject
Project1.obj 10KB
Unit1.obj 47KB
Project1.exe 825KB
Unit1.dfm 2KB
Unit1.~cpp 4KB
Project1.bpr 3KB
Unit1.~dfm 2KB
Project1.cpp 1KB
Project1.res 876B
Project1.~bpr 3KB
Unit1.h 2KB
Unit1.ddp 51B
Unit1.~ddp 51B
Project1.tds 960KB
Unit1.cpp 4KB
Unit1.~h 2KB
BataProject
Project1.obj 11KB
Project1.~cpp 2KB
Unit1.obj 23KB
Project1.exe 607KB
Unit1.dfm 2KB
Unit1.~cpp 3KB
Project1.bpr 3KB
Unit1.~dfm 2KB
Project1.cpp 2KB
Project1.res 876B
Project1.~bpr 3KB
Unit1.h 1KB
Unit1.ddp 51B
Unit1.~ddp 51B
Project1.tds 832KB
Unit1.cpp 3KB
Unit1.~h 1KB
共 33 条
- 1
资源评论
BCB_123_tcx
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功