📄 sender1.cpp
字号:
/*---------------------------------------------------------------------------
Author: Fran鏾is PIETTE
Description: Simple client program which just send data to a server and display
all incomming data.
Creation: Dec 29, 1998 (From Delphi version created Oct 01, 1998)
Version: 1.03
EMail: francois.piette@swing.be http://www.rtfm.be/fpiette
francois.piette@pophost.eunet.be francois.piette@rtfm.be
Support: Use the mailing list twsocket@rtfm.be See website for details.
Legal issues: Copyright (C) 1998, 1999 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@pophost.eunet.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
History:
Aug 15, 1999 V1.03 Adapted for BCB4 (Moved FIniFileName initialization from
FormCreate to form constructor).
---------------------------------------------------------------------------*/
#include <vcl.h>
#include <vcl/IniFiles.hpp>
#include <stdlib.h>
#pragma hdrstop
#include "Sender1.h"
#define SectionWindow "RecvForm"
#define KeyTop "Top"
#define KeyLeft "Left"
#define KeyWidth "Width"
#define KeyHeight "Height"
#define SectionData "Data"
#define KeyPort "Port"
#define KeyServer "Server"
#define KeyData "Data"
#define KeyRepeat "RepeatCount"
#define KeyContinuous "ContinuousSend"
#define KeyLength "DataLength"
#define KeyUseDataSent "UseDataSent"
#define KeyDisplay "Display"
#define KeyLinger "Linger"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "WSocket"
#pragma resource "*.dfm"
TSenderForm *SenderForm;
//---------------------------------------------------------------------------
__fastcall TSenderForm::TSenderForm(TComponent* Owner)
: TForm(Owner)
{
FIniFileName = LowerCase(ExtractFileName(Application->ExeName));
FIniFileName = FIniFileName.SubString(1, FIniFileName.Length() - 3) + "ini";
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::FormShow(TObject *Sender)
{
TIniFile *IniFile;
if (!FInitialized) {
FInitialized = TRUE;
IniFile = new TIniFile(FIniFileName);
Width = IniFile->ReadInteger(SectionWindow, KeyWidth, Width);
Height = IniFile->ReadInteger(SectionWindow, KeyHeight, Height);
Top = IniFile->ReadInteger(SectionWindow, KeyTop,
(Screen->Height - Height) / 2);
Left = IniFile->ReadInteger(SectionWindow, KeyLeft,
(Screen->Width - Width) / 2);
PortEdit->Text = IniFile->ReadString(SectionData, KeyPort, "telnet");
ServerEdit->Text = IniFile->ReadString(SectionData, KeyServer, "localhost");
DataEdit->Text = IniFile->ReadString(SectionData, KeyData, "The quick brown fox jumps over the lazy dog");
RepeatEdit->Text = IniFile->ReadString(SectionData, KeyRepeat, "");
LengthEdit->Text = IniFile->ReadString(SectionData, KeyLength, "60");
ContCheckBox->Checked = IniFile->ReadInteger(SectionData, KeyContinuous, 0);
LingerCheckBox->Checked = IniFile->ReadInteger(SectionData, KeyLinger, 1);
DisplayDataCheckBox->Checked = IniFile->ReadInteger(SectionData, KeyDisplay, 0);
UseDataSentCheckBox->Checked = IniFile->ReadInteger(SectionData, KeyUseDataSent, 1);
delete IniFile;
RepeatEdit->Enabled = !ContCheckBox->Checked;
CountLabel->Caption = "";
}
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::FormClose(TObject *Sender,
TCloseAction &Action)
{
TIniFile *IniFile;
IniFile = new TIniFile(FIniFileName);
IniFile->WriteInteger(SectionWindow, KeyTop, Top);
IniFile->WriteInteger(SectionWindow, KeyLeft, Left);
IniFile->WriteInteger(SectionWindow, KeyWidth, Width);
IniFile->WriteInteger(SectionWindow, KeyHeight, Height);
IniFile->WriteString(SectionData, KeyPort, PortEdit->Text);
IniFile->WriteString(SectionData, KeyServer, ServerEdit->Text);
IniFile->WriteString(SectionData, KeyData, DataEdit->Text);
IniFile->WriteString(SectionData, KeyRepeat, RepeatEdit->Text);
IniFile->WriteString(SectionData, KeyLength, LengthEdit->Text);
IniFile->WriteInteger(SectionData, KeyContinuous, ContCheckBox->Checked);
IniFile->WriteInteger(SectionData, KeyLinger, LingerCheckBox->Checked);
IniFile->WriteInteger(SectionData, KeyUseDataSent, UseDataSentCheckBox->Checked);
IniFile->WriteInteger(SectionData, KeyDisplay, DisplayDataCheckBox->Checked);
delete IniFile;
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::FormDestroy(TObject *Sender)
{
if (FDataBuf) {
free(FDataBuf);
FDataBuf = NULL;
}
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::ContCheckBoxClick(TObject *Sender)
{
RepeatEdit->Enabled = !ContCheckBox->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::Display(AnsiString Msg)
{
if (DisplayMemo->Lines->Count > 200)
DisplayMemo->Clear();
DisplayMemo->Lines->Add(Msg);
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::ActionButtonClick(TObject *Sender)
{
int Len;
int N;
int T;
AnsiString Buf;
// The ActionButton is used to start or stop data transmission
if (FSending) {
// We are already sending, so user wants to stop
// Display updated counter
CountLabel->Caption = IntToStr(FCount);
// Check if some data remains in TWSocket"s internal buffer
if (!WSocket1->AllSent &&
(Application->MessageBox("Data is still being sent\n"
"Close anyway ?",
"Warning", MB_YESNO) != IDYES))
return;
Display("Stop requested");
if (!WSocket1->AllSent)
Display("Not all data has been sent");
FAutoStart = 0;
// Close the socket-> This will delete any data not already sent to
// winsock.
PostMessage(Handle, WM_CLOSE_REQUEST, 0, (LPARAM)WSocket1);
return;
}
// The user wants to start data transmission
CountLabel->Caption = "";
PauseButton->Caption = "&Pause";
PauseButton->Visible = TRUE;
ActionButton->Caption = "&Stop";
FPaused = FALSE;
FSending = TRUE;
FFinished = FALSE;
FCount = 0;
// Setup final count
if (ContCheckBox->Checked)
FFinalCount = 0;
else
FFinalCount = StrToInt(Trim(RepeatEdit->Text));
// Check which method use to send more data
// Using OnDataSent event will prevent internal TWSocket buffer to be
// enlarged without limit.
FUseDataSent = UseDataSentCheckBox->Checked;
if (FUseDataSent)
WSocket1->OnDataSent = WSocket1DataSent;
else
WSocket1->OnDataSent = WSocket1NoDataSent;
// Prepare data to be sent
Buf = "0000 " + DataEdit->Text;
Len = StrToInt(Trim(LengthEdit->Text));
if (Len <= 0)
Len = Buf.Length();
if (FDataBuf)
free(FDataBuf);
FDataBufSize = Len + 3;
FDataBuf = (char *)malloc(FDataBufSize);
if (Len > 0) {
if (Len < Buf.Length())
Move(&Buf[1], &FDataBuf[0], Len);
else {
T = 0;
while (T < Len) {
N = Buf.Length();
if ((T + N) > Len)
N = Len - T;
if (N > 0)
Move(&Buf[1], &FDataBuf[T], N);
T = T + N;
}
}
}
FDataBuf[Len] = '\r';
FDataBuf[Len + 1] = '\n';
FDataBuf[Len + 2] = 0;
// Launch DNS lookup-> When done, we'll try to connect.
WSocket1->DnsLookup(Trim(ServerEdit->Text));
}
//---------------------------------------------------------------------------
// We comes here when DNS lookup is finished, even in case of failure.
void __fastcall TSenderForm::WSocket1DnsLookupDone(TObject *Sender,
WORD Error)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -