利用Delphi编写Socket通信程序

2016-08-22 10:16:27来源:http://525927.blog.51cto.com/515927/121295作者:xjfreeman人点击


利用Delphi编写Socket通信程序 笔者在工作中遇到对局域网中各工作站与服务器之间进行Socket通信的问题。现在将本人总结出来的TServerSocket和TClientSocket两个组件的基本用法写出来,希望与您分享。
ClientSocket组件为客户端组件。它是通信的请求方,也就是说,它是主动地与服务器端建立连接。
ServerSocket组件为服务器端组件。它是通信的响应方,也就是说,它的动作是监听以及被动接受客户端的连接请求,并对请求进行回复。
ServerSocket组件可以同时接受一个或多个ClientSocket组件的连接请求,并与每个ClientSocket组件建立单独的连接,进行单独的通信。因此,一个服务器端可以为多个客户端服务。
设计思路

本例包括一个服务器端程序和一个客户端程序。客户端程序可以放到多个计算机上运行,同时与服务器端进行连接通信。
本例的重点,一是演示客户端与服务器端如何通信;二是当有多个客户端同时连接到服务
器端时,服务器端如何识别每个客户端,并对请求给出相应的回复。为了保证一个客户端断开连接时不影响其它客户端与服务器端的通信,同时保证服务器端能够正
确回复客户端的请求,在本例中声明了一个记录类型:



typeclient_record=record CHandle: integer;//客户端套接字句柄 CSocket:TCustomWinSocket;//客户端套接字 CName:string;//客户端计算机名称 CAddress:string; //客户端计算机IP地址 CUsed: boolean; //客户端联机标志end;



利用这个记录类型数据保存客户端的信息,同时保存当前客户端的连接状态。其
中,CHandle保存客户端套接字句柄,以便准确定位每个与服务器端保持连接的客户端;Csocket保存客户端套接字,通过它可以对客户端进行回复。
Cused记录当前客户端是否与服务器端保持连接。
下面对组件ServerSocket和ClientSocket的属性设置简单说明。
ServerSocket的属性:
· Port,是通信的端口,必须设置。在本例中设置为1025;
· ServerTypt,服务器端读写信息类型,设置为stNonBlocking表示异步读写信息,本例中采用这种方式。
· ThreadCacheSize,客户端的最大连接数,就是服务器端最多允许多少客户端同时连接。本例采用默认值10。
其它属性采用默认设置即可。
ClientSocket的属性:
· Port,是通信的端口,必须与服务器端的设置相同。在本例中设置为1025;
· ClientType,客户端读写信息类型,应该与服务器端的设置相同,为stNonBlocking表示异步读写信息。
· Host,客户端要连接的服务器的IP地址。必须设置,当然也可以在代码中动态设置。
其它属性采用默认设置即可。
程序源代码:
· 服务器端源码(uServerMain.pas):



unit uServerMain;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ScktComp, ToolWin, ComCtrls, ExtCtrls, StdCtrls, Buttons;constCMax=10;//客户端最大连接数typeclient_record=record CHandle: integer;//客户端套接字句柄 CSocket:TCustomWinSocket;//客户端套接字 CName:string;//客户端计算机名称 CAddress:string; //客户端计算机IP地址 CUsed: boolean; //客户端联机标志end;typeTfrmServerMain = 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;varfrmServerMain: TfrmServerMain;implementation{$R *.DFM}//打开套接字连接,并使套接字进入监听状态procedure TfrmServerMain.tbConnectClick(Sender: TObject);beginServerSocket.Open ;end;//关闭套接字连接,不再监听客户端的请求procedure TfrmServerMain.tbDisconnectedClick(Sender: TObject);beginServerSocket.Close;StatusBar.Panels[0].Text :='服务器套接字连接已经关闭,无法接受客户端的连接请求.';end;//从客户端读取信息procedure TfrmServerMain.ServerSocketClientRead(Sender: TObject;Socket: TCustomWinSocket);vari:integer;begin//将从客户端读取的信息添加到Memo1中Memo1.Lines.Add(Socket.ReceiveText);for i:=0 to sessions dobegin//取得匹配的客户端if session[i].CHandle = Socket.SocketHandle thenbeginsession[i].CSocket.SendText('回复客户端'+session[i].CAddress+' ==> '+Edit1.Text); end;end;end;//服务器端套接字进入监听状态,以便监听客户端的连接procedure TfrmServerMain.ServerSocketListen(Sender: TObject;Socket: TCustomWinSocket);beginStatusBar.Panels[0].Text :='等待客户端连接...';end;//当客户端连接到服务器端以后procedure TfrmServerMain.ServerSocketClientConnect(Sender: TObject;Socket: TCustomWinSocket);vari,j:integer;beginj:=-1;for i:=0 to sessions dobegin//在原有的客户端连接数组中有中断的客户端连接if not session[i].CUsed thenbeginsession[i].CHandle := Socket.SocketHandle ;//客户端套接字句柄session[i].CSocket := Socket;//客户端套接字session[i].CName := Socket.RemoteHost ;//客户端计算机名称session[i].CAddress := Socket.RemoteAddress ;//客户端计算机IPsession[i].CUsed := True; //连接数组当前位置已经占用Break;end;j:=i;end;if j=sessions thenbegininc(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);vari:integer;beginfor i:=0 to sessions dobeginif session[i].CHandle =Socket.SocketHandle thenbeginsession[i].CHandle :=0;session[i].CUsed := False;Break;end;end;StatusBar.Panels[0].Text :='客户端 '+Socket.RemoteHost + ' 已经断开';end;//关闭窗口procedure TfrmServerMain.tbCloseClick(Sender: TObject);beginClose;end;procedure TfrmServerMain.FormCreate(Sender: TObject);beginsessions := 0;end;procedure TfrmServerMain.FormClose(Sender: TObject;var Action: TCloseAction);beginServerSocket.Close ;end;//当客户端正在与服务器端连接时procedure TfrmServerMain.ServerSocketGetSocket(Sender: TObject;Socket: Integer; var ClientSocket: TServerClientWinSocket);beginStatusBar.Panels[0].Text :='客户端正在连接...';end;//客户端发生错误procedure TfrmServerMain.ServerSocketClientError(Sender: TObject;Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;var ErrorCode: Integer);beginStatusBar.Panels[0].Text :='客户端'+Socket.RemoteHost +'发生错误!';ErrorCode := 0;end;end.



· 客户端源码(uClientMain.pas):



unit uClientMain;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ScktComp, ComCtrls, ToolWin, ExtCtrls, StdCtrls, Buttons;constSocketHost = '172.16.1.6'; //服务器端地址typeTfrmClientMain = class(TForm)ControlBar1: TControlBar;ToolBar1: TToolBar;tbConnected: TToolButton;tbSend: TToolButton;tbClose: TToolButton;tbDisconnected: TToolButton;ClientSocket: TClientSocket;Edit1: TEdit;Memo1: TMemo;StatusBar: TStatusBar;btnSend: TBitBtn;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);private{ Private declarations }public{ Public declarations }end;varfrmClientMain: TfrmClientMain;implementation{$R *.DFM}//打开套接字连接procedure TfrmClientMain.tbConnectedClick(Sender: TObject);beginClientSocket.Open ;end;//关闭套接字连接procedure TfrmClientMain.tbDisconnectedClick(Sender: TObject);beginClientSocket.Close;end;//接受服务器端的回复procedure TfrmClientMain.ClientSocketRead(Sender: TObject;Socket: TCustomWinSocket);begin Memo1.Lines.Add(Socket.ReceiveText);end;//发送信息到服务器端procedure TfrmClientMain.tbSendClick(Sender: TObject);beginClientSocket.Socket.SendText(Edit1.Text); end;procedure TfrmClientMain.tbCloseClick(Sender: TObject);beginClose;end;//设置要连接的服务器端地址procedure TfrmClientMain.FormShow(Sender: TObject);beginClientSocket.Host := SocketHost;end;//已经连接到服务器端procedure TfrmClientMain.ClientSocketConnect(Sender: TObject;Socket: TCustomWinSocket);begintbSend.Enabled := True;tbDisconnected.Enabled :=True;btnSend.Enabled := True;StatusBar.Panels[0].Text := '已经连接到 '+ Socket.RemoteHost ;end;//正在连接到服务器端procedure TfrmClientMain.ClientSocketConnecting(Sender: TObject;Socket: TCustomWinSocket);beginStatusBar.Panels[0].Text := '正在连接到服务器... ' ;end;//当断开与服务器端的连接时发生procedure TfrmClientMain.ClientSocketDisconnect(Sender: TObject;Socket: TCustomWinSocket);begintbSend.Enabled := False;btnSend.Enabled := False;tbDisconnected.Enabled := False;StatusBar.Panels[0].Text := '已经断开与 '+ Socket.RemoteHost +' 的连接';end;procedure TfrmClientMain.FormClose(Sender: TObject;var Action: TCloseAction);beginClientSocket.Close ;end;//当与服务器端的连接发生错误时procedure TfrmClientMain.ClientSocketError(Sender: TObject;Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;var ErrorCode: Integer);beginStatusBar.Panels[0].Text := '与服务器端的连接发生错误';ErrorCode := 0;end;end.



小结

上述方法是比较简单的实现方法,同时也是相对较容易理解的方法。通过这个方法,笔者成功实现了局域网内多个客户端与服务器端进行Socket通信的功能,同时可以保证一个客户端的连接、通信或是断开都不影响其它客户端的正常通信。
附录:
服务器端窗体和客户端窗体及组件的属性设置参加相应的DFM文件。



uServerMain.pas对应的DFM文件(uServerMain.dfm)object frmServerMain: TfrmServerMainLeft = 297Top = 258BorderIcons = [biSystemMenu, biMinimize]BorderStyle = bsSingleCaption = 'ServerSocket'ClientHeight = 279ClientWidth = 476Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'MS Sans Serif'Font.Style = []OldCreateOrder = FalseOnClose = FormCloseOnCreate = FormCreatePixelsPerInch = 96TextHeight = 13object ControlBar1: TControlBarLeft = 0Top = 0Width = 476Height = 30Align = alTopAutoSize = TrueTabOrder = 0object ToolBar1: TToolBarLeft = 11Top = 2Width = 459Height = 22ButtonHeight = 21ButtonWidth = 55Caption = 'ToolBar1'EdgeInner = esNoneEdgeOuter = esNoneFlat = TrueShowCaptions = TrueTabOrder = 0object tbConnect: TToolButtonLeft = 0Top = 0Caption = '连接'ImageIndex = 0OnClick = tbConnectClickendobject tbDisconnected: TToolButtonLeft = 55Top = 0Caption = '断开'ImageIndex = 4OnClick = tbDisconnectedClickendobject tbClose: TToolButtonLeft = 110Top = 0Caption = '关闭'ImageIndex = 3OnClick = tbCloseClickendendendobject Edit1: TEditLeft = 0Top = 232Width = 473Height = 21TabOrder = 1Text = '你好!'endobject Memo1: TMemoLeft = 0Top = 30Width = 476Height = 195Align = alTopTabOrder = 2endobject StatusBar: TStatusBarLeft = 0Top = 257Width = 476Height = 22Panels = <itemWidth = 50end>SimplePanel = Falseendobject ServerSocket: TServerSocketActive = FalsePort = 1025ServerType = stNonBlockingOnListen = ServerSocketListenOnGetSocket = ServerSocketGetSocketOnClientConnect = ServerSocketClientConnectOnClientDisconnect = ServerSocketClientDisconnectOnClientRead = ServerSocketClientReadOnClientError = ServerSocketClientErrorLeft = 368endenduClientMain.pas对应的DFM文件(uClientMain.dfm)object frmClientMain: TfrmClientMainLeft = 361Top = 290BorderIcons = [biSystemMenu, biMinimize]BorderStyle = bsSingleCaption = 'ClientSocket'ClientHeight = 230ClientWidth = 402Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'MS Sans Serif'Font.Style = []OldCreateOrder = FalsePosition = poScreenCenterOnClose = FormCloseOnShow = FormShowPixelsPerInch = 96TextHeight = 13object ControlBar1: TControlBarLeft = 0Top = 0Width = 402Height = 30Align = alTopAutoSize = TrueTabOrder = 0object ToolBar1: TToolBarLeft = 11Top = 2Width = 385Height = 22ButtonHeight = 21ButtonWidth = 55Caption = 'ToolBar1'EdgeInner = esNoneEdgeOuter = esNoneFlat = TrueShowCaptions = TrueTabOrder = 0object tbConnected: TToolButtonLeft = 0Top = 0Caption = '连接'ImageIndex = 0OnClick = tbConnectedClickendobject tbSend: TToolButtonLeft = 55Top = 0Caption = '发送'Enabled = FalseImageIndex = 1OnClick = tbSendClickendobject tbDisconnected: TToolButtonLeft = 110Top = 0Caption = '断开'Enabled = FalseImageIndex = 3OnClick = tbDisconnectedClickendobject tbClose: TToolButtonLeft = 165Top = 0Caption = '退出'ImageIndex = 2OnClick = tbCloseClickendendendobject Edit1: TEditLeft = 0Top = 184Width = 321Height = 21TabOrder = 1Text = '问候'endobject Memo1: TMemoLeft = 0Top = 30Width = 402Height = 147Align = alTopTabOrder = 2endobject StatusBar: TStatusBarLeft = 0Top = 208Width = 402Height = 22Panels = <itemWidth = 50end>SimplePanel = Falseendobject btnSend: TBitBtnLeft = 336Top = 183Width = 60Height = 22Caption = '发送'Enabled = FalseTabOrder = 4OnClick = tbSendClickendobject ClientSocket: TClientSocketActive = FalseClientType = ctNonBlockingPort = 1025OnConnecting = ClientSocketConnectingOnConnect = ClientSocketConnectOnDisconnect = ClientSocketDisconnectOnRead = ClientSocketReadOnError = ClientSocketErrorLeft = 320endend



(责任编辑:Sunny)

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台