unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,winsock, ComCtrls, ToolWin, ImgList, Menus, ExtCtrls;type TRecvThread = class(TThread) private // procedure CreateSocket(); procedure OnRecive(var ok : boolean); function SocketThread(): DWORD; protected procedure Execute; override; publicend;type TForm1 = class(TForm) Button2: TButton; Memo1: TMemo; MainMenu1: TMainMenu; N1: TMenuItem; N4: TMenuItem; N2: TMenuItem; N5: TMenuItem; N6: TMenuItem; N3: TMenuItem; N7: TMenuItem; ImageList1: TImageList; LightImg: TImageList; StatusBar1: TStatusBar; CoolBar1: TCoolBar; ToolBar1: TToolBar; TBClear: TToolButton; ToolButton3: TToolButton; TBActive: TToolButton; ToolButton1: TToolButton; TBTest: TToolButton; ToolButton2: TToolButton; TBAbout: TToolButton; ToolButton4: TToolButton; TBExit: TToolButton; Edit1: TEdit; Label1: TLabel; Label2: TLabel; Edit2: TEdit; Label3: TLabel; Edit3: TEdit; Timer1: TTimer; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); procedure TBExitClick(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } thread : TRecvThread; public { Public declarations } end;var Form1: TForm1; Addr : TSockAddr; m_sock : TSocket; szPort :WORD;//端口 szADDR: array[0..63] of char;//IP //recvbufferIndex : Integer; //recvbuffer : array[0..4096] of Char; cachebuffer : array[0..4096] of Char; loop : Boolean; //接通平台标志 THESEQ_NO : integer;implementationuses xintai;{$R *.dfm}function GET_SEQ_NO():string;begin if THESEQ_NO >= 9999 then begin THESEQ_NO := 0 end else begin Inc(THESEQ_NO); end; result := FormatDatetime('YYYYMMDD', Now)+inttostr(THESEQ_NO);end;procedure memcpy(o, input : PChar; len : Integer);var i : integer;begin i := 0; while i < len do begin o := input; Inc(i); end;end;procedure TRecvThread.CreateSocket();begin GetPrivateProfileString ('Info', 'ADDRESS','', szADDR, sizeof(szADDR),'./SendMsg.ini'); szPort:=GetPrivateProfileInt('Info','APPPORT',0,'./SendMsg.ini'); m_sock := socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); Addr.sin_family := AF_INET; Addr.sin_port := htons(szPort); Addr.sin_addr := TInAddr(inet_addr(szADDR)); form1.Memo1.Lines.Add('准备链接平台>>>>>'); application.ProcessMessages; while 0 > connect(m_sock, Addr, sizeof(Addr)) do begin form1.Memo1.Lines.Add('平台链接失败,5秒后进行重链>>>>>'); loop:=false; sleep(5000);//5妙后重联 end; loop:=true;//平台链接ok while loop do begin OnRecive(loop) ;end;end;procedure TRecvThread.OnRecive(var ok : boolean);Var recvLen : Integer; submit_rsp: TSTRUCT_SUBMIT_RSP;begin FillChar(cachebuffer,SizeOf(cachebuffer),0); recvLen := recv( m_sock, cachebuffer, 4096, 0 ); if recvLen>0 then begin loop:=false;exit;end; memcpy(@submit_rsp, cachebuffer, recvLen); //Recv(ASocket,buf,SizeOf(buf),0); //接收数据 form1.Memo1.Lines.Add(string(submit_rsp.sHeader.Service_ID)); form1.Memo1.Lines.Add(string(submit_rsp.sHeader.Command_ID)); if string(submit_rsp.sHeader.Command_ID) = '10005' then begin form1.Memo1.Lines.Add('定购回应');end; if string(submit_rsp.sHeader.Command_ID) = '10015' then begin form1.Memo1.Lines.Add('退订回应');end; if string(submit_rsp.sHeader.Command_ID) = '10025' then begin form1.Memo1.Lines.Add('暂停回应');end; if string(submit_rsp.sHeader.Command_ID) = '10035' then begin form1.Memo1.Lines.Add('回复回应');end; form1.Memo1.Lines.Add('回应:'+string(@submit_rsp.Sequence_No)); form1.Memo1.Lines.Add('回应状态:'+inttostr(ntohl(submit_rsp.Command_Result))); //发送消息后断开 closesocket( m_sock ); Suspend(); Terminate(); CloseHandle(Handle);end;function TRecvThread.SocketThread(): DWORD;begin CreateSocket(); // end;procedure TRecvThread.Execute();begin SocketThread();end;procedure SubmitMsg(sock : TSocket;handset:string;Command_ID:string;starttime:string);var submit : TSTRUCT_SUBMIT;begin ZeroMemory(@submit,sizeof(submit)) ; fillchar(submit.sSubmitHeader.Service_ID,sizeof(submit.sSubmitHeader.Service_ID),ord(0)); strpcopy(submit.sSubmitHeader.Service_ID,'961681030') ; //业务代码 fillchar(submit.sSubmitHeader.Command_ID,sizeof(submit.sSubmitHeader.Command_ID),ord(0)); strpcopy( submit.sSubmitHeader.Command_ID,Command_ID) ; //定义本条消息所对应的操作 submit.sSubmitHeader.Command_Length:=htons(sizeof(submit.sSubmitHeader)+sizeof(submit.hnqcSubmit));//消息长度 submit.sSubmitHeader.Server_Process:=htons(1);//本条消息目的应用进程标识。在各消息中定义 // fillchar(submit.hnqcSubmit.Sequence_No,sizeof(submit.hnqcSubmit.Sequence_No),ord(0)); strpcopy( submit.hnqcSubmit.Sequence_No,GET_SEQ_NO()) ; //由消息发起方分配的一个用于把,流水号 fillchar(submit.hnqcSubmit.Sub_MDN,sizeof(submit.hnqcSubmit.Sub_MDN),ord(0)); strpcopy( submit.hnqcSubmit.Sub_MDN,handset) ; //字符型,应该是电话号码 fillchar(submit.hnqcSubmit.STARTTIME,sizeof(submit.hnqcSubmit.STARTTIME),ord(0)); strpcopy( submit.hnqcSubmit.STARTTIME,starttime) ; //请求时间 ;YYYY-MM-DD HH:MM:SS submit.hnqcSubmit.FACTORY:=htonl(3); //受理渠道 submit.hnqcSubmit.PACKAGE:=htonl(0); //是否属于套餐订购的标志 submit.hnqcSubmit.PPFlag:=0; //预付费标记 // send(sock,submit, ntohl(submit.sSubmitHeader.Command_Length), 0); form1.Memo1.Lines.Add('向平台提交一条Submit消息');end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin //断开 thread.Suspend(); thread.Terminate(); CloseHandle(thread.Handle); closesocket( m_sock ); WSACleanup; end;procedure TForm1.FormCreate(Sender: TObject);begin WSASetLastError( 0 ); edit3.Text:=FormatDatetime('YYYY-MM-DD HH:MM:SS', Now); THESEQ_NO:=0; loop:=false;end;procedure TForm1.TBExitClick(Sender: TObject);begin close;end;procedure TForm1.Button2Click(Sender: TObject);begin thread := TRecvThread.Create(false); if loop=true then begin //发送消息 SubmitMsg(m_sock,trim(edit1.Text),trim(edit2.Text),trim(edit3.Text)); //接收消息 end;end;end.