TcpClient 与 TcpServer UdpSocket 的使用方法!(20分)

  • 主题发起人 主题发起人 QSmile
  • 开始时间 开始时间
Q

QSmile

Unregistered / Unconfirmed
GUEST, unregistred user!
标题:TcpClient 与 TcpServer UdpSocket 的使用方法!

简要谈谈,给个试子最好
 
多多少少给点提示吧?
 
服务器端:
unit uServer;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls, Sockets,
ExtCtrls;

type
TfrmMain = class(TForm)
IdTCPServer: TIdTCPServer;
TcpServer: TTcpServer;
Panel1: TPanel;
ServerType: TRadioGroup;
btnStart: TButton;
btnStop: TButton;
Log: TMemo;
Label1: TLabel;
btnClear: TButton;
procedure IdTCPServerExecute(AThread: TIdPeerThread);
procedure btnStartClick(Sender: TObject);
procedure btnClearClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TcpServerAccept(Sender: TObject;
ClientSocket: TCustomIpClient);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
procedure AddLogMsg(const aMsg:string='');
procedure SetButton(const ServerStarted: boolean=true);
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}
const
IDFileName = 'C:/IDFile.tst';
BRFileName = 'C:/BRFile.tst';

procedure TfrmMain.AddLogMsg(const aMsg: string);
begin
Log.Lines.Add(aMsg);
end;

procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
buf: TMemoryStream;
begin
if FileExists(IDFileName) then DeleteFile(IDFileName);
AddLogMsg('IdTCPServerExecute - begining receive data...');
buf := TMemoryStream.Create;
try
AThread.Connection.ReadStream(buf, -1, true);
//AThread.Connection.ReadStream(buf);
AddLogMsg('Receive data completed');
buf.SaveToFile(IDFileName);
AddLogMsg('Save data to file of '+IDFileName);
finally
buf.Free;
end;
end;

procedure TfrmMain.SetButton(const ServerStarted: boolean);
begin
btnStart.Enabled := not ServerStarted;
btnStop.Enabled := ServerStarted;
end;

procedure TfrmMain.btnStartClick(Sender: TObject);
begin
IdTcpServer.Active := true;
AddLogMsg(Format('IdTcpServer started at localhost:%d',[IdTcpServer.DefaultPort]));

TcpServer.Active := true;
AddLogMsg(Format('Borland TcpServer started at localhost:%s',[TcpServer.LocalPort]));

SetButton(true);
end;

procedure TfrmMain.btnClearClick(Sender: TObject);
begin
Log.Lines.Clear;
end;

procedure TfrmMain.btnStopClick(Sender: TObject);
//var
// Idx: integer;
begin
IdTcpServer.Active := false;
AddLogMsg('IdTcpServer stopped');

TcpServer.Active := false;
// for Idx:=0 to TcpServer.ServerSocketThread.ThreadPool.Count-1 do
// TClientSocketThread(TcpServer.ServerSocketThread.ThreadPool.Items[idx]).Terminate;
AddLogMsg('Borland TcpServer stopped');

SetButton(false);
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
SetButton(false);
end;

procedure TfrmMain.TcpServerAccept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
Rbuf: array[0..8191] of byte;
RCount: integer;

// fBuf: TMemoryStream;
fBuf: TFileStream;
begin
if FileExists(BRFileName) then DeleteFile(BRFileName);
AddLogMsg('TcpServerAccept - begining receive data...');
//fbuf := TMemoryStream.Create;

fBuf := TFileStream.Create('c:/'+FormatDateTime('mm-ss-zzz',now), fmCreate);
// fBuf := TFileStream.Create(BRFileName, fmCreate);
try
RCount := ClientSocket.ReceiveBuf(Rbuf, 8192);
while RCount > 0 do
begin
fBuf.WriteBuffer(Rbuf, RCount);

RCount := ClientSocket.ReceiveBuf(Rbuf, 8192);
end;

AddLogMsg('Receive data completed');

//fbuf.SaveToFile(BRFileName);
AddLogMsg('Save data to file of '+BRFileName);
//ClientSocket.Disconnect;
finally
fbuf.Free;
end;
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
Idx: Integer;
Buf: TList;
begin
buf := TcpServer.ServerSocketThread.ThreadPool;
TcpServer.ServerSocketThread.ServerSocket.Close;
for Idx := buf.Count - 1 downto 0 do

with TClientSocketThread(buf[Idx]) do
begin
Terminate;
Free;
end;
end;

end.

-------------------------------------------------------------

客户端:
unit uClient;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Sockets, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdAntiFreezeBase, IdAntiFreeze;

type
TfrmMain = class(TForm)
Panel1: TPanel;
Label1: TLabel;
ServerType: TRadioGroup;
btnSendStream: TButton;
btnSendBuf: TButton;
btnClear: TButton;
Log: TMemo;
IdTCPClient: TIdTCPClient;
TcpClient: TTcpClient;
SendFile: TLabeledEdit;
btnBrowse: TButton;
OpenDlg: TOpenDialog;
SvrAddr: TLabeledEdit;
IdAntiFreeze: TIdAntiFreeze;
SvrPort: TLabeledEdit;
procedure btnClearClick(Sender: TObject);
procedure btnBrowseClick(Sender: TObject);
procedure btnSendStreamClick(Sender: TObject);
procedure ServerTypeClick(Sender: TObject);
private
procedure AddLogMsg(const aMsg: string='');
procedure SendStreamUseIndy;
procedure SendStreamUseBorland;
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.btnClearClick(Sender: TObject);
begin
Log.Lines.Clear;
end;

procedure TfrmMain.btnBrowseClick(Sender: TObject);
begin
if not OpenDlg.Execute then exit;
SendFile.Text := OpenDlg.FileName;
end;

procedure TfrmMain.SendStreamUseBorland;
var
buf: TMemoryStream;
begin
buf := TMemoryStream.Create;
try
buf.LoadFromFile(SendFile.Text);
AddLogMsg(Format('load data from %s, size=%d',[SendFile.Text, buf.Size]));

TcpClient.RemoteHost := SvrAddr.Text;
TcpClient.RemotePort := SvrPort.Text;
TcpClient.Connect();
AddLogMsg(Format('Connected to %s:%s',[TcpClient.RemoteHost, TcpClient.RemotePort]));

AddLogMsg('Sending');
TcpClient.SendStream(buf);
AddLogMsg('Send buffer completed');

sleep(10);
TcpClient.Disconnect;
AddLogMsg('disconnect connection');
finally
buf.Free;
end;
end;

procedure TfrmMain.SendStreamUseIndy;
var
buf: TMemoryStream;
begin
buf := TMemoryStream.Create;
try
buf.LoadFromFile(SendFile.Text);
AddLogMsg(Format('load data from %s, size=%d',[SendFile.Text, buf.Size]));

IdTcpClient.Host := SvrAddr.Text;
IdTcpClient.Port := StrToIntDef(SvrPort.Text, 8192);
IdTcpClient.Connect();
AddLogMsg(Format('Connected to %s:%d',[IdTcpClient.Host, IdTcpClient.Port]));

AddLogMsg('Sending');
IdTcpClient.WriteStream(buf);
AddLogMsg('Send buffer completed');

sleep(10);
IdTcpClient.Disconnect;
AddLogMsg('disconnect connection');
finally
buf.Free;
end;
end;

procedure TfrmMain.AddLogMsg(const aMsg: string);
begin
Log.Lines.Add(aMsg);
end;

procedure TfrmMain.btnSendStreamClick(Sender: TObject);
begin
Case ServerType.ItemIndex of
0: SendStreamUseBorland;
1: SendStreamUseIndy;
end;
end;

procedure TfrmMain.ServerTypeClick(Sender: TObject);
begin
case ServerType.ItemIndex of
0: SvrPort.Text := '8193';
1: SvrPort.Text := '8192';
end;
end;

end.
 
客户端project1,服务器project2,
project1用IdUDPClient控件,
project2用IdUDPServer控件,
project1->
procedure TForm1.Button1Click(Sender: TObject);
begin
IdUDPClient1.Host:='127.0.0.1';
IdUDPClient1.port:=1024;
IdUDPClient1.send('command');
end;
project2->
procedure TForm1.FormCreate(Sender: TObject);
begin
IdUDPServer1.DefaultPort:=1024;
IdUDPServer1.active:=true;
end;

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var tt:TStringStream;
begin
tt:=TStringStream.create('');
tt.Read(AData,AData.size);
if pos(tt.DataString,'command') = 0 then
begin
Button1Click(nil);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('hello world');
end;
 
终于有人回答了,给分
 
后退
顶部