《delphi一个用socket封装UDPTCP通信的例子.doc》由会员分享,可在线阅读,更多相关《delphi一个用socket封装UDPTCP通信的例子.doc(11页珍藏版)》请在金锄头文库上搜索。
1、unitUnitTCPUDP;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,WinSock,ExtCtrls,ComCtrls,inifiles,StrUtils;constWM_SOCK=WM_USER+82;自定义windows消息/在tcp服务器方式下,WM_SOCK为监听消息/WM_SOCK+1到WM_SOCK+MAX_ACCEPT为与连接客户端进行通讯时的消息MAX_ACCEPT=100;FD_SET=MAX_ACCEPT;typeTFo
2、rmTCPUDP=class(TForm)BtnSend:TButton;MemoReceive:TMemo;EditSend:TEdit;Label2:TLabel;Label3:TLabel;Bevel2:TBevel;STOpCode:TStaticText;STIndex:TStaticText;STCommand:TStaticText;GroupBox1:TGroupBox;GroupBox2:TGroupBox;GroupBox3:TGroupBox;RBTCP:TRadioButton;RBUDP:TRadioButton;Panel1:TPanel;RBClient:TRad
3、ioButton;RBServer:TRadioButton;GroupBox4:TGroupBox;BtnConnect:TButton;BtnClose:TButton;Bevel1:TBevel;StatusBar1:TStatusBar;PanelDest:TPanel;Label4:TLabel;EditRemoteHost:TEdit;Label5:TLabel;EditRemotePort:TEdit;Label6:TLabel;CmbSendTo:TComboBox;Label7:TLabel;PanelLocal:TPanel;ChkBind:TCheckBox;EditHo
4、stPort:TEdit;Label1:TLabel;procedureBtnSendClick(Sender:TObject);procedureBtnConnectClick(Sender:TObject);procedureRBTCPClick(Sender:TObject);procedureRBUDPClick(Sender:TObject);procedureBtnCloseClick(Sender:TObject);procedureFormClose(Sender:TObject;varAction:TCloseAction);procedureRBClientClick(Se
5、nder:TObject);procedureRBServerClick(Sender:TObject);procedureChkBindClick(Sender:TObject);procedureFormCreate(Sender:TObject);procedureEditHostPortChange(Sender:TObject);procedureEditRemoteHostChange(Sender:TObject);procedureEditRemotePortChange(Sender:TObject);procedureFormActivate(Sender:TObject)
6、;procedureCmbSendToKeyPress(Sender:TObject;varKey:Char);消息接送privatePrivatedeclarationsFirstFlag:Boolean;INIPath:String;procedureReadData(varMessage:TMessage);functionReadTCPUDPIni():boolean;/读取配置信息procedureWriteIniStr(FileName:String;section:string;Ident:string;StringValue:string);/写系统信息procedureWri
7、teIniBool(FileName:String;section:string;Ident:string;BoolValue:Boolean);/写系统信息protectedProtecteddeclarationsotherfieldsandmethodsprocedurewndproc(varmessage:Tmessage);override;publicPublicdeclarationsend;constDATA_LENGTH=120;/数据长度typeTUDPaction=packedrecordopcode:byte;/操作码index:word;/序列号Command:byt
8、e;/命令字data:array0.(DATA_LENGTH-1)ofchar;/数据end;varFormTCPUDP:TFormTCPUDP;AcceptSock:Array0.MAX_ACCEPTOFTsocket;FSockAccept:Array0.MAX_ACCEPTOFTSockAddrIn;AcceptSockFlag:Array0.MAX_ACCEPTOFboolean;AcceptNum:integer=0;FSockLocal:TSockAddrIn;PackageID:integer=0;/包序号BindFlag:Boolean=true;TcpFlag:Boolean
9、=false;ServerFlag:Boolean=false;functionWinSockInital(Handle:HWnd):bool;ProcedureWinSockClose();implementation$R*.dfm始化SOCKETfunctionWinSockInital(Handle:HWnd):bool;varTempWSAData:TWSAData;i:integer;beginresult:=false;1初始化SOCKETifWSAStartup(2,TempWSAData)=1then/2表示启用winsock2exit;若是用UDP通信,则用ifTcpFlag
10、thenAcceptSock0:=Socket(AF_INET,SOCK_STREAM,0)elseAcceptSock0:=Socket(AF_INET,SOCK_DGRAM,0);ifAcceptSock0=SOCKET_ERRORthenexit;if(BindFlagandnottcpflag)or(Serverflagandtcpflag)thenifbind(AcceptSock0,FSockLocal,sizeof(FSockLocal)0thenbeginWinSockClose();exit;end;ifTcpflagthenifServerflagthenbeginifLi
11、sten(AcceptSock0,1)0then/等待连接队列的最大长度为1begin WinSockClose();exit;end;endelseifconnect(AcceptSock0,FSockAccept0,sizeof(FSockAccept0)0thenbeginWinSockClose();exit;end;FD_READ在读就绪的时候,产生WM_SOCK自定义消息号ifnotTcpFlagthenWSAAsyncSelect(AcceptSock0,Handle,WM_SOCK,FD_READ)elseifServerflagthenWSAAsyncSelect(
12、AcceptSock0,Handle,WM_SOCK,FD_READorFD_ACCEPTorFD_CLOSE)elseWSAAsyncSelect(AcceptSock0,Handle,WM_SOCK,FD_READorFD_CLOSE);Result:=true;end;关闭SOCKETProcedureWinSockClose();vari:integer;beginfori:=1toMAX_ACCEPTDOifAcceptSockFlagithenbeginCloseSocket(AcceptSocki);AcceptSockFlagi:=false;end;CloseSocket(AcceptSock0);closesocket函数用来关闭一个描述符为AcceptSock0套接字WSACleanup;end;functionTFormTCPUDP.ReadTC