找传奇、传世资源到传世资源站!

delphi多客户端连接服务器(ServerSocket)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

【例子介绍】

【相关图片】

from clipboard

【源码结构】

unit SerMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls, ScktComp, ComCtrls;
const
  Cmax=10;
type
 client_record=record
    CHandle:Integer;
    CSocket:TCustomWinSocket;
    CName:String;
    CAddress:string;
    UseName:string;
    CUsed:boolean;
 end;
type
  TForm1 = class(TForm)
    StatusBar1: TStatusBar;
    ServerSocket1: TServerSocket;
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    Edit1: TEdit;
    Button1: TButton;
    ListBox1: TListBox;
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ServerSocket1Listen(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1ClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1ClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure N4Click(Sender: TObject);
    procedure ServerSocket1GetSocket(Sender: TObject; Socket: Integer;
      var ClientSocket: TServerClientWinSocket);
    procedure ServerSocket1ClientError(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
    session: array[0..CMax] of client_record; //客户端连接数组
    Sessions: integer; //客户端连接数
    cchandle:Thandle;
  public
    { Public declarations }

  end;
var
  Form1: TForm1;
  NNames:string;
implementation
uses HUtil32;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
begin
 if nnames='' then exit;
 
for i := 0 to sessions do
 begin
   if session[i].UseName=NNames then
     session[i].CSocket.SendText('回复客户端:' session[i].usename ':' Edit1.Text);
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
Var
  s:string;
begin
  sessions:=0;
   s:=formatDateTime('[YYYY-MM-DD hh:mm:ss] ',now);
 listbox1.Items.Add(s '欢迎使用清风网关');
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
  i:Integer;
  s:string;
begin
   I := listbox1.ItemIndex;
   s:=listbox1.Items.Strings[i];
   s:=GetValidStr3(s,nnames,[':']);
end;

procedure TForm1.N2Click(Sender: TObject);
var
  s:string;
begin
  ServerSocket1.Port:=7000;
  ServerSocket1.Open;
  s:=formatDateTime('[YYYY-MM-DD hh:mm:ss] ',now);
  listbox1.Items.Add(s '启动监听服务成功....');
  StatusBar1.Panels[0].Text:='  正在监听......';
  
end;

procedure TForm1.N3Click(Sender: TObject);
var
  s:string;
begin
  ServerSocket1.Close;
  s:=formatDateTime('[YYYY-MM-DD hh:mm:ss] ',now);
  listbox1.Items.Add(s '网关以关闭....');
  StatusBar1.Panels[0].Text:='网关连接以断开......';
end;

procedure TForm1.N4Click(Sender: TObject);
begin
  close;
end;

procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
  i,j:Integer;
begin
  j:=-1;
   inc(sessions);
  for i := 0 to sessions-1 do
  begin
    if not session[i].CUsed then
     begin
       if session[i].CHandle<>0 then
          continue;
       session[i].CHandle:=Socket.Handle;
       session[i].CSocket:=socket;
       session[i].CName:=Socket.RemoteHost;
       session[i].CAddress:=Socket.RemoteAddress;
       session[i].CUsed:=true;
       Break;
     end;
     j:=i;
  end;
end;

procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
  i:Integer;
  s:string;
begin
 if sessions<>0 then begin

  for I := 0 to sessions-1 do
   begin
     if session[i].CHandle=socket.Handle  then
      begin
        session[i].CHandle:=0;
        session[i].CUsed:=false;
        s:=session[i].UseName;
        session[i].UseName:='';
        session[i].CAddress:='';
        session[i].CSocket:=nil;
        sessions:=sessions-1;
        break;
      end;
   end;
 end;
 statusbar1.Panels[0].Text:='客户' s '以断开';
 s:='';
end;

procedure TForm1.ServerSocket1ClientError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  StatusBar1.Panels[0].Text:='客户端' Socket.RemoteHost '连接发生错误';
  ErrorCode:=0;
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  i:Integer;
  ss:Tstrings;
  s,stt,Name,datestr:string;
begin
    stt:= Socket.ReceiveText;
    datestr:=GetValidStr3(stt,name,[':']);
  if sessions<>0 then begin
   for I := 0 to sessions-1 do
   begin
     if session[i].CHandle=socket.Handle  then
      begin
        session[i].UseName:=name;
        break;
      end;
   end;
  end;
  listbox1.Items.Add(name ':' datestr);
end;

procedure TForm1.ServerSocket1GetSocket(Sender: TObject; Socket: Integer;
  var ClientSocket: TServerClientWinSocket);
begin
  Statusbar1.Panels[0].Text:='有客户端正在连接.............';
end;

procedure TForm1.ServerSocket1Listen(Sender: TObject; Socket: TCustomWinSocket);
begin
  StatusBar1.Panels[0].Text:='有客户端正在连接中..........';
end;

end.

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复