- 论坛徽章:
- 0
|
某银行卡业务前置机代码片断
//server 端
#define "com.h"
# define PKTRTNMSGLEN 16
#define LISTENPORT 8101
int RecvSock; /* 接收的套接字 */
int s; /* 监听套接字 */
char reportdate[9]; /* 报表日期 */
char createtime[30]; /* 报表生成日期时间 */
char frptdate[5];
/* 报表名的日期 MMDD */
void settle_exit();
void f_child_exit();
int main()
{
int iRet,len;
int addrlen, i;
char recbuffer[5000],fname[30];
struct sockaddr_in peeraddr_in;
char tmp[80];
memset(recbuffer,0,sizeof(recbuffer));
memset(fname,0,30);
addrlen = sizeof(struct sockaddr_in);
s=tcp_listen(LISTENPORT,5,5000);
if(s<=0 )
{
perror("\nRequest init failed" ;
return -1;
}
signal(SIGUSR1, settle_exit); /* 设置信号启动退出函数 */
strcpy(fname,"chenny.txt" ;
while(1)
{
memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
RecvSock = accept(s, (struct sockaddr *)&peeraddr_in, &addrlen);
if (RecvSock < 0)
{ /* accept()错误, 记录日志 */
perror("accept error\n" ;
sleep(1);
continue;
}
switch(fork())
{
case -1: /* fork()错误, 记录日志 */
perror("fork error\n" ;
sleep(1);
break;
case 0: /* clild process */
tcpclose(s);/* 关闭监控的套接字 */
rec_data(RecvSock,recbuffer,fname);
printf("receive recbuffer=[%s]\n",recbuffer);
fflush(stdout);
exit(0);
break;
default: /* parent process */
break;
}
tcpclose(RecvSock); /* 关闭接收的套接字 */
}
}
int rec_data(sock,RecData,filename)
int sock;
char *RecData,*filename;
{
signal(SIGALRM, f_child_exit);
cutblank(filename);
if( strlen(filename) >;0 )
{
alarm(60);
if ( rcvfile(sock,filename,120) ==0)
{
printf("接收文件[%s]成功!\n",filename);
fflush(stdout);
}
else
{
sock_log("接收文件失败!",__LINE__,__FILE__);
printf("接收文件失败[%s]!\n",filename);
fflush(stdout);
return -1;
}
}
if( RecData != NULL )
{
if ( RcvData(sock,RecData,20) == 0)
{
printf("接收数据成功!\n" ;
printf("[%s]\n",RecData );
}
else
{
sock_log("接收数据失败!",__LINE__,__FILE__);
printf("接收数据失败!\n" ;
fflush(stdout);
return -1;
}
}
return 0;
}
void settle_exit()
{
close(s); /* 关闭监控的套接字 */
close(RecvSock); /* 关闭接收套接字 */
kill(0, SIGUSR1); /* kill all child processes */
printf(" Settle Shutdown!\n" ;
exit(0);
}
void f_child_exit()
{
alarm(0); /* 取消连接超时设置 */
signal(SIGALRM, SIG_IGN);
close(RecvSock); /* 关闭接收套接字 */
printf( "timeout!\n" ;
exit(-1);
}
//client端
#include <com.h>;
main(argc,argv)
int argc;
char *argv[];
{
char szServerAddr[16];
int nzListeningPort;
int sock,i;
struct sockaddr_in serv_addr;
char aczRecBuf[10],aczData[5000],recdata[5000];
char temp[80],*tmp;
struct chen{
unsigned short chen4;
char chen1[10];
long chen3;
char chen2[10];
}chen;
strcpy(chen.chen1,"abcdefghi" ;
strcpy(chen.chen2,"jklmnopqr" ;
chen.chen3=123;
chen.chen4=456;
memset(&serv_addr,0,sizeof(serv_addr));
memset(temp, 0, sizeof(temp));
memset(aczData,0,sizeof(aczData));
memset(recdata,0,sizeof(recdata));
/* 发送 */
sprintf(szServerAddr, "%s", "88.1.16.102");
nzListeningPort = 7102;
printf("Server addr=[%s], port=[%d]\n", szServerAddr, nzListeningPort);
strcpy(aczData,"陈年以哦卡沃你打 I love you very much !\n");
strcat(aczData,"毛主席好,你好我好大家好陈是猪头八,我要回去啦!!!!!");
printf("%s,len=[%d]\n",aczData,strlen(aczData));
fflush(stdout);
memcpy(&tmp,&chen,sizeof(chen));
if ( send_recv2(szServerAddr,nzListeningPort,&tmp,sizeof(chen),&chen,sizeof(chen))==0)
{
printf("发送接收数据成功!\n");
fflush(stdout);
/*printf("\nrecdata=[%s]\n",recdata);*/
printf("chen.chen1=[%s],chen.chen2=[%s]\n",chen.chen1,chen.chen2);
printf("chen.chen3=[%d],chen.chen4=[%d]\n",chen.chen3,chen.chen4);
fflush(stdout);
}
else
{
printf("发送接收数据失败!\n");
fflush(stdout);
}
return;
} |
|