BVB Source Codes

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

Return Download Delphi implements a simple P2P chat program: download UntSrv.pas Source code - Download Delphi implements a simple P2P chat program Source code - Type:.pas
  1. unit UntSrv;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, ScktComp;
  8.  
  9. type
  10.   TFrmSrv = class(TForm)
  11.     SrvSocket: TServerSocket;
  12.     gp1: TGroupBox;
  13.     MemClientLink: TMemo;
  14.     gb2: TGroupBox;
  15.     gb3: TGroupBox;
  16.     MemSendMsg: TMemo;
  17.     btnSend: TButton;
  18.     gp4: TGroupBox;
  19.     MemClientMsg: TMemo;
  20.     SrvSocket2: TServerSocket;
  21.     SrvSocket1: TServerSocket;
  22.     procedure SrvSocketClientConnect(Sender: TObject;
  23.       Socket: TCustomWinSocket);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure SrvSocketListen(Sender: TObject; Socket: TCustomWinSocket);
  26.     procedure SrvSocketClientRead(Sender: TObject;
  27.       Socket: TCustomWinSocket);
  28.     procedure SrvSocketClientDisconnect(Sender: TObject;
  29.       Socket: TCustomWinSocket);
  30.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  31.     procedure btnSendClick(Sender: TObject);
  32.   private
  33.     { Private declarations }
  34.   public
  35.     { Public declarations }
  36.   end;
  37.  
  38. var
  39.   FrmSrv: TFrmSrv;
  40.   ClientHost:TList;
  41. implementation
  42.  
  43. {$R *.dfm}
  44. //监听客户端
  45. procedure TFrmSrv.SrvSocketClientConnect(Sender: TObject;
  46.   Socket: TCustomWinSocket);
  47. begin
  48.   ClientHost.Add(Socket);
  49.   MemClientLink.Lines.Add(Socket.RemoteHost+':'+Socket.RemoteAddress);
  50. end;
  51.  
  52. procedure TFrmSrv.FormCreate(Sender: TObject);
  53. begin
  54.   ClientHost:=TList.Create;
  55.   MemClientLink.ReadOnly:=True;
  56. end;
  57. //程序在进行启动连接时
  58. procedure TFrmSrv.SrvSocketListen(Sender: TObject;
  59.   Socket: TCustomWinSocket);
  60. begin
  61.   MemClientLink.Lines.Add('Link...'+#13+Socket.RemoteHost+Socket.RemoteAddress);
  62. end;
  63. //接收客户端信息
  64. procedure TFrmSrv.SrvSocketClientRead(Sender: TObject;
  65.   Socket: TCustomWinSocket);
  66. Var
  67.   Len : Word;
  68.   Buf : Array of Char;
  69.   Msg : String;
  70. begin //当收到一次信息后,还会侦听一次
  71.   try
  72.     Socket.ReceiveBuf(Len,2); //前两个字节存放消息体内容长度
  73.     if Len < 1 then Exit;
  74.     SetLength(Buf,Len);
  75.     Socket.ReceiveBuf(Buf[0],len);
  76.     SetLength(Msg, len);
  77.     Move(Buf[0], Msg[1], Len);
  78.     MemClientMsg.Lines.Add(Socket.LocalHost+':'+Msg);
  79.     MemClientMsg.Lines.Add(#13);
  80.   except
  81.  
  82.   end;
  83. end;
  84. //释放客户端
  85. procedure TFrmSrv.SrvSocketClientDisconnect(Sender: TObject;
  86.   Socket: TCustomWinSocket);
  87. begin
  88.   ClientHost.Remove(Socket);
  89. end;
  90. //关闭窗体
  91. procedure TFrmSrv.FormClose(Sender: TObject; var Action: TCloseAction);
  92. Var
  93.   I : Integer;
  94. begin
  95.   for i := 0 to ClientHost.Count-1 do ClientHost.Delete(i);
  96.   FreeAndNil(ClientHost);
  97. end;
  98.  
  99. procedure TFrmSrv.btnSendClick(Sender: TObject);
  100. Var
  101.   Len,I : Word;
  102.   Buf : Array of Char;
  103.   Msg : String;
  104. begin
  105.   if ClientHost.Count<=0 then
  106.   begin
  107.     MessageDlg('对不起,当前没有一台客户端程序!',MtWarning,[Mbyes],0);
  108.     Exit;    
  109.   end;
  110.  
  111.   Msg:=Trim(MemSendMsg.Text);
  112.   Len:=Length(Msg);
  113.   SetLength(Buf,Len+2);   //设置buf长度,前两个字节存放消息体内容长度
  114.   Move(Len,Buf[0],2);     //把消息体内容长度放入前两个字节
  115.   Move(Msg[1],Buf[2],Len);//把消息体内容放入包中,从第三个字节开始存放
  116.   for i := 0 to ClientHost.Count-1 do
  117.   begin
  118.     try
  119.       TCustomWinSocket(ClientHost[i]).SendBuf(Buf[0],Len+2);
  120.     except
  121.       TCustomWinSocket(ClientHost[i]).Close;
  122.       ClientHost.Remove(ClientHost[i]);
  123.     end;
  124.   end;
  125. end;
  126.  
  127. end.
  128.  
downloadUntSrv.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