BVB Source Codes

Delphi implements a simple P2P chat program Show UntClient.pas Source code

Return Download Delphi implements a simple P2P chat program: download UntClient.pas Source code - Download Delphi implements a simple P2P chat program Source code - Type:.pas
  1. unit UntClient;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient,
  8.   ScktComp, ExtCtrls;
  9.  
  10. type
  11.   TFrmClient = class(TForm)
  12.     ClientSocket: TClientSocket;
  13.     gb1: TGroupBox;
  14.     gb2: TGroupBox;
  15.     MemReadMsg: TMemo;
  16.     btnSend: TButton;
  17.     gb3: TGroupBox;
  18.     MemSendMsg: TMemo;
  19.     Connet: TButton;
  20.     GroupBox1: TGroupBox;
  21.     LeSrvName: TLabeledEdit;
  22.     LeSrvPort: TLabeledEdit;
  23.     procedure ConnetClick(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure LeSrvPortKeyPress(Sender: TObject; var Key: Char);
  26.     procedure LeSrvNameKeyPress(Sender: TObject; var Key: Char);
  27.     procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
  28.     procedure btnSendClick(Sender: TObject);
  29.   private
  30.     { Private declarations }
  31.   public
  32.     { Public declarations }
  33.   end;
  34.  
  35. var
  36.   FrmClient: TFrmClient;
  37.  
  38. implementation
  39.  
  40. {$R *.dfm}
  41.  
  42. procedure TFrmClient.ConnetClick(Sender: TObject);
  43. begin
  44.   Try
  45.     if Trim(LeSrvName.Text )='' then
  46.     begin
  47.       MessageDlg('请输入服务器IP地址或机器名称!',MtInformation,[mbyes],0);
  48.       exit;
  49.     end;
  50.  
  51.     if Trim(LeSrvPort.Text )='' then
  52.     begin
  53.       MessageDlg('请输入服务器端口号!',MtInformation,[mbyes],0);
  54.       exit;
  55.     end
  56.     else
  57.     if (Strtoint(Trim(LeSrvPort.Text))<0) or (Strtoint(Trim(LeSrvPort.Text))>999999) then
  58.     begin
  59.       MessageDlg('服务器端口号就在0-9999之间!',MtInformation,[mbyes],0);
  60.       exit;
  61.     end;
  62.     //ClientSocket.Active:=False;
  63.     //IdUdpClient1.Active:=False;
  64.     ClientSocket.Port:=Strtoint(Trim(LeSrvPort.Text));
  65.     ClientSocket.Host:=trim(LeSrvName.Text);
  66.     //IdUdpClient1.Host:=trim(LeSrvName.Text);
  67.     //IdUdpClient1.Port:=ClientSocket.Port;
  68.     //IdUdpClient1.Active:=true;
  69.     ClientSocket.Active:=true;
  70.     BtnSend.Enabled:=true;
  71.     self.Connet.Enabled:=false;
  72.   Except
  73.     MessageBox(0,'连接失败,请查看服务端是否有启动!','',MB_OK + MB_ICONWARNING);
  74.   end;
  75. end;
  76.  
  77. procedure TFrmClient.FormCreate(Sender: TObject);
  78. begin
  79.   BtnSend.Enabled:=false;
  80. end;
  81.  
  82. procedure TFrmClient.LeSrvPortKeyPress(Sender: TObject; var Key: Char);
  83. begin
  84.   if Not(Key in ['0'..'9',#10,#8,#13]) then
  85.   begin
  86.     Key:=#0;
  87.   end;
  88. end;
  89.  
  90. procedure TFrmClient.LeSrvNameKeyPress(Sender: TObject; var Key: Char);
  91. begin
  92.   if Not(Key in ['0'..'9',#8,#10,#13,#46]) then
  93.   begin
  94.     Key:=#0;
  95.   end;
  96. end;
  97.  
  98. procedure TFrmClient.ClientSocketRead(Sender: TObject;
  99.   Socket: TCustomWinSocket);
  100. var
  101.   Len:Word;
  102.   Buf:Array of Char;
  103.   Msg:String;
  104. begin
  105.   try
  106.     Socket.ReceiveBuf(Len, 2);
  107.     If Len <1 then Exit;
  108.     SetLength(Buf,Len);
  109.     Socket.ReceiveBuf(Buf[0],Len);
  110.     SetLength(Msg,Len);
  111.     Move(buf[0],Msg[1],Len);
  112.     MemReadMsg.Lines.Add(Msg);
  113.     MemReadMsg.Lines.Add(#13);
  114.   except
  115.   end;
  116. end;
  117.  
  118. procedure TFrmClient.btnSendClick(Sender: TObject);
  119. Var
  120.   Len:Word;
  121.   Buf:Array of Char;
  122.   Msg:String;
  123. Begin//前两个字节存放消息体内容长度)
  124.   Msg:=Trim(MemSendMsg.Text);
  125.   Len:=length(Msg);
  126.   SetLength(Buf,Len+2);
  127.   Move(len,Buf[0],2);
  128.   Move(Msg[1],Buf[2],Len);
  129.   ShowMessage(inttostr(high(Buf)));
  130.   ClientSocket.Socket.SendBuf(Buf[0],Len+2);
  131.   MemReadMsg.Lines.Add('自己:');
  132.   MemReadMsg.Lines.Add(#13);
  133.   MemReadMsg.Lines.Add('  '+Msg);
  134.   MemReadMsg.Lines.Add(#13);
  135. end;
  136.  
  137. end.
  138.  
downloadUntClient.pas Source code - Download Delphi implements a simple P2P chat program Source code
Related Source Codes/Software:
Delphi get system process port - Through a Windows dynamic link library system prov... 2012-02-18
Graduation: Delphi wages management system - A graduate student's graduation project of Delphi ... 2012-02-18
Simple Delphi calculator - Implements functions similar to the simple electro... 2012-02-18
RM files addresses a security check DELPHI implementation - When you're downloading movies in RM format, there... 2012-02-18
Delphi version of the Access database compression program v1.0 - We normally encounter useless data exists in the A... 2012-02-18
Delphi uses the MMS protocol to download streaming media file - Delphi uses the MMS protocol to download streaming... 2012-02-18
Delphi kanji literacy software full version - Delphi pupils ' Chinese character recognition soft... 2012-02-18
Delphi examples of using the Image control to display and draw text - The Delphi examples through the use of an Image co... 2012-02-18
Delphi screen touch cash register system source program - From the perspective of directory structure, this ... 2012-02-18
Children's mathematical arithmetic software in Delphi source code - The count of pupils is perfect according to the on... 2012-02-18
Delphi car rental company business management system - A car rental company's business management systems... 2012-02-18
Delphi major revision of the human resources management system source code - RedHR owned a large OFFICE of human resources mana... 2012-02-18
DELPHI pawn industry management system source code - Pawn shop of an integrated business management sys... 2012-02-18
Cool interface Delphi chat software source code - Interface is really cool? This is a small LAN chat... 2012-02-18
Delphi shield Ctrl+Alt+Del reqijian method - Real shielding theory of Delphi is this: using a r... 2012-02-18
Classic RPG games with Delphi source code - The actual and Diablo game or scene is similar in ... 2012-02-18
35 Delphi7 Indy instance package - Application example demonstrates a lot of Indy Dem... 2012-02-18
Delphi transportation company business management system - A Delphi transportation company business managemen... 2012-02-18
Using Delphi to achieve control of the camera - This procedure describes how to use DELPHI AVICAP3... 2012-02-18
Cell of the report component for Delphi example source program - UFIDA table company developed a report component, ... 2012-02-18

 Back to top