uClientMain.pas
资源名称:print.rar [点击查看]
上传用户:cnjnqp
上传日期:2022-05-02
资源大小:194k
文件大小:4k
源码类别:
打印编程
开发平台:
Delphi
- unit uClientMain;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ScktComp, ComCtrls, ToolWin, ExtCtrls, StdCtrls, Buttons, Spin;
- const
- SocketHost = '172.16.1.6'; //服务器端地址
- type
- TfrmClientMain = class(TForm)
- ToolBar1: TToolBar;
- tbConnected: TToolButton;
- ToolButton1: TToolButton;
- tbSend: TToolButton;
- ToolButton2: TToolButton;
- tbClose: TToolButton;
- ToolButton3: TToolButton;
- tbDisconnected: TToolButton;
- ClientSocket: TClientSocket;
- StatusBar: TStatusBar;
- Panel1: TPanel;
- Label2: TLabel;
- Edit3: TEdit;
- Label1: TLabel;
- Label3: TLabel;
- Edit1: TEdit;
- Button1: TButton;
- btnSend: TBitBtn;
- GroupBox1: TGroupBox;
- Memo1: TMemo;
- SpinEdit1: TSpinEdit;
- procedure tbConnectedClick(Sender: TObject);
- procedure tbDisconnectedClick(Sender: TObject);
- procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
- procedure tbSendClick(Sender: TObject);
- procedure tbCloseClick(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure ClientSocketConnect(Sender: TObject;
- Socket: TCustomWinSocket);
- procedure ClientSocketConnecting(Sender: TObject;
- Socket: TCustomWinSocket);
- procedure ClientSocketDisconnect(Sender: TObject;
- Socket: TCustomWinSocket);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
- ErrorEvent: TErrorEvent; var ErrorCode: Integer);
- procedure Button1Click(Sender: TObject);
- procedure Edit1KeyUp(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- frmClientMain: TfrmClientMain;
- implementation
- {$R *.dfm}
- //打开套接字连接
- procedure TfrmClientMain.tbConnectedClick(Sender: TObject);
- begin
- ClientSocket.Open ;
- end;
- //关闭套接字连接
- procedure TfrmClientMain.tbDisconnectedClick(Sender: TObject);
- begin
- ClientSocket.Close;
- end;
- //接受服务器端的回复
- procedure TfrmClientMain.ClientSocketRead(Sender: TObject;
- Socket: TCustomWinSocket);
- begin
- Memo1.Lines.Add(Socket.ReceiveText);
- end;
- //发送信息到服务器端
- procedure TfrmClientMain.tbSendClick(Sender: TObject);
- begin
- ClientSocket.Socket.SendText(Edit1.Text);
- end;
- procedure TfrmClientMain.tbCloseClick(Sender: TObject);
- begin
- Close;
- end;
- //设置要连接的服务器端地址
- procedure TfrmClientMain.FormShow(Sender: TObject);
- begin
- ClientSocket.Host := SocketHost;
- end;
- //已经连接到服务器端
- procedure TfrmClientMain.ClientSocketConnect(Sender: TObject;
- Socket: TCustomWinSocket);
- begin
- tbSend.Enabled := True;
- tbDisconnected.Enabled :=True;
- btnSend.Enabled := True;
- StatusBar.Panels[0].Text := '已经连接到 '+ Socket.RemoteHost ;
- end;
- //正在连接到服务器端
- procedure TfrmClientMain.ClientSocketConnecting(Sender: TObject;
- Socket: TCustomWinSocket);
- begin
- StatusBar.Panels[0].Text := '正在连接到服务器... ' ;
- end;
- //当断开与服务器端的连接时发生
- procedure TfrmClientMain.ClientSocketDisconnect(Sender: TObject;
- Socket: TCustomWinSocket);
- begin
- tbSend.Enabled := False;
- btnSend.Enabled := False;
- tbDisconnected.Enabled := False;
- StatusBar.Panels[0].Text := '已经断开与 '+ Socket.RemoteHost +' 的连接';
- end;
- procedure TfrmClientMain.FormClose(Sender: TObject;
- var Action: TCloseAction);
- begin
- ClientSocket.Close ;
- end;
- //当与服务器端的连接发生错误时
- procedure TfrmClientMain.ClientSocketError(Sender: TObject;
- Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
- var ErrorCode: Integer);
- begin
- StatusBar.Panels[0].Text := '与服务器端的连接发生错误';
- ErrorCode := 0;
- end;
- procedure TfrmClientMain.Button1Click(Sender: TObject);
- begin
- ClientSocket.Host :=trim(Edit3.Text) ;
- ClientSocket.Port :=SpinEdit1.Value ;
- ToolBar1.Enabled :=true;
- end;
- procedure TfrmClientMain.Edit1KeyUp(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- if (Key=13) and (trim(Edit1.Text)<>'') then ClientSocket.Socket.SendText(Edit1.Text);
- end;
- end.