//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UnitNet.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormNet *FormNet;
#define ProjectTitle_C "网络调试助手"
#define ProjectTitle_E "NetAssistant"
#define ProjectName "TCP232"
#define ProjectVersion "1.0"
static unsigned char SendBuf[MAX_BUF_SIZE+1];
static int default_port=DEFAULT_PORT;
//---------------------------------------------------------------------------
__fastcall TFormNet::TFormNet(TComponent* Owner)
: TForm(Owner)
{UpdateConfig(false);
ReceivedSize=0;
SendedSize=0;
Caption=ProjectTitle_C;
Wnd_cmForm(this,Caption,true);
Wnd_SetMode(1,1);
transmitting=false;
IPEdit_IPAddress=new TcmIPEdit(this);
IPEdit_IPAddress->Parent=Edit_IPAddess->Parent;
IPEdit_IPAddress->Left=Edit_IPAddess->Left;
IPEdit_IPAddress->Top=Edit_IPAddess->Top;
IPEdit_IPAddress->Height=Edit_IPAddess->Height;
IPEdit_IPAddress->Width=Edit_IPAddess->Width;
Edit_IPAddess->Visible=false;
IPEdit_IPAddress2=new TcmIPEdit(this);
IPEdit_IPAddress2->Parent=Edit_IPAddess2->Parent;
IPEdit_IPAddress2->Left=Edit_IPAddess2->Left;
IPEdit_IPAddress2->Top=Edit_IPAddess2->Top;
IPEdit_IPAddress2->Height=Edit_IPAddess2->Height;
IPEdit_IPAddress2->Width=Edit_IPAddess2->Width;
Edit_IPAddess2->Visible=false;
////网络设置设置////////////////////////////
MySocket=new TcmSocket(this);
MySocket->OnAccept=MySocketAccept;
MySocket->OnActiveChanged=MySocketActiveChanged;
MySocket->OnDiscard=MySocketDiscard;
MySocket->OnError=MySocketError;
MySocket->OnReceive=MySocketReceive;
MySocket->MaxBufSize=MAX_BUF_SIZE;
IPEdit_IPAddress->IPdata=MySocket->get_ip_data();
IPEdit_IPAddress2->IPdata=IPEdit_IPAddress->IPdata;
ComboBox_Protocol->ItemIndex=1;
MySocket->Protocol=cmTcpServer;
Edit_PortNUM->Text=IntToStr(default_port);
Edit_PortNUM2->Text=IntToStr(default_port);
MemoSend->Text=HomePageUrl_C;
TextRecv->PopupMenu=ContextPopupMenu;
MemoSend->PopupMenu=ContextPopupMenu;
Net_SyncButtonState();
Caption=ProjectTitle_C;
Application->Title=Caption;
Application->OnHint=OnHint;
TrayIcon=new TcmTrayIcon(this);
TrayIcon->RestoreOnDBCLick=true;
TrayIcon->PopupMenu=PopupMenu1;
TrayIcon->Visible=true;
TrayIcon->Hint=this->Caption;
ContextPopupMenu=new TContextPopupMenu(this);
}
//---------------------------------------------------------------------------
void __fastcall TFormNet::OnHint(TObject *Sender)
{ String strHint;
if(Application->Hint.Length()>0)
{ strHint=Application->Hint;
}
else
{ strHint="就绪!";
}
HintLabel->Caption=strHint;
HintLabel->Update();
}
//---------------------------------------------------------------------------
void TFormNet::UpdateConfig(bool bWriteOrRead)
{ String SectionName="NetSettings",configfile=ChangeFileExt(Application->ExeName,".cfg");
TIniFile *MyIniFile=new TIniFile(configfile);
if(bWriteOrRead)
{ SectionName="Options";
MyIniFile->WriteInteger(SectionName,"LocalPort",MySocket->LocalPort);
MyIniFile->WriteBool(SectionName,"ReceiveShowHex",CheckBox_ReceiveShowHex->Checked);
MyIniFile->WriteBool(SectionName,"ReceiveAutoReturn",CheckBox_ReceiveAutoReturn->Checked);
if(isNumeric(Edit_SendInterval->Text.c_str()))SendInterval=StrToInt(Edit_SendInterval->Text);
MyIniFile->WriteInteger(SectionName,"SendInterval",SendInterval);
if(isNumeric(Edit_PortNUM->Text.c_str()))default_port=StrToInt(Edit_PortNUM->Text);
MyIniFile->WriteInteger(SectionName,"LocalPort",default_port);
MyIniFile->WriteInteger(SectionName,"ChecksumMode",ChecksumMode);
MyIniFile->WriteInteger(SectionName,"FixedCheckSum",FixedCheckSum);
}
else
{ SectionName="Options";
CheckBox_ReceiveShowHex->Checked=MyIniFile->ReadBool(SectionName,"ReceiveShowHex",true);
CheckBox_ReceiveAutoReturn->Checked=MyIniFile->ReadBool(SectionName,"ReceiveAutoReturn",false);
SendInterval=MyIniFile->ReadInteger(SectionName,"SendInterval",1000);
Edit_SendInterval->Text=IntToStr(SendInterval);
default_port=MyIniFile->ReadInteger(SectionName,"LocalPort",DEFAULT_PORT);
ChecksumMode=MyIniFile->ReadInteger(SectionName,"ChecksumMode",0); \
FixedCheckSum=MyIniFile->ReadInteger(SectionName,"FixedCheckSum",0x0D);
}
delete MyIniFile;
}
//---------------------------------------------------------------------------
String TFormNet::ConnectBtnCaption(void)
{ if(MySocket->Active)
{ return "断开 ";
}
else if(MySocket->Protocol==cmTcpServer)
{ return "开始监听 ";
}
else
{ return "连接 ";
}
}
//---------------------------------------------------------------------------
void TFormNet::Net_SyncButtonState(void)
{ if(MySocket->Active)
{ cmBtn_NetConnect->Glyph=NULL;
ImageList1->GetBitmap(1,cmBtn_NetConnect->Glyph);
cmBtn_NetConnect->Caption=ConnectBtnCaption();
if(MySocket->Protocol==cmUDP)
{ Label_IP2->Caption="目标主机:";
Label_portNUM2->Caption="端口:";
Label_portNUM2->Visible=true;
IPEdit_IPAddress2->Visible=true;
IPEdit_IPAddress2->Enabled=true;
Edit_PortNUM2->Visible=true;
Edit_PortNUM2->Enabled=true;
ComboBox_Connections->Visible=false;
}
else if(MySocket->Protocol==cmTcpServer)
{ Label_IP2->Caption="连接对象:";
IPEdit_IPAddress2->Visible=false;
Label_portNUM2->Visible=false;
Edit_PortNUM2->Visible=false;
ComboBox_Connections->Visible=true;
ComboBox_Connections->Items->Text=" All Connections";
ComboBox_Connections->ItemIndex=0;
}
else if(MySocket->Protocol==cmTcpClient)
{ Label_IP2->Caption="本地主机:";
Label_portNUM2->Caption="端口:";
IPEdit_IPAddress2->Visible=true;
IPEdit_IPAddress2->IP=MySocket->LocalHost;
IPEdit_IPAddress2->Enabled=false;
Label_portNUM2->Visible=true;
Edit_PortNUM2->Visible=true;
Edit_PortNUM2->Text=IntToStr(MySocket->LocalPort);
Edit_PortNUM2->Enabled=false;
ComboBox_Connections->Visible=false;
}
}
else
{ cmBtn_NetConnect->Glyph=NULL;
ImageList1->GetBitmap(0,cmBtn_NetConnect->Glyph);
cmBtn_NetConnect->Caption=ConnectBtnCaption();
}
ComboBox_Protocol->Enabled=!MySocket->Active;
IPEdit_IPAddress->Enabled=!MySocket->Active;
Edit_PortNUM->Enabled=!MySocket->Active;
PanelToolBar->Visible=MySocket->Active;
Label_Protocol->Caption="(1)协议类型";
if(MySocket->Protocol==cmUDP || MySocket->Protocol==cmTcpServer)
{ Label_IPAddress->Caption="(2)本地IP地址";
Label_PortNum->Caption="(3)本地端口号";
}
else if(MySocket->Protocol==cmTcpClient)
{ Label_IPAddress->Caption="(2)服务器IP地址";
Label_PortNum->Caption="(3)服务器端口号";
}
}
//---------------------------------------------------------------------------
void __fastcall TFormNet::Btn_SendClick(TObject *Sender)
{ if(CheckBox_FileInput->Checked)
{ Net_BtnClick_SendFromFile(Sender);
return;
}
if(transmitting)
{ transmitting=false;
Btn_Send->Caption="发送";
return;
}
if(!MySocket->Active)
{ char *msgError="网络尚未连接! \n\n发送失败!";
Application->MessageBox(msgError,"Warning",MB_OK | MB_ICONSTOP);
return;
}
int DataSize=0,Offset=0,lastOffset=0,recycleCount=0;
bool SendOK=false;
char *DataPtr=NULL;
transmitting=true;
MemoSend->Enabled=false;
Edit_SendInterval->Enabled=false;
ComboBox_Protocol->Enabled=false;
IPEdit_IPAddress->Enabled=false;
Edit_PortNUM->Enabled=false;
CheckBox_FileInput->Enabled=false;
cmBtn_NetConnect->Enabled=false;
Btn_Send->Caption="停止发送";
if(isNumeric(Edit_SendInterval->Text.c_str()))SendInterval=StrToInt(Edit_SendInterval->Text);
else Edit_SendInterval->Te