科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件利用Delphi编写Socket通信程序

利用Delphi编写Socket通信程序

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文将本人总结出来的TServerSocket和TClientSocket两个组件的基本用法写出来,希望与您分享。

作者:AustinLei 来源:天极网 2007年10月31日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
程序源代码:

  · 服务器端源码(uServerMain.pas):

unit uServerMain;
interface
 uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ScktComp, ToolWin, ComCtrls, ExtCtrls, StdCtrls, Buttons;
 const
  CMax=10; //客户端最大连接数
 type
  client_record=record
  CHandle: integer; //客户端套接字句柄
  CSocket:TCustomWinSocket; //客户端套接字
  CName:string; //客户端计算机名称
  CAddress:string; //客户端计算机IP地址
  CUsed: boolean; //客户端联机标志
 end;
 type
  TfrmServerMain = class(TForm)
  ServerSocket: TServerSocket;
  ControlBar1: TControlBar;
  ToolBar1: TToolBar;
  tbConnect: TToolButton;
  tbClose: TToolButton;
  tbDisconnected: TToolButton;
  Edit1: TEdit;
  Memo1: TMemo;
  StatusBar: TStatusBar;
  procedure tbConnectClick(Sender: TObject);
  procedure tbDisconnectedClick(Sender: TObject);
  procedure ServerSocketClientRead(Sender: TObject;Socket: TCustomWinSocket);
  procedure ServerSocketListen(Sender: TObject;Socket: TCustomWinSocket);
  procedure ServerSocketClientConnect(Sender: TObject;Socket: TCustomWinSocket);
  procedure ServerSocketClientDisconnect(Sender: TObject;Socket: TCustomWinSocket);
  procedure tbCloseClick(Sender: TObject);
  procedure FormCreate(Sender: TObject);
  procedure FormClose(Sender: TObject; var Action: TCloseAction);
  procedure ServerSocketGetSocket(Sender: TObject; Socket: Integer;
  var ClientSocket: TServerClientWinSocket);
  procedure ServerSocketClientError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
  private
  {
   Private declarations
  }
  public
  {
   Public declarations
  }
  session: array[0..CMax] of client_record; //客户端连接数组
  Sessions: integer; //客户端连接数
 end;
 var
  frmServerMain: TfrmServerMain;
  implementation
  {$R *.DFM}
  //打开套接字连接,并使套接字进入监听状态
  procedure TfrmServerMain.tbConnectClick(Sender: TObject);
 begin
  ServerSocket.Open ;
 end;
 //关闭套接字连接,不再监听客户端的请求
 procedure TfrmServerMain.tbDisconnectedClick(Sender: TObject);
 begin
  ServerSocket.Close;
  StatusBar.Panels[0].Text :='服务器套接字连接已经关闭,无法接受客户端的连接请求.';
 end;
 //从客户端读取信息
 procedure TfrmServerMain.ServerSocketClientRead(Sender: TObject;Socket: TCustomWinSocket);
 var
  i:integer;
 begin
  //将从客户端读取的信息添加到Memo1中
  Memo1.Lines.Add(Socket.ReceiveText);
  for i:=0 to sessions do
  begin
   //取得匹配的客户端
   if session[i].CHandle = Socket.SocketHandle then
   begin
    session[i].CSocket.SendText('回复客户端'+session[i].CAddress+' ==> '+Edit1.Text);
   end;
  end;
 end;
 //服务器端套接字进入监听状态,以便监听客户端的连接
 procedure TfrmServerMain.ServerSocketListen(Sender: TObject;Socket: TCustomWinSocket);
 begin
  StatusBar.Panels[0].Text :='等待客户端连接...';
 end;
 //当客户端连接到服务器端以后
 procedure TfrmServerMain.ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
 var
  i,j:integer;
 begin
  j:=-1;
  for i:=0 to sessions do
  begin
   //在原有的客户端连接数组中有中断的客户端连接
   if not session[i].CUsed then
   begin
    session[i].CHandle := Socket.SocketHandle ;//客户端套接字句柄
    session[i].CSocket := Socket; //客户端套接字
    session[i].CName := Socket.RemoteHost ; //客户端计算机名称
    session[i].CAddress := Socket.RemoteAddress ;//客户端计算机IP
    session[i].CUsed := True; //连接数组当前位置已经占用
    Break;
   end;
  j:=i;
 end;
 if j=sessions then
 begin
  inc(sessions);
  session[j].CHandle := Socket.SocketHandle ;
  session[j].CSocket := Socket;
  session[j].CName := Socket.RemoteHost ;
  session[j].CAddress := Socket.RemoteAddress ;
  session[j].CUsed := True;
 end;
 StatusBar.Panels[0].Text := '客户端 '+Socket.RemoteHost + ' 已经连接';
end;
//当客户端断开连接时
procedure TfrmServerMain.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
 i:integer;
begin
 for i:=0 to sessions do
 begin
  if session[i].CHandle =Socket.SocketHandle then
  begin
   session[i].CHandle :=0;
   session[i].CUsed := False;
   Break;
  end;
 end;
 StatusBar.Panels[0].Text :='客户端 '+Socket.RemoteHost + ' 已经断开';
end;
//关闭窗口
procedure TfrmServerMain.tbCloseClick(Sender: TObject);
begin
 Close;
end;
procedure TfrmServerMain.FormCreate(Sender: TObject);
begin
 sessions := 0;
end;
procedure TfrmServerMain.FormClose(Sender: TObject;var Action: TCloseAction);
begin
 ServerSocket.Close ;
end;
//当客户端正在与服务器端连接时
procedure TfrmServerMain.ServerSocketGetSocket(Sender: TObject;
Socket: Integer; var ClientSocket: TServerClientWinSocket);
begin
 StatusBar.Panels[0].Text :='客户端正在连接...';
end;
//客户端发生错误
procedure TfrmServerMain.ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
 StatusBar.Panels[0].Text :='客户端'+Socket.RemoteHost +'发生错误!';
 ErrorCode := 0;
end;
end.
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章