ПЗ (1229304), страница 12
Текст из файла (страница 12)
{
if (TcpServer1->Active)
TcpServer1->Active = false;
if (TcpClient1->Active)
TcpClient1->Active = false;
delete SList,SList2,Memo;
Sleep(300); // Даем время для закрытия сервера, у клиента.
}
//---------------------------------------------------------------------------
// Accept - для авторизации клиентов
void __fastcall TForm1::TcpServer1Accept(TObject *Sender, TCustomIpClient *ClientSocket)
{
UnicodeString msg="";
ReadUSFromBuf(ClientSocket,&msg);
if (MsgBlockServ(msg)) return;
UnicodeString kod="",my_name="",port="";
kod = Read_From_Text(msg,"kod");
my_name = Read_From_Text(msg,"my_name");
port = Read_From_Text(msg,"port");
if (kod == "55556") // сообщаем клиенту о начале симуляции
{
Show_Main_Form = true;
Timer1->Interval = 300;
Timer1->Enabled = true;
TcpServer1->OnAccept = TcpServer1Accept2;
return;
}
if ((kod == "55555") && (my_name == Name_of_client))
{
Form1->Caption = "Пользователь " + Name_of_client;
TcpServer1->Active=false;
TcpServer1->LocalPort = port;
TcpServer1->Active=true;
// разблокируем кнопку для отправки данных на сервер
Form1->BSendServ->Enabled = true;
Form2->Caption = "Ждите...";
// блокируем кнопку подключения
Form2->BConnect->Enabled = false;
}
}
//---------------------------------------------------------------------------
// Accept2 - Принятие отчетов от сервера
void __fastcall TForm1::TcpServer1Accept2(TObject *Sender, TCustomIpClient *ClientSocket)
{
UnicodeString s;
UnicodeString s2;
ReadUSFromBuf(ClientSocket, &s);
if (MsgBlockServ(s))
{
Form1->TcpServer1->Active = false;
return;
}
Receive_Inf(s);
NullFields();
Timer1->Enabled = true; // включаем таймер для отчета времени
if (s.SubString(1,5) == "Отчет")
{
ListBox1->Items->Add(s.SubString(1,5) + " " +
StrToInt(ListBox1->Items->Count));
s.Delete(1,7);
s2 = Form_Report(s);
SList->Add(s2);
SList2->Add(s);
Plot();
// разблокировали кнопку
BSendServ->Enabled = true;
}
else // то есть начальные данные
{
ListBox1->Items->Add("Начальные данные");
s = Form_Report(s);
SList->Add(s); SList2->Add(s);
MParamServ->Lines->Add("Начальные данные");
MParamServ->Lines->Add(Begin_Date(s));
}
};
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString s; UnicodeString s2;
randomize();
s =
"My_Name Tst1 \r\n"
"Number_Client 1 \r\n"
"cur_cash " + IntToStr(random(100)) + " \r\n"
"inv " + IntToStr(random(100)) + " \r\n"
"pow_cur_lev " + IntToStr(random(100)) + " \r\n"
"pow_inc_price " + IntToStr(random(100)) + " \r\n"
"SRW_level " + IntToStr(random(100)) + " \r\n"
"SRW_inc_price " + IntToStr(random(100)) + " \r\n"
"inv_SRW " + IntToStr(random(100)) + " \r\n"
"SRW_start_inc_price " + IntToStr(random(100)) + " \r\n"
"marketing " + IntToStr(random(100)) + " \r\n"
"part_mark " + IntToStr(random(100)) + " \r\n"
"st_inner_price "+ IntToStr(random(100)) + " \r\n"
"planed_output " + IntToStr(random(100)) + " \r\n"
"pow_use_lev " + IntToStr(random(100)) + " \r\n"
"trg_inn_price " + IntToStr(random(100)) + " \r\n"
"cur_inn_price " + IntToStr(random(100)) + " \r\n"
"trg_output " + IntToStr(random(100)) + " \r\n"
"amort_perc " + IntToStr(random(100)) + " \r\n"
"pow_load " + IntToStr(random(100)) + " \r\n"
"cur_amort_perc "+ IntToStr(random(100)) + " \r\n"
"max_occupy " + IntToStr(random(100)) + " \r\n"
"cur_occupy " + IntToStr(random(100)) + " \r\n"
"step_occupy " + IntToStr(random(100)) + " \r\n"
"bank_perc " + IntToStr(random(100)) + " \r\n"
"repayment " + IntToStr(random(100)) + " \r\n"
"attractivness " + IntToStr(random(100)) + " \r\n"
"part_attr " + IntToStr(random(100)) + " \r\n"
"price_prod " + IntToStr(random(100)) + " \r\n"
"store " + IntToStr(random(100)) + " \r\n"
"gross_profit " + IntToStr(random(100)) + " \r\n"
"part_gross_profit "+IntToStr(random(100))+ " \r\n"
"tax " + IntToStr(random(100)) + " \r\n"
"sold " + IntToStr(random(100)) + " \r\n"
"nobuy " + IntToStr(random(100)) + " \r\n"
"part_sold " + IntToStr(random(100)) + " \r\n"
"V_market " + IntToStr(random(100)) + " \r\n"
"USD " + IntToStr(random(100)) + " \r\n"
"timer 500 \r\n" ;
Receive_Inf(s);
ListBox1->Items->Add(s.SubString(1,5) +
StrToInt(ListBox1->Items->Count));
s.Delete(1,7);
s2 = Form_Report(s);
SList->Add(s2);
SList2->Add(s);
Plot();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BReportsClick(TObject *Sender)
{
Integer i, nStartSel, nEndSel;
Form3->Show();
Form3->REReports->Clear();
nStartSel = 0; nEndSel = 0;
for (i = 0; i < ListBox1->Items->Count; i++) {
if (i == ListBox1->Items->Count-1) {
Form3->REReports->SelAttributes->Style= TFontStyles()<<fsBold;
Form3->REReports->SelText = ListBox1->Items->Strings[i]+'\n'+SList->Strings[i];
}
else {
Form3->REReports->Lines->Add(ListBox1->Items->Strings[i]);
Form3->REReports->Lines->Add(SList->Strings[i]);
Form3->REReports->Lines->Add(' ');
}
}
}
//---------------------------------------------------------------------------
Модуль UConnect
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UConnect.h"
#include "UClient.h"
#include "UCFuncList.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
extern trueid;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::BConnectClick(TObject *Sender)
{
if((Form2->LENameSim->Text != "") && (Form2->LENameClient->Text !="") &&
(Form2->MEIPAddress->Text != "") && (Form2->EPort->Text != ""))
{
Form1->TcpClient1->RemoteHost = MEIPAddress->Text;
Form1->TcpClient1->RemotePort = StrToInt(EPort->Text);
Name_of_sim = Form2->LENameSim->Text;
Name_of_client = Form2->LENameClient->Text;
UnicodeString auth = L"sim "+Name_of_sim+L" my_name "+Name_of_client;
Form1->TcpClient1->Active = true;
if (Form1->TcpClient1->Connect())
{
int datasz = 0;
void* pBuf = MakeBuf(&datasz,auth);
Form1->TcpClient1->SendBuf(pBuf, datasz ,0);
Form1->TcpClient1->Active = false;
free(pBuf);
// "Клиент отправил данные на подключение");
}
else
{
func_log("Control System " +Name_of_client ,"Connect",
"Нет контакта с сервером");
Application->MessageBox(L"Нет контакта с сервером.",
L"Внимание", MB_ICONWARNING) ;
}
}
else
{
Application->MessageBox(L"Заполнены не все поля!",
L"Внимание", MB_ICONWARNING) ;
}
/* Form1->Show();
Form2->Hide(); */
}
//---------------------------------------------------------------------------
Модуль UReports
#include <vcl.h>
#pragma hdrstop
#include "UReports.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm3 *Form3;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
Приложение Б
(рекомендуемое)
Код программного комплекса Client
Модуль PServer
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
//---------------------------------------------------------------------------
USEFORM("UServer.cpp", Form1);
//---------------------------------------------------------------------------
WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
try
{
HINSTANCE hMutex;
hMutex = CreateMutex(NULL,true,"some_big_big_unique_mutex_name");
if (GetLastError()==ERROR_ALREADY_EXISTS)
{
// мы уже есть - ругаемся и выходим
Application->MessageBox(L"Сервер уже запущен",
L"Повторный запуск сервера", MB_ICONERROR);
Application->Terminate();
}
ReleaseMutex(hMutex);
Application->Initialize();
Application->MainFormOnTaskBar = true;
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
//---------------------------------------------------------------------------
Модуль UFuncList
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <math.h>
#pragma hdrstop
#include "UFuncList.h"
#include "UServer.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
FILE *LogFile,*f;
//---------------------------------------------------------------------------
void func_log(char *Str)
{
if (!FileExists("LogFile.txt"))
{
LogFile = fopen("LogFile.txt","at");
fprintf(LogFile,"Введение лога\n\n%s",Str);
}
else
{
LogFile = fopen("LogFile.txt","at");
fprintf(LogFile,"\n%s",Str);
}
fclose(LogFile);
}















