📄 tcpfunc.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <errno.h>#include "etc.h"#include "api20.h"/* Global functions */int PutMessage(int sockid, char *header, FIELDINFO *fieldinfo, int optcode);int ConnectServer(char *hostname, short s_remote_port);/* Local functions */static int maxnodenumber(FIELDINFO *fieldinfo1);static void merge(char *tempbuf, FIELDINFO *fieldinfo, int start, int end, char *header, int flag1);static int judge(FIELDINFO *fieldinfo, int *start, int *end );extern int debug;/* * ConnectServer() * param -- * return -- */int ConnectServer(char *hostname, short s_remote_port){ static char cache_hostname[128]; static int first_call = TRUE; static struct sockaddr_in ser_addr; struct sockaddr_in backup; int sd; struct hostent *h; struct in_addr *p; if (debug) bms_trace("Socket [%s][%d]",hostname,s_remote_port); if (first_call) { cache_hostname[0] = '\0'; first_call = FALSE; bms_trace("Socket first_call"); } if (strcmp(hostname, cache_hostname) != 0) { /* Make a backup first */ backup = ser_addr; /* Get IP address of the host */ memset((char *) &ser_addr, 0, sizeof(ser_addr)); ser_addr.sin_family = AF_INET; ser_addr.sin_addr.s_addr = inet_addr(hostname); if ((long) ser_addr.sin_addr.s_addr == INADDR_NONE) { h = gethostbyname(hostname); if (h != NULL) { p = (struct in_addr *) (h->h_addr_list[0]); ser_addr.sin_addr.s_addr = p->s_addr; } else { /* Cannot resolve address */ /* Restore from backup */ ser_addr = backup; return -1; } } /* Cache hostname */ strcpy(cache_hostname, hostname); } /* Then setup port for all cases */ ser_addr.sin_port = htons(s_remote_port); /* Open socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if (sd < 0) { bms_trace("Socket failed"); return -2; } if (connect(sd, (struct sockaddr *) &ser_addr, sizeof(ser_addr)) < 0) { close(sd); return -3; } return sd;}/* * PutMessage() * param -- * return -- */int PutMessage(int sockid, char *header, FIELDINFO *fieldinfo, int optcode){ char temp_buf[MAX_MESS_LEN]; int count, flag,start,end ,ret,timeout; char cnetCode[4]; FILE *fp; char tempbuf[1024]; start=1; end=0; memset(tempbuf,0,sizeof(tempbuf)); memset(temp_buf,0,sizeof(temp_buf)); memset(cnetCode,0,sizeof(cnetCode)); while(1) { /*count= maxnodenumber(fieldinfo);*/ flag=judge(fieldinfo, &start, &end ); merge(temp_buf,fieldinfo, start, end, header, flag); //if (debug) //fprintf(stdout, "SndBuf = [%s]\n", temp_buf); //detail_log( temp_buf, strlen(temp_buf),optcode, "送网银","tohost" ); start=end + 1; if (flag == 0 ) { temp_buf[6]='0'; //ret=SockSnd(sockid,temp_buf); if ( ret < 0 ) { bms_trace("发送数据失败"); return -2; } break; } else if( flag == 1 ) { temp_buf[6]='1'; //ret=SockSnd(sockid,temp_buf); if ( ret < 0 ) { bms_trace("发送数据失败"); return -1; } } } return 0;}/* * judge * param -- * return -- */static int judge(FIELDINFO *fieldinfo, int *start, int *end ) { FIELDINFO *tempptr,*tempptr1; char tempbuf[MAX_MESS_LEN+1]; char tempbuf1[FIELD_LEN_LEN+1]; char tempbuf2[MAX_MESS_LEN+1]; int i; int count; memset(tempbuf,0,sizeof(tempbuf)); memset(tempbuf1,0,sizeof(tempbuf1)); memset(tempbuf2,0,sizeof(tempbuf2)); tempptr=fieldinfo; for(i=1;i<*start;i++) tempptr=tempptr->next; i=0; if (*start == 1 ) { i++; tempptr=tempptr->next; } tempbuf[0]='\0'; tempbuf2[0]='\0'; while(tempptr->next != NULL) { i++; strcat(tempbuf,tempptr->tag); sprintf(tempbuf1,"%02d",strlen(tempptr->value)); /*不足3位的应用0补齐*/ strcat(tempbuf,tempbuf1); strcat(tempbuf,tempptr->value); tempptr1=tempptr->next; strcpy(tempbuf2,tempptr1->tag); sprintf(tempbuf1,"%02d",strlen(tempptr1->value)); /*不足3位的应用0补齐*/ strcat(tempbuf2,tempbuf1); strcat(tempbuf2,tempptr1->value); if (strlen(tempbuf) + strlen(tempbuf2) >= MAX_MESS_LEN - RESMESS_HEAD_OFFSET) { *end=*start+i-1; return 1; } else { tempptr=tempptr->next; } } i++; *end=*start+i-1; return 0;} /* * merge * param -- * return -- */static void merge(char *tempbuf, FIELDINFO *fieldinfo, int start, int end, char *header, int flag1){ int i; FIELDINFO *tempptr; char tempbuf1[FIELD_LEN_LEN+1]; char tempbuf2[MAX_MESS_LEN+1]; char head[RESMESS_HEAD_OFFSET+1]; memset(tempbuf1,0,sizeof(tempbuf1)); memset(tempbuf2,0,sizeof(tempbuf2)); memset(head,0,sizeof(head)); tempptr=fieldinfo; for(i=1; i<start; i++) { tempptr=tempptr->next; } if (start == 1 ) { tempptr=tempptr->next; start++; } tempbuf[0]='\0'; for (i=start; i<end+1; i++) { strcat(tempbuf,tempptr->tag); sprintf(tempbuf1,"%02d",strlen(tempptr->value)); /*不足3位的应用0补齐*/ strcat(tempbuf,tempbuf1); strcat(tempbuf,tempptr->value); tempptr=tempptr->next; } sprintf(head,"%s",header); if (flag1 == 1) { head[HAVE_NEXT]='1'; } else { head[HAVE_NEXT]='0'; } sprintf(tempbuf2,"%s%s", head, tempbuf); strcpy(tempbuf, tempbuf2); }/* * maxnodenumber * param -- * return -- */static int maxnodenumber(FIELDINFO *fieldinfo1){ FIELDINFO *tempptr; int count1; tempptr=fieldinfo1; count1=0; while(tempptr->next != NULL) { tempptr=tempptr->next; count1++; } return count1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -