我的学习生涯(Delphi篇) - 02

    也许大家都用上了微信。

    这得感谢微信之父---张晓龙先生。我们程序员知道这个事情,应该是张先生开发出了Foxmail。

    Foxmail就是使用Delphi开发的!

   -------------------------------------------------------------------------------------------------美丽分割线---------------------------

    年代:2005

   文件:my0524.7z

   效果如下图:

我的学习生涯(Delphi篇) - 02_第1张图片

我的学习生涯(Delphi篇) - 02_第2张图片


Userver.pas

unit Userver;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ScktComp, StdCtrls, Buttons, Mask;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    ClientSocket1: TClientSocket;
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    Edit3: TEdit;
    Label3: TLabel;
    BitBtn1: TBitBtn;
    Button2: TButton;
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure ClientSocket1Disconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure FormCreate(Sender: TObject);
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
    procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure ClientSocket1Connecting(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure Edit3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BitBtn1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

  mycomputer:string;
  flags1:integer;
implementation

{$R *.dfm}

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  Memo2.Lines.Add(Socket.ReceiveText);
end;

procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  ClientSocket1.Active:=False;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.Caption:='CLIENT';
  Application.Title:='WINSOCKET32通信-CLIENT端';
  flags1:=0;
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
//var
//  i4:integer;
begin
  if key=#10 then
  begin
    if Length(mycomputer)<4 then
    begin
      ShowMessage(IntToStr(length(mycomputer)));
      Exit;
    end;
    if Edit3.Enabled then
    begin
      //i4:=Memo1.Lines.Count;
      try
        ClientSocket1.Socket.SendText(mycomputer+Memo1.Lines.Text);
        Memo2.Lines.Add(mycomputer+Memo1.Lines.Text);
        //Memo1.Lines.Clear;
        Memo1.Lines.Text:='';
      except
        Application.MessageBox('发送数据失败,请重新发送!','提示',MB_OK);
        Exit;
      end;
    end
    else
    begin
      Memo1.Color:=clBtnFace;
      Memo1.ReadOnly:=True;
    end;
  end;
end;

procedure TForm1.ClientSocket1Error(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
  try
    //Application.ProcessMessages;
    if ErrorCode=10061 then
    begin
      //Socket.OnErrorEvent(nil,Socket,ErrorEvent,10061);
      Application.MessageBox('服务器没有启动!','提示',MB_OK);
      Exit;
    end;
    if ErrorCode=10065 then
    begin
       //Application.MessageBox('网络断开,无法使用!','提示',MB_OK);

       Exit;
    end;
  except
     ESocketError.Create('网络断开,无法使用!');
     Exit;
  end;
end;

procedure TForm1.ClientSocket1Connecting(Sender: TObject;
  Socket: TCustomWinSocket);
begin
   Application.MessageBox('连接服务器成功!','提示',MB_OK);
   Edit2.Color:=clBtnFace;
   Edit2.Font.Color:=clRed;
   Edit2.ReadOnly:=True;
   Exit;
end;

procedure TForm1.Edit3MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if flags1=0 then
  begin
    flags1:=1;
    Edit3.Text:='';
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if Edit3.Text<>'' then
  begin
    mycomputer:=Edit3.Text+':';
    Edit3.Color:=clBtnFace;
    Edit3.ReadOnly:=true;
  end
  else
  begin
    Application.MessageBox('请输入昵称!','提示',MB_OK);
    Exit;
  end;

  if (Edit1.Text<>'')and(Edit2.Text<>'')then
  begin
    try
      ClientSocket1.Active:=False;
      //ClientSocket1.Address:=Edit1.Text;
      ClientSocket1.Host:=Edit1.Text;
      ClientSocket1.Port:=StrToInt(Edit2.Text);
      ClientSocket1.Active:=True;
    except
      //Application.MessageBox('服务器没有启动!','提示',MB_OK)
    end;

  end
  
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Length(mycomputer)<4 then
  begin
    ShowMessage(IntToStr(length(mycomputer)));
    Exit;
  end;
  if Edit3.Enabled then
  begin
    //ClientSocket1.Socket.SendText(mycomputer+Memo1.Lines.Strings[i4-1]);
     try
        ClientSocket1.Socket.SendText(mycomputer+Memo1.Lines.Text);
        Memo2.Lines.Add(mycomputer+Memo1.Lines.Text);
        Memo1.Lines.Clear;
      except
        Application.MessageBox('发送数据失败,请重新发送!','提示',MB_OK);
        Exit;
      end;
  end
  else
  begin
    Memo1.Color:=clBtnFace;
    Memo1.ReadOnly:=True;
  end;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  ClientSocket1.Active:=False;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ClientSocket1.Close;
  Action:=caFree;
end;

end.

Uclient.pas

unit Uclient;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    ServerSocket1: TServerSocket;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ServerSocket1Accept(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Edit1.Text<>'' then
  begin
    ServerSocket1.Port:=StrToInt(Edit1.Text);
    ServerSocket1.Active:=True;
    Memo1.ReadOnly:=False;
    Application.MessageBox('服务器建立成功!','提示',MB_OK);
    Edit1.Font.Color:=clRed;
    Edit1.Color:=clBtnFace;
    Edit1.ReadOnly:=True;

  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ServerSocket1.Close;
end;

procedure TForm1.ServerSocket1Accept(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  ServerSocket1.Active:=True;
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  Memo2.Lines.Add(Socket.ReceiveText);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
//var
//  i5:integer;
begin
  if key=#10 then
  begin
    //i5:=Memo1.Lines.Count;
    //ServerSocket1.Socket.Connections[0].SendText('xiao bin:'+Memo1.Lines.Strings[i5-1]);
    ServerSocket1.Socket.Connections[0].SendText('xiao bin:'+Memo1.Lines.Text);
    Memo2.Lines.Append('xiao bin:'+Memo1.Lines.Text);
    //Memo1.Lines.Clear;
    Memo1.Lines.Text:=' ';
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.Caption:='SERVER';
  Application.Title:='WINSOCKET32通信-SERVER端'
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  ServerSocket1.Close;
end;

end.

利用Delphi控件完成Winsocket通信。



你可能感兴趣的:(我的学习生涯(Delphi篇) - 02)