uServerMain.pas
资源名称:print.rar [点击查看]
上传用户:cnjnqp
上传日期:2022-05-02
资源大小:194k
文件大小:6k
源码类别:
打印编程
开发平台:
Delphi
- unit uServerMain;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ScktComp, ToolWin, ComCtrls, ExtCtrls, StdCtrls, Buttons, Registry, Spin;
- 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;
- StatusBar: TStatusBar;
- Panel1: TPanel;
- GroupBox1: TGroupBox;
- Memo1: TMemo;
- 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;
- procedure ZebraPrint(data: string); stdcall; external 'Zebra105SL.dll';
- var
- frmServerMain: TfrmServerMain;
- implementation
- {$R *.dfm}
- //split string
- function SplitString(const Source, ch: string): TStringList;
- var
- temp: string;
- i: Integer;
- begin
- Result := TStringList.Create;
- //假如是空自符串则返回空列表
- if trim(Source) = ''
- then exit;
- temp := Source;
- i := pos(ch, Source);
- while i <> 0 do
- begin
- Result.add(copy(temp, 0, i - 1));
- Delete(temp, 1, i);
- i := pos(ch, temp);
- end;
- Result.add(temp);
- end;
- //打开套接字连接,并使套接字进入监听状态
- 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
- arr: TStringList;
- i: integer;
- tmp: string;
- begin
- tmp := Socket.ReceiveText;
- Memo1.Text := tmp;
- arr := SplitString(tmp, '|');
- //ShowMessage(inttostr(arr.count));
- if arr.count>1 then
- begin
- for i := 0 to arr.count - 1 do
- ZebraPrint(arr[i]);
- //ShowMessage(arr[i]);
- 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);
- var
- registry: TRegistry;
- begin
- sessions := 0;
- ServerSocket.Port := 1025;
- ServerSocket.Open;
- //运行自行注册
- registry:=TRegistry.Create;
- registry.RootKey:= HKEY_LOCAL_MACHINE; //HKEY_CURRENT_USER;
- registry.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',true);
- registry.WriteString('ZerbraPrinter', Application.ExeName);
- registry.CloseKey;
- 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.