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

📄 nsmgp.c

📁 基于上海电信 sgmp3.0短信协议,实现的源程序,现网运行稳定,unix (freeBSD) 环境下编程.
💻 C
📖 第 1 页 / 共 3 页
字号:
{
    int res, ret, bytes_written = 0, i;
    unsigned int  Total_Length = LEN_SMGP_HEAD + LEN_SMGP_DELIVER_RESP;
    unsigned int  Command_ID = ID_SMGP_DELIVER_RESP;
    unsigned char buffer[48];
    unsigned char *p;
    unsigned int back;
    struct pollfd poll_list;

    memset(buffer,0,sizeof(buffer));
    p = buffer;
    MakeHead(p, Total_Length , Command_ID, Sequence);
    // p +=Total_Length;
    p += LEN_SMGP_HEAD;
    /* MsgID : string(10) */
    memcpy(p, MsgID, 10);
    //printf("Put MsgID:%s\n",MsgID);
    //strlen(MsgID));
    p += 10; 
    /* result : Integer(4) */
    back =htonl(Result);
    memcpy(p, (char *)&back, 4);
    p += 4;

    for (i = 0; i < 30; i++)
    {
        poll_list.fd = Socket_fd;
        poll_list.events = POLLOUT;

        res = poll(&poll_list, (unsigned int) 1, 1000);
        if (res == 0) { 
            continue;
        } else if (res < 0) {
            if (errno == EINTR || errno == EAGAIN) 
                continue;
            else    
            {       
                INFO( "[-ERR]: poll(2) () Failed: %s\n",  strerror(errno));
                return (-1);
            }       
        } 

        ret =  write(Socket_fd, &buffer[bytes_written], Total_Length - bytes_written);
        if (ret < 0) 
        {
            if (errno == EINTR || errno == EAGAIN) 
                continue;
            else    
            {       
                INFO( "[-ERR]: write(2) (  %d ) Failed: %s\n",  Total_Length, strerror(errno));
                return (-1);
            }       
        }
        bytes_written += ret; 
        if (bytes_written == Total_Length)
            break;  
    }
    if ( bytes_written < Total_Length )
    {
        return -1;
    }
    return 0;
}

/************************************************************************************/
/* 											   		                                */			
/* Send ActiveTest  to SMG			                                               */
/* Function name: ActiveTest	     			                                   */
/* 											                                        */
/* Argument: int Socket_fd                                                          */
/*          unsigned int Sequence		                                            */		
/* Return : int 							                                        */
/* 			 0 : right						                                        */
/*			-1 : send errror			   	                                        */			
/************************************************************************************/
int ActiveTest(int Socket_fd, unsigned int Sequence)
{
    unsigned int  Total_Length = LEN_SMGP_HEAD ;
    unsigned int  Command_ID = ID_SMGP_ACTIVE_TEST;
    unsigned char buffer[13];

    memset(buffer,0,sizeof(buffer));
    MakeHead(buffer,LEN_SMGP_HEAD  , Command_ID, Sequence);
    if(write(Socket_fd, buffer, Total_Length) != Total_Length)
    {
        return -1;
    }
    return 0;
}

/************************************************************************************/
/* 											   		                                */			
/* Recv ActiveTestResp  from SMG			                                               */
/* Function name: ActiveTestResp	     			                                   */
/* 											                                        */
/* Argument: int Socket_fd                                                          */
/*          unsigned int Sequence		                                            */		
/* Return : int 							                                        */
/* 			 0 : right						                                        */
/*			-1 : send errror			   	                                        */			
/************************************************************************************/
int ActiveTestResp(int Socket_fd, unsigned int Sequence)
{
    unsigned int  Total_Length = LEN_SMGP_HEAD ;
    unsigned int  Command_ID = ID_SMGP_ACTIVE_TEST_RESP;
    unsigned char buffer[13];

    memset(buffer,0,sizeof(buffer));
    MakeHead(buffer, LEN_SMGP_HEAD, Command_ID, Sequence);
    if(write(Socket_fd, buffer, Total_Length) != Total_Length)
        return (-1);
    return (0);
}

/************************************************************************************/
/* 											   		                                */			
/* Send Exit  packet to SMG			                                               */
/* Function name: Exit	     			                                   */
/* 											                                        */
/* Argument: int Socket_fd                                                          */
/*          unsigned int Sequence		                                            */		
/* Return : int 							                                        */
/* 			 0 : right						                                        */
/*			-1 : send errror			   	                                        */			
/************************************************************************************/
int Exit(int Socket_fd, unsigned int Sequence)
{
    char *buffer;
    int offset;

    buffer =(char*)malloc(12);
    MakeHead(buffer, 12, ID_SMGP_EXIT, Sequence);
    if(write(Socket_fd, buffer, offset ) != offset)
    {
        free(buffer);
        return -1;
    }
    free(buffer);
    return 0;
}

/************************************************************************************/
/* 											   		                                */			
/* Send Exit  Resp packet to SMG			                                               */
/* Function name: SendExitResp    			                                   */
/* 											                                        */
/* Argument: int Socket_fd                                                          */
/*          unsigned int Sequence		                                            */		
/* Return : int 							                                        */
/* 			 0 : right						                                        */
/*			-1 : send errror			   	                                        */
/************************************************************************************/
int SendExitResp(int Socket_fd, unsigned int Sequence)
{
    char *buffer;
    int offset;

    buffer =(char*)malloc(12);
    MakeHead(buffer, 12, ID_SMGP_EXIT_RESP, Sequence);
    if(write(Socket_fd, buffer, offset) != offset)
    {
        free(buffer);
        return -1;
    }
    free(buffer);
    return 0;
}

/************************************************************************************/
/* 											   		                                */			
/* Send Query packet to SMG			                                        */
/* Function name: Query    			                                        */
/* 											                                        */
/* Argument: int Socket_fd                                                          */
/*          unsigned int Sequence		                                            */		
/* Return : int 							                                        */
/* 			 0 : right						                                        */
/*			-1 : send errror			   	                                        */
/************************************************************************************/
int Query(int Socket_fd, unsigned int Sequence, char *Time, int QueryType, char *QueryCode)
{
    unsigned int Command_ID = ID_SMGP_QUERY;
    char *buffer;
    int offset = 0;
    char ch;

    buffer =(char*)malloc(31);
    /* Msg Head */
    MakeHead(buffer, 31, Command_ID, Sequence);
    offset = 31;
    /* Time : String(8) */
    memcpy(buffer+offset, Time, strlen(Time));
    offset += 8;
    /* Query_Type: Integer(1) */
    ch = QueryType;
    memcpy(buffer+offset, (char*)&ch, 1);
    offset += 1;
    /* Query_Code : String(10) */
    memcpy(buffer+offset, QueryCode, strlen(QueryCode));
    offset += 10;

    if(write(Socket_fd, buffer, offset) != offset)
    {
        free(buffer);
        return -1;
    }
    free(buffer);
    return 0;

}


/************************************************************************************/
/* 											   		                                */			
/* Recv Query  Resp packet to SMG			                                        */
/* Function name: QueryResp    			                                            */
/* 											                                        */
/* Argument: int Socket_fd                                                          */
/*          unsigned int Sequence		                                            */		
/* Return : int 							                                        */
/* 			 0 : right						                                        */
/*			-1 : send errror			   	                                        */
/************************************************************************************/
int QueryRespRecv(int Socket_fd, char *Time, int *QueryType, char *QueryCode, unsigned int *MT_TLMsg, unsigned int *MT_Tluser, unsigned int *MT_Scs, unsigned int *MT_WT, unsigned int *MT_FL, unsigned int *MO_Scs, unsigned int *MO_WT, unsigned int *MO_FL)
{
    char *buffer;
    int len;
    unsigned int temp;
    int offset =0;

    buffer = (char *)malloc(59);
    memset(buffer, 0, 59);
    len=read(Socket_fd,buffer,59);
    if(len != 59){
        free(buffer);
        return -1;
    }
    /* Time : String(10) */
    memcpy(Time, buffer, 8);
    offset += 8;
    /* Query_Type : Integer(1) */
    *QueryType = buffer[offset -1];
    offset += 1;
    /* Query_Code : String(10) */
    memcpy(QueryCode, buffer+offset, 10);
    offset += 10;
    /* MT_TLMsg : Integer(4)     */
    memcpy((char*)&temp, buffer+offset, 4);
    offset += 4;
    *MT_TLMsg = htonl(temp);
    /* MT_TLusr : Integer(4)     */
    memcpy((char*)&temp, buffer+offset, 4);
    offset += 4;
    *MT_Tluser = htonl(temp);	
    /* MT_Scs : Integer(4)       */
    memcpy((char*)&temp, buffer+offset, 4);
    offset += 4;
    *MT_Scs = htonl(temp);
    /* MT_WT : Integer(4)        */
    memcpy((char*)&temp, buffer+offset, 4);
    offset += 4;
    *MT_WT = htonl(temp);
    /* MT_FL : Integer(4)        */
    memcpy((char*)&temp, buffer+offset, 4);
    offset += 4;
    *MT_FL = htonl(temp);
    /* MO_Scs : Integer(4)       */
    memcpy((char*)&temp, buffer+offset, 4);
    offset += 4;
    *MO_Scs = htonl(temp);
    /* MO_WT : Integer(4)        */
    memcpy((char*)&temp, buffer+offset, 4);
    offset += 4;
    *MO_WT = htonl(temp);
    /* MO_FL : Integer(4)        */
    memcpy((char*)&temp, buffer+offset, 4);
    offset += 4;
    *MO_FL = htonl(temp);

    free(buffer);
    return 0;
}


/*分解话单通知内容*/
int GetSmcNotice( unsigned char *smcBuf, 
                  char *mobile , 
                  char *service ,                  
                  char *date1 , 
                  char *date2 , 
                  char *status , 
                  char *feecode   )
{                                    
// MsgContent:09918061574          ,DXXDG     ,20060601,20060630,0,001500

            	char tmpBuf[128]="";
            	char *pstr=NULL;
            	char *phead=NULL;
            	char *ptmp=NULL;
            	int  num=0;
            	             	
            	pstr=smcBuf;
            	phead=smcBuf;
            	while( *pstr )
            	{                     	    
            	   if ( (*pstr) ==',' || (*(pstr+1)) =='\0')
            	   {
            	       num++;
            	       memset( tmpBuf ,0 ,sizeof( tmpBuf ));
            	                   	                  	       
            	       if ( (*pstr) ==',' )
            	       { 
            	          strncpy( tmpBuf ,phead, pstr-phead ); 
            	          phead=pstr+1;
            	       }
            	       if ( (*(pstr+1)) =='\0')
            	       { 
            	          strcpy( tmpBuf ,phead );             	         
            	       }            	       
            	                   	       
            	       ptmp=tmpBuf;
            	       while ( *ptmp )  //ignore tail space ' '
            	       {            	       	  
            	       	  if ( *ptmp ==' ' )
            	       	  {
            	       	      *ptmp='\0' ;
            	       	      break;
            	       	  }
            	       	  ptmp++;             	       	 
            	       }
            	                   	        
            	       //INFO("[INFO]:tmpBuf=%s\n",tmpBuf);
            	       //printf("[INFO]:tmpBuf=%s\n",tmpBuf);
            	        
            	       if ( 1==num )
            	           strcpy( mobile ,tmpBuf );
            	       if ( 2==num )
            	           strcpy( service ,tmpBuf );
            	       if ( 3==num )
            	           strcpy( date1 ,tmpBuf );
            	       if ( 4==num )
            	           strcpy( date2 ,tmpBuf );
            	       if ( 5==num )
            	           strcpy( status ,tmpBuf );
            	       if ( 6==num )
            	           strcpy( feecode ,tmpBuf );            	                                                
            	   }            	       
            	   pstr++; 	
            	}            	
    return 0;
    
}     


⌨️ 快捷键说明

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