Indy tcp server
来源:互联网 发布:微信视频打赏平台源码 编辑:程序博客网 时间:2024/05/22 14:14
unit serverUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, jpeg, IdBaseComponent, IdComponent,
IdTCPServer, ComCtrls, DB, DBAccess, MyAccess,MyRec;
type
TDataState = (dstNone, dstReceiving);
TForm1 = class(TForm)
ListBox1: TListBox;
Panel1: TPanel;
BtStart: TButton;
BtStop: TButton;
BtClose: TButton;
Image1: TImage;
IdTCPServer1: TIdTCPServer;
ProgressBar1: TProgressBar;
MyConnection1: TMyConnection;
procedure BtCloseClick(Sender: TObject);
procedure BtStartClick(Sender: TObject);
procedure echo(Data: String);
procedure BtStopClick(Sender: TObject);
procedure IdTCPServer1Exception(AThread: TIdPeerThread;
AException: Exception);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
State:TDataState;
implementation
{$R *.dfm}
procedure TForm1.BtCloseClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.echo(Data: String);
begin
if ListBox1.Items.Count >20 then ListBox1.Clear;
ListBox1.items.append(Data);
end;
procedure TForm1.BtStartClick(Sender: TObject);
begin
BtStart.Enabled := False;
IdTCPServer1.Active:=true;
echo('图片传送服务已启动 ( yiparts.com )');
BtStop.Enabled := True;
end;
procedure TForm1.BtStopClick(Sender: TObject);
begin
BtStart.Enabled := True;
IdTCPServer1.Active:=false;
echo('服务停止');
BtStop.Enabled := False;
end;
procedure TForm1.IdTCPServer1Exception(AThread: TIdPeerThread;
AException: Exception);
var
aFileSize : Integer; //aFileSize用来表示文件大小,整型
aFileName : String; //aFileName表示文件名,字串型
Buff : array[0..1023] of Byte; // Buff 缓存区大小设置,byte型
ReadCount : Integer; //实际每次读取文件块的大小,整型
Stream : TFileStream;
s,sCommand, sAction : string;
begin
s := AThread.Connection.ReadLn(#13#10, 100);
sCommand := copy(s,1,4);
sAction := copy(s,6,100);
if sCommand = 'SEND' then
begin
Stream:=nil;
if State = dstNone then
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//aFileName := AThread.Connection.ReadLn(#13#10, 100);
aFileName := sAction;
if aFileName = '' then
Exit;
AThread.Connection.WriteLn;
AThread.Connection.ReadBuffer(aFileSize, 4);//开始读取文件长度,放进变量aFileSize中
ProgressBar1.Max := aFileSize;
echo('开始接收文件:'+ aFileName);
Stream := TFileStream.Create(aFileName, fmCreate);//创建一个文件流
State := dstReceiving;
end;
end;//State = dstNone
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件流
repeat
if aFileSize - Stream.Size > SizeOf(Buff) then
ReadCount := SizeOf(Buff)
else
ReadCount := aFileSize - Stream.Size;
//从连接中读取 ReadCount长度的文件块放到缓冲区Buff,中
AThread.Connection.ReadBuffer(Buff, ReadCount);
//将缓冲区中的内容写进文件流中,这是就是写到文件aFileName中啦
Stream.WriteBuffer(Buff, ReadCount);
//文件流当前写的大小,值赋给进度条的位置
ProgressBar1.Position := Stream.Size;
//显示已经传输和实际文件大小的比例
Caption := IntToStr(trunc(Stream.Size/1024)) + 'K/' + IntToStr(trunc(aFileSize/1024)) + 'K';
//这句作用是让消息传递动态显示起来,如果没有这句上面的caption是不会显示跳动的
Application.ProcessMessages;
until Stream.Size >= aFileSize; //直到文件大小和原文件大小一致结束循环
AThread.Connection.WriteLn('OK'); //返回一个信息 OK,让客户端知道你读完了
end;
Stream.Free;
//Stream:=nil;
//定义成没有人连接,准备接受下一个人的连接
State:= dstNone;
echo('文件接收完成');
echo('开始分析记录文件');
end; //IF SEND
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
echo('客户端断开连接');
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
echo('客户端连接成功');
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, jpeg, IdBaseComponent, IdComponent,
IdTCPServer, ComCtrls, DB, DBAccess, MyAccess,MyRec;
type
TDataState = (dstNone, dstReceiving);
TForm1 = class(TForm)
ListBox1: TListBox;
Panel1: TPanel;
BtStart: TButton;
BtStop: TButton;
BtClose: TButton;
Image1: TImage;
IdTCPServer1: TIdTCPServer;
ProgressBar1: TProgressBar;
MyConnection1: TMyConnection;
procedure BtCloseClick(Sender: TObject);
procedure BtStartClick(Sender: TObject);
procedure echo(Data: String);
procedure BtStopClick(Sender: TObject);
procedure IdTCPServer1Exception(AThread: TIdPeerThread;
AException: Exception);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
State:TDataState;
implementation
{$R *.dfm}
procedure TForm1.BtCloseClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.echo(Data: String);
begin
if ListBox1.Items.Count >20 then ListBox1.Clear;
ListBox1.items.append(Data);
end;
procedure TForm1.BtStartClick(Sender: TObject);
begin
BtStart.Enabled := False;
IdTCPServer1.Active:=true;
echo('图片传送服务已启动 ( yiparts.com )');
BtStop.Enabled := True;
end;
procedure TForm1.BtStopClick(Sender: TObject);
begin
BtStart.Enabled := True;
IdTCPServer1.Active:=false;
echo('服务停止');
BtStop.Enabled := False;
end;
procedure TForm1.IdTCPServer1Exception(AThread: TIdPeerThread;
AException: Exception);
var
aFileSize : Integer; //aFileSize用来表示文件大小,整型
aFileName : String; //aFileName表示文件名,字串型
Buff : array[0..1023] of Byte; // Buff 缓存区大小设置,byte型
ReadCount : Integer; //实际每次读取文件块的大小,整型
Stream : TFileStream;
s,sCommand, sAction : string;
begin
s := AThread.Connection.ReadLn(#13#10, 100);
sCommand := copy(s,1,4);
sAction := copy(s,6,100);
if sCommand = 'SEND' then
begin
Stream:=nil;
if State = dstNone then
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//aFileName := AThread.Connection.ReadLn(#13#10, 100);
aFileName := sAction;
if aFileName = '' then
Exit;
AThread.Connection.WriteLn;
AThread.Connection.ReadBuffer(aFileSize, 4);//开始读取文件长度,放进变量aFileSize中
ProgressBar1.Max := aFileSize;
echo('开始接收文件:'+ aFileName);
Stream := TFileStream.Create(aFileName, fmCreate);//创建一个文件流
State := dstReceiving;
end;
end;//State = dstNone
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件流
repeat
if aFileSize - Stream.Size > SizeOf(Buff) then
ReadCount := SizeOf(Buff)
else
ReadCount := aFileSize - Stream.Size;
//从连接中读取 ReadCount长度的文件块放到缓冲区Buff,中
AThread.Connection.ReadBuffer(Buff, ReadCount);
//将缓冲区中的内容写进文件流中,这是就是写到文件aFileName中啦
Stream.WriteBuffer(Buff, ReadCount);
//文件流当前写的大小,值赋给进度条的位置
ProgressBar1.Position := Stream.Size;
//显示已经传输和实际文件大小的比例
Caption := IntToStr(trunc(Stream.Size/1024)) + 'K/' + IntToStr(trunc(aFileSize/1024)) + 'K';
//这句作用是让消息传递动态显示起来,如果没有这句上面的caption是不会显示跳动的
Application.ProcessMessages;
until Stream.Size >= aFileSize; //直到文件大小和原文件大小一致结束循环
AThread.Connection.WriteLn('OK'); //返回一个信息 OK,让客户端知道你读完了
end;
Stream.Free;
//Stream:=nil;
//定义成没有人连接,准备接受下一个人的连接
State:= dstNone;
echo('文件接收完成');
echo('开始分析记录文件');
end; //IF SEND
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
echo('客户端断开连接');
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
echo('客户端连接成功');
end;
end.
- Indy tcp server
- Indy tcp client
- Indy Client / Server 程序示例
- indy tcp的即时通讯的问题
- indy
- Indy TCP/IP 组件里的几个常用方法
- Delphi2009的Indy全接触之TCP篇
- Delphi中关于TCP通讯,Indy粘数据包问题
- Delphi2009的Indy全接触之TCP篇
- 关于Delphi中Indy控件,TCP通信实时性问题
- TCP server
- TCP Server
- tcp-server
- TCP--server
- 使用INDY TCP组件实现基于协议采用XML方式的文件传输
- indy编程
- Indy + TStringList
- Indy 9.0
- 看过这个视频后真有点想哭!
- duniya vm 完成
- 继续无聊
- 用DELPHI实现文件加密压缩
- 把Oracle中的数据导到文本文件
- Indy tcp server
- Indy tcp client
- 对数学的思考(转载)
- SQL Server 2005 学习笔记--SSIS篇(2)
- CSS盒子模式详解1
- Lucene.Net系列一 ---simple example
- Lucene.net 系列二 --- index (上)
- 空手道及主要流派简介
- Lucene.net 系列三 --- index 中