X
xlz
Unregistered / Unconfirmed
GUEST, unregistred user!
服务端代码:
#include "xyjh.h"
#include "cgasnet.h"
#define XZCBSJ 01
#define DRCBSJ 02
char cbjfilename[80];
int closefd, cbyh, zbs;
long rq;
main(argc,argv)
char *argv[];
int argc;
{
int srvport;
signal( SIGINT , SIG_IGN ) ;
signal( SIGHUP , SIG_IGN ) ;
signal( SIGCLD , SIG_IGN ) ;
if (argc!=2) {
printf("/n/n启动格式错误!/n/n"
exit(0);
}
srvport=atol(argv[1]);
if( fork() == 0 )
sub_main(srvport);
else
exit( 0 );
}
sub_main(port)
int port;
{
struct sockaddr_in s_add , con_s_add ;
struct servent *p_servent ;
int sockfd , new_sockfd ;
pid_t child_pid ;
int addr_len ;
int bind(),accept();
sockfd = socket ( AF_INET , SOCK_STREAM , IPPROTO_IP );
if ( sockfd < 0 )
{
perror ( " socket call error: " );
exit ( -1 );
}
s_add.sin_family = AF_INET ;
/*s_add.sin_port = htons( SRVPORT ) ;*/
s_add.sin_port = htons( port ) ;
s_add.sin_addr.s_addr = htonl( INADDR_ANY ) ;
if( bind ( sockfd , &s_add , sizeof( s_add ) ) < 0 )
{
perror ( " bind call error: "
exit ( -1 );
}
if ( listen ( sockfd , 5 ) < 0 )
{
perror ( "listen call error: " );
exit ( -1 );
}
addr_len = sizeof (con_s_add ) ;
printf ( "启动成功 ! /n/n" ) ;
while( 1 )
{
new_sockfd = accept ( sockfd , &con_s_add , &addr_len );
if ( new_sockfd < 0 )
{
perror ( " accept call error: " );
exit ( -1 );
}
if ( ( child_pid = fork() ) == 0 )
{
close ( sockfd );
sjjh( new_sockfd );
close ( new_sockfd );
exit( 0 );
}
else if ( child_pid > 0 )
close ( new_sockfd ) ;
else
close ( new_sockfd ) ;
/*sjjh ( new_sockfd );*/
}
}
sjjh(fd)
int fd;
{
int flag, FirstReadLen;
char pkl[20], jym[20], syhbh[20], tbtmp[512];
char frombank[FROMBANKLEN], tobank[TOBANKLEN];
char exestr[512];
char str[512];
void TIMEOUTread(), TIMEOUTwrite();
closefd=fd;
signal(SIGALRM,TIMEOUTread);
alarm(TIMEOUT);
FirstReadLen = read(fd, frombank, FROMBANKLEN);
if ( FirstReadLen <= 0 ) {
signal(SIGALRM,SIG_IGN);
return ;
}
signal(SIGALRM,SIG_IGN);
ltrim(frombank);
dzrtrim(frombank);
errout("银行上传",frombank,__FILE__,__LINE__,0);
getfield(frombank,1,pkl); ltrim(pkl); rtrim(pkl);
getfield(frombank,2,jym); ltrim(jym); rtrim(jym);
getfield(frombank,3,syhbh); ltrim(syhbh); rtrim(syhbh);
switch(atoi(jym)){
case XZCBSJ:
flag=xzcbsj(frombank,tobank);
if (flag==0)
sprintf(exestr,"sxzcbsj %d %d %ld",fd,cbyh,rq);
break;
case DRCBSJ:
flag=drcbsj(frombank,tobank,FirstReadLen);
if (flag==0)
sprintf(exestr,"sdrcbsj %d %d %ld %d",fd,cbyh,rq,zbs);
default :
flag=-1;
sprintf(tobank,"31|"
break;
}
if (flag) {
strcpy(tbtmp,tobank); rtrim(tbtmp);
sprintf(tobank,"%04d|%s",strlen(tbtmp)+5,tbtmp);
rtrim(tobank);
signal(SIGALRM,TIMEOUTwrite);
alarm(TIMEOUT);
flag =write ( fd, tobank, strlen(tobank) ) ;
errout("发送银行",tobank,__FILE__,__LINE__,0);
signal(SIGALRM,SIG_IGN);
/*close(fd);*/
}else{
rtrim(exestr);
flag=system(exestr);
}
/*if (flag<=0) autocz(jym);*/
}
void TIMEOUTwrite()
{
close(closefd);
errout("发送超时","",__FILE__,__LINE__,closefd);
exit(-1);
}
void TIMEOUTread()
{
close(closefd);
errout("接收超时","",__FILE__,__LINE__,closefd);
exit(-1);
}
int xzcbsj(f_bankcx,t_bankcx)
char *f_bankcx, *t_bankcx;
{
char cbystr[20], rqstr[20], pkl[20], jymstr[20];
getfield(f_bankcx,1,pkl); rtrim(pkl); ltrim(pkl);
getfield(f_bankcx,2,jymstr); rtrim(jymstr); ltrim(jymstr);
getfield(f_bankcx,3,cbystr); rtrim(cbystr); ltrim(cbystr);
getfield(f_bankcx,4,rqstr); rtrim(rqstr); ltrim(rqstr);
if (checkdig(cbystr)) {
errout("抄表员字段有误!",cbystr,__FILE__,__LINE__,999);
sprintf(f_bankcx,"37|"
return -1;
}
if (strlen(rqstr)!=8||rdefmtdate(&rq,"yyyymmdd",rqstr)){
errout("时期格式有误!",rqstr,__FILE__,__LINE__,999);
sprintf(f_bankcx,"37|"
return -1;
}
cbyh = atoi(cbystr);
sprintf(t_bankcx,"98|"
return 0;
}
int drcbsj(f_bankdz,t_bankdz, FirstRead)
char *f_bankdz, *t_bankdz;
int FirstRead;
{
int Ixym, i;
long FileLen, len;
char rqstr[20], jymstr[20], bsstr[20], cbystr[20], pkl[20], str[20];
char *data;
FILE *fp;
getfield(f_bankdz,1,pkl); rtrim(pkl); ltrim(pkl);
getfield(f_bankdz,2,jymstr); rtrim(jymstr); ltrim(jymstr);
getfield(f_bankdz,3,cbystr); rtrim(cbystr); ltrim(cbystr);
getfield(f_bankdz,4,rqstr); rtrim(rqstr); ltrim(rqstr);
getfield(f_bankdz,5,bsstr); rtrim(bsstr); ltrim(bsstr);
if (checkdig(bsstr)) {
errout("记录数字段有误!",bsstr,__FILE__,__LINE__,999);
sprintf(t_bankdz,"37|"
return -1;
}
if (checkdig(cbystr)) {
errout("抄表员字段有误!",cbystr,__FILE__,__LINE__,999);
sprintf(t_bankdz,"37|"
return -1;
}
if (strlen(rqstr)!=8||rdefmtdate(&rq,"yyyymmdd",rqstr)){
errout("时期格式有误!",rqstr,__FILE__,__LINE__,999);
sprintf(t_bankdz,"37|"
return -1;
}
zbs = atoi(bsstr);
cbyh = atoi(cbystr);
sprintf(cbjfilename,"%s/cbj%04d%s.txt", getenv("CBJDIR",cbyh,rqstr);
if ( (fp=fopen(cbjfilename,"wb")==NULL ){
fclose(fp);
errout("打开写文件失败!",cbjfilename,__FILE__,__LINE__,999);
sprintf(t_bankdz,"98|"
return -1;
}
fwrite(f_bankdz,FirstRead,1,fp); /* 写入第一包 */
FileLen = atol( pkl );
len = FileLen-FirstRead;
if ( len <=0 )
{
fclose(fp);
sprintf(t_bankdz,"98|"
return 0;
}
data = ( char * ) malloc ( len ) ;
for ( i = 0; i < len; i++ ) data = 0;
i=readn ( closefd, data, len );
if ( i>0 )
fwrite(data,len,1,fp);
fclose(fp);
sprintf(t_bankdz,"98|"
return 0;
}
int
readn ( int fd, char *ptr, long nbytes)
{
long nleft, nread;
int sockfd;
sockfd = fd;
nleft = nbytes;
while ( nleft > 0 ) {
nread = read (fd, ptr, nleft );
if ( nread < 0 )
return ( nread );
else
if ( nread == 0 )
break;
nleft -= nread;
ptr += nread;
}
return nbytes - nleft ;
}
#include "xyjh.h"
#include "cgasnet.h"
#define XZCBSJ 01
#define DRCBSJ 02
char cbjfilename[80];
int closefd, cbyh, zbs;
long rq;
main(argc,argv)
char *argv[];
int argc;
{
int srvport;
signal( SIGINT , SIG_IGN ) ;
signal( SIGHUP , SIG_IGN ) ;
signal( SIGCLD , SIG_IGN ) ;
if (argc!=2) {
printf("/n/n启动格式错误!/n/n"
exit(0);
}
srvport=atol(argv[1]);
if( fork() == 0 )
sub_main(srvport);
else
exit( 0 );
}
sub_main(port)
int port;
{
struct sockaddr_in s_add , con_s_add ;
struct servent *p_servent ;
int sockfd , new_sockfd ;
pid_t child_pid ;
int addr_len ;
int bind(),accept();
sockfd = socket ( AF_INET , SOCK_STREAM , IPPROTO_IP );
if ( sockfd < 0 )
{
perror ( " socket call error: " );
exit ( -1 );
}
s_add.sin_family = AF_INET ;
/*s_add.sin_port = htons( SRVPORT ) ;*/
s_add.sin_port = htons( port ) ;
s_add.sin_addr.s_addr = htonl( INADDR_ANY ) ;
if( bind ( sockfd , &s_add , sizeof( s_add ) ) < 0 )
{
perror ( " bind call error: "
exit ( -1 );
}
if ( listen ( sockfd , 5 ) < 0 )
{
perror ( "listen call error: " );
exit ( -1 );
}
addr_len = sizeof (con_s_add ) ;
printf ( "启动成功 ! /n/n" ) ;
while( 1 )
{
new_sockfd = accept ( sockfd , &con_s_add , &addr_len );
if ( new_sockfd < 0 )
{
perror ( " accept call error: " );
exit ( -1 );
}
if ( ( child_pid = fork() ) == 0 )
{
close ( sockfd );
sjjh( new_sockfd );
close ( new_sockfd );
exit( 0 );
}
else if ( child_pid > 0 )
close ( new_sockfd ) ;
else
close ( new_sockfd ) ;
/*sjjh ( new_sockfd );*/
}
}
sjjh(fd)
int fd;
{
int flag, FirstReadLen;
char pkl[20], jym[20], syhbh[20], tbtmp[512];
char frombank[FROMBANKLEN], tobank[TOBANKLEN];
char exestr[512];
char str[512];
void TIMEOUTread(), TIMEOUTwrite();
closefd=fd;
signal(SIGALRM,TIMEOUTread);
alarm(TIMEOUT);
FirstReadLen = read(fd, frombank, FROMBANKLEN);
if ( FirstReadLen <= 0 ) {
signal(SIGALRM,SIG_IGN);
return ;
}
signal(SIGALRM,SIG_IGN);
ltrim(frombank);
dzrtrim(frombank);
errout("银行上传",frombank,__FILE__,__LINE__,0);
getfield(frombank,1,pkl); ltrim(pkl); rtrim(pkl);
getfield(frombank,2,jym); ltrim(jym); rtrim(jym);
getfield(frombank,3,syhbh); ltrim(syhbh); rtrim(syhbh);
switch(atoi(jym)){
case XZCBSJ:
flag=xzcbsj(frombank,tobank);
if (flag==0)
sprintf(exestr,"sxzcbsj %d %d %ld",fd,cbyh,rq);
break;
case DRCBSJ:
flag=drcbsj(frombank,tobank,FirstReadLen);
if (flag==0)
sprintf(exestr,"sdrcbsj %d %d %ld %d",fd,cbyh,rq,zbs);
default :
flag=-1;
sprintf(tobank,"31|"
break;
}
if (flag) {
strcpy(tbtmp,tobank); rtrim(tbtmp);
sprintf(tobank,"%04d|%s",strlen(tbtmp)+5,tbtmp);
rtrim(tobank);
signal(SIGALRM,TIMEOUTwrite);
alarm(TIMEOUT);
flag =write ( fd, tobank, strlen(tobank) ) ;
errout("发送银行",tobank,__FILE__,__LINE__,0);
signal(SIGALRM,SIG_IGN);
/*close(fd);*/
}else{
rtrim(exestr);
flag=system(exestr);
}
/*if (flag<=0) autocz(jym);*/
}
void TIMEOUTwrite()
{
close(closefd);
errout("发送超时","",__FILE__,__LINE__,closefd);
exit(-1);
}
void TIMEOUTread()
{
close(closefd);
errout("接收超时","",__FILE__,__LINE__,closefd);
exit(-1);
}
int xzcbsj(f_bankcx,t_bankcx)
char *f_bankcx, *t_bankcx;
{
char cbystr[20], rqstr[20], pkl[20], jymstr[20];
getfield(f_bankcx,1,pkl); rtrim(pkl); ltrim(pkl);
getfield(f_bankcx,2,jymstr); rtrim(jymstr); ltrim(jymstr);
getfield(f_bankcx,3,cbystr); rtrim(cbystr); ltrim(cbystr);
getfield(f_bankcx,4,rqstr); rtrim(rqstr); ltrim(rqstr);
if (checkdig(cbystr)) {
errout("抄表员字段有误!",cbystr,__FILE__,__LINE__,999);
sprintf(f_bankcx,"37|"
return -1;
}
if (strlen(rqstr)!=8||rdefmtdate(&rq,"yyyymmdd",rqstr)){
errout("时期格式有误!",rqstr,__FILE__,__LINE__,999);
sprintf(f_bankcx,"37|"
return -1;
}
cbyh = atoi(cbystr);
sprintf(t_bankcx,"98|"
return 0;
}
int drcbsj(f_bankdz,t_bankdz, FirstRead)
char *f_bankdz, *t_bankdz;
int FirstRead;
{
int Ixym, i;
long FileLen, len;
char rqstr[20], jymstr[20], bsstr[20], cbystr[20], pkl[20], str[20];
char *data;
FILE *fp;
getfield(f_bankdz,1,pkl); rtrim(pkl); ltrim(pkl);
getfield(f_bankdz,2,jymstr); rtrim(jymstr); ltrim(jymstr);
getfield(f_bankdz,3,cbystr); rtrim(cbystr); ltrim(cbystr);
getfield(f_bankdz,4,rqstr); rtrim(rqstr); ltrim(rqstr);
getfield(f_bankdz,5,bsstr); rtrim(bsstr); ltrim(bsstr);
if (checkdig(bsstr)) {
errout("记录数字段有误!",bsstr,__FILE__,__LINE__,999);
sprintf(t_bankdz,"37|"
return -1;
}
if (checkdig(cbystr)) {
errout("抄表员字段有误!",cbystr,__FILE__,__LINE__,999);
sprintf(t_bankdz,"37|"
return -1;
}
if (strlen(rqstr)!=8||rdefmtdate(&rq,"yyyymmdd",rqstr)){
errout("时期格式有误!",rqstr,__FILE__,__LINE__,999);
sprintf(t_bankdz,"37|"
return -1;
}
zbs = atoi(bsstr);
cbyh = atoi(cbystr);
sprintf(cbjfilename,"%s/cbj%04d%s.txt", getenv("CBJDIR",cbyh,rqstr);
if ( (fp=fopen(cbjfilename,"wb")==NULL ){
fclose(fp);
errout("打开写文件失败!",cbjfilename,__FILE__,__LINE__,999);
sprintf(t_bankdz,"98|"
return -1;
}
fwrite(f_bankdz,FirstRead,1,fp); /* 写入第一包 */
FileLen = atol( pkl );
len = FileLen-FirstRead;
if ( len <=0 )
{
fclose(fp);
sprintf(t_bankdz,"98|"
return 0;
}
data = ( char * ) malloc ( len ) ;
for ( i = 0; i < len; i++ ) data = 0;
i=readn ( closefd, data, len );
if ( i>0 )
fwrite(data,len,1,fp);
fclose(fp);
sprintf(t_bankdz,"98|"
return 0;
}
int
readn ( int fd, char *ptr, long nbytes)
{
long nleft, nread;
int sockfd;
sockfd = fd;
nleft = nbytes;
while ( nleft > 0 ) {
nread = read (fd, ptr, nleft );
if ( nread < 0 )
return ( nread );
else
if ( nread == 0 )
break;
nleft -= nread;
ptr += nread;
}
return nbytes - nleft ;
}