jsj_Delphi关于Socket编程.pdf
《jsj_Delphi关于Socket编程.pdf》由会员分享,可在线阅读,更多相关《jsj_Delphi关于Socket编程.pdf(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 Delphi?Socket?2008-07-07?03:01?Socket?TServerSocket?TClientSocket?ClientSocket?ServerSocket?ServerSocket?ClientSocket?ClientSocket?type client_record=record CHandle:integer;/?CSocket:TCustomWinSocket;/?CName:string;/?CAddress:string;/?IP?CUsed:boolean;/?end;?CHandle?Csocket?Cused?ServerSocket?Clien
2、tSocket?ServerSocket?Port?1025?ServerTypt?stNonBlocking?ThreadCacheSize?10?ClientSocket?Port?1025?ClientType?stNonBlocking?Host?IP?:?uServerMain.pas?unit uServerMain;interface uses Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,ScktComp,ToolWin,ComCtrls,ExtCtrls,StdCtrls,Buttons;c
3、onst 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:
4、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 Serv
5、erSocketClientConnect(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 ServerSocketG
6、etSocket(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:array0.CMax of client_record;/?Sessio
7、ns: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.Panels0.Text:=?.;end;/?procedure TfrmServerM
8、ain.ServerSocketClientRead(Sender:TObject;Socket:TCustomWinSocket);var i:integer;begin /?Memo1?Memo1.Lines.Add(Socket.ReceiveText);for i:=0 to sessions do begin /?if sessioni.CHandle=Socket.SocketHandle then begin sessioni.CSocket.SendText(?+sessioni.CAddress+=+Edit1.Text);end;end;end;/?procedure Tf
9、rmServerMain.ServerSocketListen(Sender:TObject;Socket:TCustomWinSocket);begin StatusBar.Panels0.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 sessioni.CUsed then begin sessio
10、ni.CHandle:=Socket.SocketHandle;/?sessioni.CSocket:=Socket;/?sessioni.CName:=Socket.RemoteHost;/?sessioni.CAddress:=Socket.RemoteAddress;/?IP sessioni.CUsed:=True;/?Break;end;j:=i;end;if j=sessions then begin inc(sessions);sessionj.CHandle:=Socket.SocketHandle;sessionj.CSocket:=Socket;sessionj.CName
11、:=Socket.RemoteHost;sessionj.CAddress:=Socket.RemoteAddress;sessionj.CUsed:=True;end;StatusBar.Panels0.Text:=?+Socket.RemoteHost+?;end;/?procedure TfrmServerMain.ServerSocketClientDisconnect(Sender:TObject;Socket:TCustomWinSocket);var i:integer;begin for i:=0 to sessions do begin if sessioni.CHandle
12、=Socket.SocketHandle then begin sessioni.CHandle:=0;sessioni.CUsed:=False;Break;end;end;StatusBar.Panels0.Text:=?+Socket.RemoteHost+?;end;/?procedure TfrmServerMain.tbCloseClick(Sender:TObject);begin Close;end;procedure TfrmServerMain.FormCreate(Sender:TObject);begin sessions:=0;end;procedure TfrmSe
13、rverMain.FormClose(Sender:TObject;var Action:TCloseAction);begin ServerSocket.Close;end;/?procedure TfrmServerMain.ServerSocketGetSocket(Sender:TObject;Socket:Integer;var ClientSocket:TServerClientWinSocket);begin StatusBar.Panels0.Text:=?;end;/?procedure TfrmServerMain.ServerSocketClientError(Sende
14、r:TObject;Socket:TCustomWinSocket;ErrorEvent:TErrorEvent;var ErrorCode:Integer);begin StatusBar.Panels0.Text:=?+Socket.RemoteHost+?;ErrorCode:=0;end;end.?(uClientMain.pas)?unit uClientMain;interface uses Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,ScktComp,ComCtrls,ToolWin,ExtC
15、trls,StdCtrls,Buttons;const SocketHost=172.16.1.6;/?type TfrmClientMain=class(TForm)ControlBar1:TControlBar;ToolBar1:TToolBar;tbConnected:TToolButton;tbSend:TToolButton;tbClose:TToolButton;tbDisconnected:TToolButton;ClientSocket:TClientSocket;Edit1:TEdit;Memo1:TMemo;StatusBar:TStatusBar;btnSend:TBit
16、Btn;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 ClientSocketConnec
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- jsj_Delphi 关于 Socket 编程
限制150内