Delphi Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
델파이 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
FreePascal/Lazarus
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
델마당
볼랜드포럼 광고 모집

델파이 자료실
Delphi Programming Resources
[312] [ Indy10 ] 기본 채팅 + 파일 보내기 + 파일 받기
이즈니 [jeju123] 7760 읽음    2011-07-10 23:29
안녕하십니까.
이즈니 입니다.

Indy9 버전에서 Indy10 버전으로 올라가면서 바뀐 부분이 있어 테스트겸 하여 만들었습니다.
델파이 7 + Indy10 + JEDI : 버전이 맞지 않거나 설치되지 않은 분은 아래의 소스만 참조 ^^;;

( 하나의 폼에 서버와 클라이언트의 기능을 넣느라 소스가 좀... 이해바랍니다. )

-----------------------------------------------------------------------------------------
unit Socket5Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, WinSock, IdHTTP, IdStreamVCL, IdContext, XPMan, JvDialogs, DB,
  JvDataSource, JvMemoryDataset, ImgList, ExtCtrls, IdAntiFreezeBase,
  IdAntiFreeze, IdTCPConnection, IdTCPClient, IdBaseComponent, IdComponent,
  IdTCPServer, JvSpecialProgress, StdCtrls, JvExStdCtrls, JvCheckBox,
  JvEdit, Grids, DBGrids, JvExDBGrids, JvDBGrid, JvMemo, Buttons,
  JvExButtons, JvBitBtn, JvCombobox, JvButton, JvTransparentButton, JvLED,
  JvLabel, ComCtrls, JvExComCtrls, JvComCtrls, JvExControls, JvGradient;

type
  TSocket5MainForm = class(TForm)
    BackGroundGradient: TJvGradient;
    XPManifest1: TXPManifest;
    TcpServer: TIdTCPServer;
    TcpClient: TIdTCPClient;
    IdAntiFreeze: TIdAntiFreeze;
    ClientTimer: TTimer;
    RotateImageList: TImageList;
    ServerRotateTimer: TTimer;
    MemoryData: TJvMemoryData;
    MemoryDataFileName: TStringField;
    MemoryDataDs: TJvDataSource;
    PageControl: TJvPageControl;
    Server: TTabSheet;
    JvLabel1: TJvLabel;
    JvLabel2: TJvLabel;
    ServerConnectLED: TJvLED;
    ServerRotate: TJvTransparentButton;
    ServerIpEdit: TJvComboBox;
    ServerPortEdit: TJvComboBox;
    ServerConnectButton: TJvBitBtn;
    ServerMsgMemo: TJvMemo;
    DBGrid1: TJvDBGrid;
    ServerStatusMemo: TJvMemo;
    ServerMsgEdit: TJvEdit;
    ServerEnterButton: TJvBitBtn;
    AllCheckBox: TJvCheckBox;
    ServerIdEdit: TJvEdit;
    CloseButton1: TJvBitBtn;
    Client: TTabSheet;
    ServerFileButton: TSpeedButton;
    ServerFileDownButton: TJvBitBtn;
    JvLabel5: TJvLabel;
    JvLabel4: TJvLabel;
    ClientConnectLED: TJvLED;
    ClientRotate: TJvTransparentButton;
    ClientIpEdit: TJvComboBox;
    ClientPortEdit: TJvComboBox;
    ClientConnectButton: TJvBitBtn;
    ClientMsgMemo: TJvMemo;
    ClientIdEdit: TJvEdit;
    ClientMsgEdit: TJvEdit;
    ClientEnterButton: TJvBitBtn;
    ClientStatusMemo: TJvMemo;
    CloseButton2: TJvBitBtn;
    OpenDialog: TJvOpenDialog;
    ClientFileUpButton: TJvBitBtn;
    ClientFileDownButton: TJvBitBtn;
    ClientlProgress: TJvSpecialProgress;
    ClientFileButton: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure ServerConnectButtonClick(Sender: TObject);
    procedure AllCheckBoxClick(Sender: TObject);
    procedure TcpServerConnect(AContext: TIdContext);
    procedure TcpServerDisconnect(AContext: TIdContext);
    procedure TcpServerExecute(AContext: TIdContext);
    procedure ServerEnterButtonClick(Sender: TObject);
    procedure ServerIpEditExit(Sender: TObject);
    procedure ServerIpEditKeyPress(Sender: TObject; var Key: Char);
    procedure ServerRotateTimerTimer(Sender: TObject);
    procedure ServerFileDownButtonClick(Sender: TObject);
    procedure ClientConnectButtonClick(Sender: TObject);
    procedure ClientEnterButtonClick(Sender: TObject);
    procedure TcpClientConnected(Sender: TObject);
    procedure TcpClientDisconnected(Sender: TObject);
    procedure ClientTimerTimer(Sender: TObject);
    procedure TcpClientStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: String);
    procedure DBGrid1DblClick(Sender: TObject);
    procedure CloseButton1Click(Sender: TObject);
    procedure ClientFileUpButtonClick(Sender: TObject);
    procedure TcpClientWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Integer);
    procedure TcpClientWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Integer);
    procedure TcpClientWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
    procedure ServerFileButtonClick(Sender: TObject);
    procedure ClientFileDownButtonClick(Sender: TObject);
  private
    { Private declarations }
    procedure WindowStateChecking;
    procedure TcpClientConnect( Bool: Boolean );
    procedure TcpServerActive( Bool: Boolean );
    function  IpAddress( Bool: Boolean ) : String;
    procedure IpEditIpAddressMove;
    procedure AnyMessage( var Msg: TMsg; var Handled: Boolean );
  public
    { Public declarations }
  end;

var
  Socket5MainForm: TSocket5MainForm;
  DataDir: String;
  MaxValue: Integer;

implementation

{$R *.dfm}

procedure TSocket5MainForm.WindowStateChecking;
var hWnd: THandle;
begin
  hWnd := Self.Handle;
  if hWnd <> 0 then begin
    Application.Restore;
    if Self.WindowState = wsMinimized then Self.WindowState := wsNormal;
    SetWindowPos(hWnd, HWND_TOPMOST,   Left, Top, Width, Height, SWP_SHOWWINDOW);
    SetWindowPos(hWnd, HWND_NOTOPMOST, Left, Top, Width, Height, SWP_SHOWWINDOW);
  end;
end;

procedure TSocket5MainForm.TcpClientConnect( Bool: Boolean );
begin
  if Bool then begin
    try
      TcpClient.Host := ClientIpEdit.Text;
      TcpClient.Port := StrToInt( ClientPortEdit.Text );
      TcpClient.Connect;
      ClientConnectButton.Caption := '끊기 F1';
    except
      ClientStatusMemo.Lines.Add( '연결 에러입니다.' );
    end;
  end else begin
    try
      TcpClient.Disconnect;
      ClientConnectButton.Caption := '연결 F1';
    except
      ClientStatusMemo.Lines.Add( '연결끊기 에러입니다.' );
    end;
  end;
  ClientConnectLED.Status := TcpClient.Connected;
  ClientTimer.Enabled := TcpClient.Connected;
end;

procedure TSocket5MainForm.TcpServerActive( Bool: Boolean );
var cList: TList;
    Count: Integer;
begin
  if Bool then begin
    try
      MemoryData.Open;
      MemoryData.Append;
      MemoryData.FieldByName( 'IpAddr' ).AsString := ServerIpEdit.Text;
      MemoryData.Post;
      TcpServer.DefaultPort := StrToInt( ServerPortEdit.Text );
      TcpServer.Active := true;
      ServerConnectButton.Caption := '끊기 F1';
      ServerStatusMemo.Lines.Add( '연결 되었습니다.' );
    except
      MemoryData.Cancel;
      ServerStatusMemo.Lines.Add( '연결 에러입니다.' );
    end;
  end else begin
    try
      cList := TcpServer.Contexts.LockList;
      for Count := 0 to cList.Count - 1 do TIdContext( cList[ Count ] ).Connection.Disconnect;
    finally
      TcpServer.Contexts.UnlockList;
    end;
    try
      MemoryData.Close;
      TcpServer.Active := false;
      ServerConnectButton.Caption := '연결 F1';
      ServerStatusMemo.Lines.Add( '연결을 끊었습니다.' );
    except
      ServerStatusMemo.Lines.Add( '연결끊기 에러입니다.' );
    end;
  end;
  ServerConnectLED.Status := TcpServer.Active;
  ServerRotateTimer.Enabled := TcpServer.Active;
end;

function  TSocket5MainForm.IpAddress( Bool: Boolean ) : String;
var WSData: TWSAData;
    HostEnt: PHostEnt;
    HostName: String;
    IdHttp: TIdHTTP;
    nPos: Integer;
begin
  try
    if Bool then begin
      WSAStartup( $101, WSData );
      SetLength( HostName, 255 );
      GetHostName( PChar( HostName ), 255 );
      HostEnt := GetHostByName( PChar( HostName ) );
      with HostEnt^^ do Result := Format( '%d.%d.%d.%d', [ Byte( h_addr^[ 0 ] ), Byte( h_addr^[ 1 ] ), Byte( h_addr^[ 2 ] ), Byte( h_addr^[ 3 ] ) ] );
      WSACleanup;
    end else begin
      IdHTTP := TIdHTTP.Create( nil );
      try
        Result := IdHTTP.Get( 'http://checkip.dyndns.com' );
      finally
        IdHTTP.Free;
      end;
      nPos := Pos( ':', Result );
      if nPos > 0 then Result := Copy( Result, nPos + 1, Length( Result ) - nPos );
      nPos := Pos( '<', Result );
      if nPos > 0 then Result := Copy( Result, 1, nPos - 1 );
      Result := Trim( Result );
    end;
  except
    Result := '0.0.0.0';
  end;
end;

procedure TSocket5MainForm.IpEditIpAddressMove;
begin
  ServerIpEdit.Items.Clear;
  ServerIpEdit.Items.Add( IpAddress( true ) );
  ServerIpEdit.Items.Add( IpAddress( false ) );
  ServerIpEdit.ItemIndex := 0;
  ClientIpEdit.Items.Clear;
  ClientIpEdit.Items.Add( IpAddress( true ) );
  ClientIpEdit.Items.Add( IpAddress( false ) );
  ClientIpEdit.ItemIndex := 0;
end;

procedure TSocket5MainForm.AnyMessage( var Msg: TMsg; var Handled: Boolean );
begin
  if Msg.message = WM_MouseWheel then begin
    if ActiveControl is TJvDBGrid then begin
      if Msg.wParam > 0 then begin
        KeyBd_Event( VK_UP, VK_UP, KEYEVENTF_KEYUP, 0 );
        ActiveControl.Invalidate;
      end else if Msg.wParam < 0 then begin
        KeyBd_Event( VK_DOWN, VK_DOWN, KEYEVENTF_KEYUP, 0 );
        ActiveControl.Invalidate;
      end;
    end;
  end;
end;

procedure TSocket5MainForm.FormCreate(Sender: TObject);
begin
  Application.Title := Self.Caption;
  Application.OnMessage := AnyMessage;
  DataDir := ExtractFilePath( ParamStr( 0 ) ) + 'Down\';
  ForceDirectories( DataDir );
  IpEditIpAddressMove;
end;

procedure TSocket5MainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if TcpServer.Active then TcpServerActive( false );
  if TcpClient.Connected then TcpClientConnect( false );
end;

procedure TSocket5MainForm.FormActivate(Sender: TObject);
begin
  if PageControl.ActivePageIndex in [ 0 ] then ServerIpEdit.SetFocus;
end;

procedure TSocket5MainForm.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_F1 then begin
    Key := 0;
    if PageControl.ActivePageIndex in [ 0 ] then ServerConnectButtonClick( nil )
                                            else ClientConnectButtonClick( nil );
  end;
end;

procedure TSocket5MainForm.ServerConnectButtonClick(Sender: TObject);
begin
  if TcpServer.Active then TcpServerActive( false )
                      else TcpServerActive( true );
  ServerMsgEdit.SetFocus;
end;

procedure TSocket5MainForm.AllCheckBoxClick(Sender: TObject);
begin
  ServerMsgEdit.SetFocus;
end;

procedure TSocket5MainForm.TcpServerConnect(AContext: TIdContext);
begin
  with MemoryData do begin
    try
      Append;
      FieldByName( 'IpAddr' ).AsString := AContext.Connection.Socket.Binding.PeerIP;
      Post;
    except
      Cancel;
    end;
  end;
  ServerStatusMemo.Lines.Add( AContext.Connection.Socket.Binding.PeerIP + ' 연결됨' );
end;

procedure TSocket5MainForm.TcpServerDisconnect(AContext: TIdContext);
begin
  with MemoryData do begin
    try
      if Locate( 'IpAddr', AContext.Connection.Socket.Binding.PeerIP,[ loPartialKey ] ) then begin
        Edit;
        Delete;
      end;
    except
      Cancel;
    end;
  end;
  ServerStatusMemo.Lines.Add( AContext.Connection.Socket.Binding.PeerIP + ' 끊김' );
end;

procedure TSocket5MainForm.TcpServerExecute(AContext: TIdContext);
var cList: TList;
    Count: Integer;
    Msg: String;
    fName: String;
    fSize: Integer;
    fStream: TIdStreamVCL;
begin
  WindowStateChecking;
  Msg := AContext.Connection.Socket.Binding.PeerIP + AContext.Connection.IOHandler.ReadLn;
  ServerMsgMemo.Lines.Add( Msg );
  if Msg = AContext.Connection.Socket.Binding.PeerIP +  ':파일 보내기' then begin
    Msg := AContext.Connection.IOHandler.ReadLn;
    fName := DataDir + Msg;
    ServerMsgMemo.Lines.Add( ':파일명 - ' + Msg );
    Msg := AContext.Connection.IOHandler.ReadLn;
    fSize := StrToInt( Msg );
    ServerMsgMemo.Lines.Add( ':파일 크기 - ' + FormatFloat( '#,##0', fSize ) + '바이트' );
    Application.ProcessMessages;
    fStream := TIdStreamVCL.Create( TFileStream.Create( fName, fmCreate ), true );
    Application.ProcessMessages;
    try
      AContext.Connection.IOHandler.ReadStream( fStream, fSize, false );
    finally
      FreeAndNil( fStream );
      ServerMsgMemo.Lines.Add( ':파일 위치 - ' + fName );
      ServerMsgMemo.Lines.Add( ':파일 받기 완료!' );
    end;
  end else if Msg = AContext.Connection.Socket.Binding.PeerIP +  ':파일 받기' then begin
    Msg := AContext.Connection.IOHandler.ReadLn;
    fName := Msg;
    Application.ProcessMessages;
    fStream := TIdStreamVCL.Create( TFileStream.Create( fName, fmOpenRead + fmShareDenyNone ), true );
    Application.ProcessMessages;
    fSize := fStream.Size;
    ServerMsgMemo.Lines.Add( ':파일명 - ' + ExtractFileName( fName ) );
    ServerMsgMemo.Lines.Add( ':파일 크기 - ' + FormatFloat( '#,##0', fSize ) + '바이트' );
    AContext.Connection.IOHandler.WriteLn( ':파일 받기' );
    AContext.Connection.IOHandler.WriteLn( ExtractFileName( fName ) );
    AContext.Connection.IOHandler.WriteLn( IntToStr( fSize ) );
    try
      AContext.Connection.IOHandler.Write( fStream );
    finally
      FreeAndNil( fStream );
      ServerMsgMemo.Lines.Add( ':파일 보내기 완료!' );
    end;
  end else begin
    if AllCheckBox.Checked then begin
      try
        cList := TcpServer.Contexts.LockList;
        for Count := 0 to cList.Count - 1 do TIdContext( cList[ Count ] ).Connection.IOHandler.WriteLn( Msg );
      finally
        TcpServer.Contexts.UnlockList;
      end;
    end else begin
      Acontext.Connection.IOHandler.WriteLn( Msg );
    end;
  end;
end;

procedure TSocket5MainForm.ServerEnterButtonClick(Sender: TObject);
var cList: TList;
    Count: Integer;
    Msg: String;
begin
  Msg := ServerIpEdit.Text + ServerIdEdit.Text + ' > ' + ServerMsgEdit.Text;
  ServerMsgMemo.Lines.Add( Msg );
  if AllCheckBox.Checked then begin
    try
      cList := TcpServer.Contexts.LockList;
      for Count := 0 to cList.Count - 1 do TIdContext( cList[ Count ] ).Connection.IOHandler.WriteLn( Msg );
    finally
      TcpServer.Contexts.UnlockList;
    end;
  end else begin
    try
      cList := TcpServer.Contexts.LockList;
      for Count := 0 to cList.Count - 1 do begin
        if TIdContext( cList[ Count ] ).Connection.Socket.Binding.PeerIP =
           MemoryData.FieldByName( 'IpAddr' ).AsString then TIdContext( cList[ Count ] ).Connection.IOHandler.WriteLn( Msg );
      end;
    finally
      TcpServer.Contexts.UnlockList;
    end;
  end;
  ServerMsgEdit.Text := '';
  ServerMsgEdit.SetFocus;
end;

procedure TSocket5MainForm.ServerIpEditExit(Sender: TObject);
begin
  if ServerIpEdit.ImeMode = imSHanguel then ServerIpEdit.ImeMode := imDontCare;
end;

procedure TSocket5MainForm.ServerIpEditKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then begin
    Key := #0;
    if TWinControl( Sender ) = ServerMsgEdit then ServerEnterButtonClick( nil ) else
    if TWinControl( Sender ) = ClientMsgEdit then ClientEnterButtonClick( nil )
                                             else SelectNext( TWinControl( Sender ), true, true );
  end;
end;

procedure TSocket5MainForm.ServerRotateTimerTimer(Sender: TObject);
begin
  if ServerRotate.Images.ActiveIndex in [ 7 ] then ServerRotate.Images.ActiveIndex := 0
                                              else ServerRotate.Images.ActiveIndex := ServerRotate.Images.ActiveIndex + 1;
end;

procedure TSocket5MainForm.ServerFileDownButtonClick(Sender: TObject);
var cList: TList;
    Count: Integer;
    fName: String;
    fSize: Integer;
    fStream: TIdStreamVCL;
begin
  if FileExists( ServerMsgEdit.Text ) and TcpServer.Active then begin
    fName := ServerMsgEdit.Text;
    Application.ProcessMessages;
    fStream := TIdStreamVCL.Create( TFileStream.Create( fName, fmOpenRead + fmShareDenyNone ), true );
    Application.ProcessMessages;
    fSize := fStream.Size;
    ServerMsgMemo.Lines.Add( ':파일명 - ' + ExtractFileName( fName ) );
    ServerMsgMemo.Lines.Add( ':파일 크기 - ' + FormatFloat( '#,##0', fSize ) + '바이트' );
    if AllCheckBox.Checked then begin
      try
        cList := TcpServer.Contexts.LockList;
        for Count := 0 to cList.Count - 1 do begin
          ServerMsgMemo.Lines.Add( ':파일 보내기 - ' + TIdContext( cList[ Count ] ).Connection.Socket.Binding.PeerIP );
          TIdContext( cList[ Count ] ).Connection.IOHandler.WriteLn( ':파일 받기' );
          TIdContext( cList[ Count ] ).Connection.IOHandler.WriteLn( ExtractFileName( fName ) );
          TIdContext( cList[ Count ] ).Connection.IOHandler.WriteLn( IntToStr( fSize ) );
          TIdContext( cList[ Count ] ).Connection.IOHandler.Write( fStream );
        end;
      finally
        FreeAndNil( fStream );
        TcpServer.Contexts.UnlockList;
        ServerMsgMemo.Lines.Add( ':파일 보내기 완료!' );
      end;
    end else begin
      try
        cList := TcpServer.Contexts.LockList;
        for Count := 0 to cList.Count - 1 do begin
          if TIdContext( cList[ Count ] ).Connection.Socket.Binding.PeerIP = MemoryData.FieldByName( 'IpAddr' ).AsString then begin
            ServerMsgMemo.Lines.Add( ':파일 보내기 - ' + TIdContext( cList[ Count ] ).Connection.Socket.Binding.PeerIP );
            TIdContext( cList[ Count ] ).Connection.IOHandler.WriteLn( ':파일 받기' );
            TIdContext( cList[ Count ] ).Connection.IOHandler.WriteLn( ExtractFileName( fName ) );
            TIdContext( cList[ Count ] ).Connection.IOHandler.WriteLn( IntToStr( fSize ) );
            TIdContext( cList[ Count ] ).Connection.IOHandler.Write( fStream );
          end;
        end;
      finally
        TcpServer.Contexts.UnlockList;
        ServerMsgMemo.Lines.Add( ':파일 보내기 완료!' );
      end;
    end;
  end;
  ServerMsgEdit.SetFocus;
end;

procedure TSocket5MainForm.ClientConnectButtonClick(Sender: TObject);
begin
  if TcpClient.Connected then TcpClientConnect( false )
                         else TcpClientConnect( true );
  ClientMsgEdit.SetFocus;
end;

procedure TSocket5MainForm.ClientEnterButtonClick(Sender: TObject);
var Msg: String;
begin
  Msg := ClientIdEdit.Text + ' > ' + ClientMsgEdit.Text;
  if TcpClient.Connected then TcpClient.IOHandler.WriteLn( Msg );
  ClientMsgEdit.Text := '';
  ClientMsgEdit.SetFocus;
end;

procedure TSocket5MainForm.TcpClientConnected(Sender: TObject);
begin
  ClientStatusMemo.Lines.Add( '연결 되었습니다.' );
end;

procedure TSocket5MainForm.TcpClientDisconnected(Sender: TObject);
begin
  TcpClient.Disconnect;
  ClientStatusMemo.Lines.Add( '연결을 끊었습니다.' );
end;

procedure TSocket5MainForm.ClientTimerTimer(Sender: TObject);
var Msg: String;
    fName: String;
    fSize: Integer;
    fStream: TIdStreamVCL;
begin
  try
    if TcpClient.IOHandler.Readable then begin
      WindowStateChecking;
      Msg := TcpClient.IOHandler.ReadLn;
      if Msg = ':파일 받기' then begin
        ClientTimer.Enabled := false;
        Msg := TcpClient.IOHandler.ReadLn;
        fName := DataDir + Msg;
        ClientMsgMemo.Lines.Add( ':파일명 - ' + Msg );
        Msg := TcpClient.IOHandler.ReadLn;
        fSize := StrToInt( Msg );
        ClientMsgMemo.Lines.Add( ':파일 크기 - ' + FormatFloat( '#,##0', fSize ) + '바이트' );
        Application.ProcessMessages;
        fStream := TIdStreamVCL.Create( TFileStream.Create( fName, fmCreate ), true );
        Application.ProcessMessages;
        try
          TcpClient.IOHandler.ReadStream( fStream, fSize, false );
        finally
          FreeAndNil( fStream );
          ClientMsgMemo.Lines.Add( ':파일 위치 - ' + fName );
          ClientMsgMemo.Lines.Add( ':파일 받기 완료!' );
        end;
        ClientTimer.Enabled := true;
      end else ClientMsgMemo.Lines.Add( Msg );
    end;
    if ClientRotate.Images.ActiveIndex in [ 7 ] then ClientRotate.Images.ActiveIndex := 0
                                                else ClientRotate.Images.ActiveIndex := ClientRotate.Images.ActiveIndex + 1;
  except
    ClientTimer.Enabled := false;
  end;
end;

procedure TSocket5MainForm.TcpClientStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: String);
begin
  if AStatus = hsDisConnected then TcpClientConnect( false );
end;

procedure TSocket5MainForm.DBGrid1DblClick(Sender: TObject);
var cList: TList;
    Count: Integer;
begin
  if MemoryData.Active and ( MemoryData.RecNo > 1 ) then begin
    try
      cList := TcpServer.Contexts.LockList;
      for Count := 0 to cList.Count - 1 do begin
        if TIdContext( cList[ Count ] ).Connection.Socket.Binding.PeerIP =
           MemoryData.FieldByName( 'IpAddr' ).AsString then TIdContext( cList[ Count ] ).Connection.Disconnect;
      end;
    finally
      TcpServer.Contexts.UnlockList;
    end;
  end;
end;

procedure TSocket5MainForm.CloseButton1Click(Sender: TObject);
begin
  Close;
end;

procedure TSocket5MainForm.ClientFileUpButtonClick(Sender: TObject);
var fStream: TIdStreamVCL;
begin
  if FileExists( ClientMsgEdit.Text ) and TcpClient.Connected then begin
    Application.ProcessMessages;
    fStream := TIdStreamVCL.Create( TFileStream.Create( ClientMsgEdit.Text, fmOpenRead + fmShareDenyNone ), true );
    Application.ProcessMessages;
    try
      TcpClient.IOHandler.WriteLn( ':파일 보내기' );
      ClientMsgMemo.Lines.Add( ':파일 보내기' );
      TcpClient.IOHandler.WriteLn( ExtractFileName( ClientMsgEdit.Text ) );
      ClientMsgMemo.Lines.Add( ':파일명 - ' + ExtractFileName( ClientMsgEdit.Text ) );
      TcpClient.IOHandler.WriteLn( IntToStr( fStream.Size ) );
      ClientMsgMemo.Lines.Add( ':파일 크기 - ' + FormatFloat( '#,##0', fStream.Size ) + '바이트' );
      TcpClient.IOHandler.Write( fStream );
    finally
      FreeAndNil( fStream );
      ClientMsgMemo.Lines.Add( ':파일 보내기 완료!' );
    end;
  end;
  ClientMsgEdit.SetFocus;
end;

procedure TSocket5MainForm.TcpClientWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Integer);
begin
  MaxValue := AWorkCountMax;
end;

procedure TSocket5MainForm.TcpClientWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Integer);
begin
  ClientlProgress.Position := Round( ( AWorkCount / MaxValue ) * 100 );
  Application.ProcessMessages;
end;

procedure TSocket5MainForm.TcpClientWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
  ClientlProgress.Position := 100;
end;

procedure TSocket5MainForm.ServerFileButtonClick(Sender: TObject);
begin
  if OpenDialog.Execute then begin
    if TControl( Sender ) = ServerFileButton then begin
      ServerMsgEdit.Text := OpenDialog.FileName;
      ServerMsgEdit.SetFocus;
    end else begin
      ClientMsgEdit.Text := OpenDialog.FileName;
      ClientMsgEdit.SetFocus;
    end;
  end;
end;

procedure TSocket5MainForm.ClientFileDownButtonClick(Sender: TObject);
begin
  TcpClient.IOHandler.WriteLn( ':파일 받기' );
  TcpClient.IOHandler.WriteLn( ClientMsgEdit.Text );
  ClientMsgEdit.SetFocus;
end;

end.

+ -

관련 글 리스트
312 [ Indy10 ] 기본 채팅 + 파일 보내기 + 파일 받기 이즈니 7760 2011/07/10
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.