⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smpp_s.cpp

📁 SMPP SERVER 可以回复ACK及状态报告
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Smpp_S.h"
#include <IniFiles.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSmpp *Smpp;

int SocketPort;
long message_id;

//---------------------------------------------------------------------------
__fastcall TSmpp::TSmpp(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void  TSmpp::ConnGUIP()
{
    message_id = 0;
    if (ServerSocket1->Active)
        return;
    ServerSocket1->Port = SocketPort;
    ServerSocket1->Open();


}




void __fastcall TSmpp::FormCreate(TObject *Sender)
{
    loadConf();
    ConnGUIP();

}
//---------------------------------------------------------------------------
void TSmpp::loadConf()
{
    TIniFile    *ini = new TIniFile(".\\config.ini");
    AnsiString str;

    SocketPort     = ini->ReadString("PORT", "SocketPort", "").ToInt();

    delete ini;
}





void __fastcall TSmpp::ServerSocket1ClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
    char buf[16],buff[512],*ptr;
    int j;
    memset(buf, 0x00, sizeof(buf));
    memset(buff, 0x00, sizeof(buff));
    Socket->ReceiveBuf(buf, 16);

    SMPP_Header *head;
    SMPP_Header *head_resp = new SMPP_Header;
    head=(SMPP_Header*)buf;
    head->Command_Length = ntohl(head->Command_Length );
    head->Command_ID     = ntohl(head->Command_ID );
    head->Command_status = ntohl(head->Command_status );
    head->Sequence_No = ntohl(head->Sequence_No );

    if(head->Command_ID == cmd_bind_receiver )     //bind
    {
       Socket->ReceiveBuf(buff, head->Command_Length - 16 );
       AnsiString system_id = AnsiString(buff).Trim();
       head_resp->Command_Length  = htonl(sizeof( SMPP_Header) + system_id.Length());
       head_resp->Command_ID = htonl(cmd_bind_receiver_resp);
       head_resp->Command_status = htonl(ESME_ROK) ;
       head_resp->Sequence_No =  htonl(head->Sequence_No);
       Socket->SendBuf((char *)head_resp,16);
       Socket->SendBuf(system_id.c_str(),system_id.Length());
    }

    if(head->Command_ID == cmd_bind_transmitter )     //bind
    {
       Socket->ReceiveBuf(buff, head->Command_Length - 16 );
       AnsiString system_id = AnsiString(buff).Trim();
       head_resp->Command_ID = htonl(cmd_bind_transmitter_resp);
       head_resp->Command_status = htonl(ESME_ROK) ;
       head_resp->Sequence_No =  htonl(head->Sequence_No);
       head_resp->Command_Length = htonl(sizeof(SMPP_Header) + system_id.Length());
       int len = sizeof(*head_resp);

       Socket->SendBuf((char *)head_resp,len);
       Socket->SendBuf(system_id.c_str(),system_id.Length());


    }
    if(head->Command_ID == cmd_enquire_link )     //link
    {
       head_resp->Command_Length = htonl(sizeof( SMPP_Header));
       head_resp->Command_ID = htonl(cmd_enquire_link_resp);
       head_resp->Command_status = htonl(ESME_ROK) ;
       head_resp->Sequence_No =  htonl(head->Sequence_No);
       Socket->SendBuf((char *)head_resp,16);

    }
    if(head->Command_ID == cmd_unbind )     //link
    {
       head_resp->Command_Length = htonl(sizeof( SMPP_Header));
       head_resp->Command_ID = htonl(cmd_unbind_resp);
       head_resp->Command_status = htonl(ESME_ROK) ;
       head_resp->Sequence_No =  htonl(head->Sequence_No);
       Socket->SendBuf((char *)head_resp,16);
    }

    if(head->Command_ID == cmd_deliver_sm_resp )     //bind
    {
       Socket->ReceiveBuf(buff, head->Command_Length - 16 );
    }


    if(head->Command_ID == cmd_submit_sm )     //bind
    {
       Socket->ReceiveBuf(buff, head->Command_Length - 12 );

       if( message_id > 100000)
           message_id = 0;

       message_id++;

       head_resp->Command_Length = htonl(sizeof( SMPP_Header) + AnsiString(message_id).Length());
       head_resp->Command_ID = htonl(cmd_submit_sm_resp);
       head_resp->Command_status = htonl(ESME_ROK) ;
       head_resp->Sequence_No =  htonl(head->Sequence_No);
       Socket->SendBuf((char *)head_resp,16);
       Socket->SendBuf(AnsiString(message_id).c_str(),AnsiString(message_id).Length());

       head_resp->Command_Length = htonl(head->Command_Length);
       head_resp->Command_ID = htonl(cmd_deliver_sm);
       head_resp->Command_status = htonl(0) ;
       head_resp->Sequence_No =  htonl(head->Sequence_No);

       Socket->SendBuf((char *)head_resp,16);
       Socket->SendBuf(buff,head->Command_Length -16 );

       //char deliver_sm[512];
       //strcpy(deliver_sm,buff);
       //SendBuff( *head_resp,buff,head->Command_Length);



    }




/*
    if(head->CmdID == cmd_bind_transmitter )     //bind
    {
       Socket->ReceiveBuf(buff, head->CmdLength - 12 );
    }

    if(head->CmdID == cmd_enquire_link)     //内容检查
    {
        HEAD_DEF *sm_hend;
        sm_hend->CmdID = htonl(cmd_enquire_link_resp );
        sm_hend->CmdStatus = htonl(ESME_ROK);
        sm_hend->CmdLength = htonl(16);
        sm_hend->SequenceNo = htonl(head->SequenceNo);
        ClientSocket1->Socket->SendBuf((char *)sm_hend,sizeof(*sm_hend));

    }
    if(head->CmdID == cmd_unbind_resp)     //内容检查
    {
       ClientSocket1->Close() ;

    }
*/
}
//---------------------------------------------------------------------------
void  TSmpp::SendBuff(SMPP_Header *head,char* buff,int len )
{
       ServerSocket1->Socket->Connections[0]->SendBuf((char *)head,sizeof(*head));
       ServerSocket1->Socket->Connections[0]->SendBuf(buff,len);


}
char*   TSmpp::submit_sm_resp(char *buff)
{


    return "sfdds";

}

void __fastcall TSmpp::ServerSocket1ClientConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  ListBox1->Items->Add(Socket->RemoteAddress+":"+IntToStr(Socket->RemotePort));

}
//---------------------------------------------------------------------------

void __fastcall TSmpp::ServerSocket1ClientError(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode=0;
for(int i=0;i<=ListBox1->Count-1;i++)
{
if((AnsiString)(Socket->RemoteAddress+":"+IntToStr(Socket->RemotePort))==ListBox1->Items->operator [](i))
{
 ListBox1->Items->Delete(i);
}
}

}
//---------------------------------------------------------------------------

void __fastcall TSmpp::ServerSocket1ClientDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
for(int i=0;i<=ListBox1->Count-1;i++)
{
if((AnsiString)(Socket->RemoteAddress+":"+IntToStr(Socket->RemotePort))==ListBox1->Items->operator [](i))
{
 ListBox1->Items->Delete(i);
}
}

}
//---------------------------------------------------------------------------

void __fastcall TSmpp::Button1Click(TObject *Sender)
{
   Close();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -