socket自动重连问题(急用 谢谢)

lcq_6075217 2008-05-05 09:28:54
int iYLSockfd=-1;

int connect_server()
{
CONNECT:
if (iYLSockfd<0)
{
iYLSockfd=etsConnectser(aYLip,iYLport);
if (iYLSockfd<0)
{
etsDebug(0, " ",aYLip,iYLport,iYLSockfd,iTimeout,errno );
etsDebug(1, "错误代码etsConnectSerTime[%s]",strerror(errno));
sleep(3);
goto CONNECT;
}
else
{etsDebug(0,"向二级[%s:%d]建立连接成功",aYLip,iYLport);}
else
{
int ilRn=socket_stat( iYLSockfd , 0 );
if (ilRn != 0){
close(iYLSockfd);
etsDebug(0,"与二级[%s:%d]建立连接状态不正常sockfd[%d]",aYLip,iYLport,iYLSockfd);
iYLSockfd=-1;
goto CONNECT;
}
}
return iYLSockfd;
}

int send_back(char* alMsgbuf,int ilMsglen)
{
if (connect_server()>0)
{
etsDebug(0,"与二级[%s:%d]建立连接状态正常sockfd[%d],开始发送长度为[%d]的报文",aYLip,iYLport,iYLSockfd,ilMsglen);

int ilRn = etsTcpsnd(iYLSockfd, alMsgbuf, ilMsglen);
if (ilRn == 0) {
etsDebug(0,"调用函数etsTcpSnd [%d] len [%d] 向二级发送业务回执成功 IP=[%s]",iYLSockfd,ilMsglen,aYLip);
return 0;
}
else {
/*通讯失败处理*/
close(iYLSockfd);
iYLSockfd=-1;
etsDebug(0,"调用函数etsTcpSnd port[%d] len [%d] 向二级发送通讯回执失败 rc[%d] IP=[%s]",iYLSockfd,ilMsglen,ilRn,aYLip);
return -1;
}
}
return -1;
}

int swDbopen();


main(int argc, char *argv[])
{

/* 连接数据库 */
int iRc = swDbopen();
if (iRc)
{
etsDebug(0,"swDbopen():失败!");
exit(-1);
}

/* 设置调试程序名称 */
memset(agDebugfile, 0x00, sizeof(agDebugfile));
sprintf(agDebugfile, "%s.debug",argv[0]);
strcpy( DebugFilen2,"CupsCheck.debug");


sprintf(alcfname,"%s/%s",getenv("HOME"),"jigang/etc/ServerCNN.ini");
if (GetProfileString(alcfname,"CUPS","DEBUG",aTmp) < 0)
{
etsDebug(0,"读DEBUG出错![%d]",errno);
fprintf(stderr,"读DEBUG出错出错![%d]",errno);
exit(-1);
}
cgDebug=atoi(aTmp);


etsDebug(3,"DEBUG文件[%s]",agDebugfile);
etsDebug(3,"DEBUG标志[%d]",cgDebug);

/*add by pc 20060311 连接超时时间必须得到*/
if (GetProfileString(alcfname,"CUPS","CONNECT_TIME",aTimeOut) < 0)
{
etsDebug(0,"读连接网关超时时间出错![%d]",errno);
fprintf(stderr,"读连接网关超时时间出错![%d]",errno);
exit(-1);
}
else {
/*设置连接超时时间*/
iTimeout=atoi(aTimeOut);
if ((iTimeout>=60) || (iTimeout<=0)) {
iTimeout=30;
}
}
if (GetProfileString(alcfname,"CONNServer","ServerRemoteHost",aYLip) < 0)
{
etsDebug(0,"读二级 IP出错![%s][%d]",alcfname,errno);
fprintf(stderr,"读二级 IP出错![%s][%d]",aYLip,errno);
exit(-1);
}

if (GetProfileString(alcfname,"CONNServer","ServerRemotePort",alATCport) < 0)
{
etsDebug(0,"读二级 监听端口号出错![%s][%d]",alcfname,errno);
fprintf(stderr,"读二级 监听端口号出错![%s][%d]",alcfname,errno);
exit(-1);
}
iYLport = atoi(alATCport);

if (GetProfileString(alcfname,"CUPS","THREADNUM",aThreadNum) < 0)
{
thread_nums=10;
memset(aThreadNum,0x00,sizeof(aThreadNum));
sprintf(aThreadNum,"%d",thread_nums);
etsDebug(0,"读线程数出错![%d],使用默认值10",errno);
}
else {
thread_nums=atoi(aThreadNum);
}
if (thread_nums == 0)
thread_nums = DEFINE_THREADS;
else if (thread_nums > MAX_THREADS)
thread_nums = MAX_THREADS;
APP_MAX_SYNCONNCOUNT = thread_nums*50;
if ( APP_MAX_SYNCONNCOUNT > SYS_MAX_SYNCONNCOUNT )
APP_MAX_SYNCONNCOUNT = SYS_MAX_SYNCONNCOUNT;
else if (APP_MAX_SYNCONNCOUNT < SYS_MIN_SYNCONNCOUNT)
APP_MAX_SYNCONNCOUNT = SYS_MIN_SYNCONNCOUNT;

etsDebug(0, "二级 IP: aYLip = [%s]", aYLip);
etsDebug(0, "二级 端口: alATCport = [%s]", alATCport);
etsDebug(0, "应答队列键: aReqid = [%s]", aReqid);
etsDebug(0, "响应队列键: aRespid = [%s]", aRespid);
etsDebug(0, "线程数: aThreadNum = [%s]",aThreadNum);
etsDebug(0, "三级监听端口: aListenPort = [%s]",aListenPort);

/*请求消息队列*/

if ((q_reqid = msgget(atoi(aReqid),IPC_CREAT|QUEUE_PERM)) <0)
{
etsDebug(0,"创建应答消息队列出错 msgget() error:[%d], 队列键值[%s]!\n",errno,aReqid);
exit(-1);
}

/*响应消息队列*/

if ((q_respid = msgget(atoi(aRespid),IPC_CREAT|QUEUE_PERM)) <0)
{
etsDebug(0,"创建响应消息队列出错 msgget() error:[%d], 队列键值[%s]!\n",errno,aRespid);
exit(-1);
}


etsDebug(1,"打开消息队列成功! reqid=[%d],respid=[%d]",q_reqid,q_respid,q_flowid);


/*建立服务端连接并侦听客户端*/
igSocket=-1;
ilPort=atoi(aListenPort);
igSocket = etsTcpconnect(ilPort);
if (igSocket < 0) {
etsDebug(0, "建立服务端侦听套接字失败!");
etsQuitw();
}
else {
etsDebug(0,"建立监听成功,端口号[%d]",ilPort);
}

for (j=0;j<64;j++)
signal(j,SIG_IGN);

signal(SIGTERM, etsQuitw);
signal(SIGINT, etsQuitw);

AcceptNum=0;
RecordPid(argv[0]);
pthread_setconcurrency(51*thread_nums +2);

connect_server();


for(;;){
len=sizeof(struct sockaddr_in);
bzero((char *)&addr,len);

newsock=accept(igSocket,(struct sockaddr *)&addr,&len);
if ( newsock<0 ){
etsDebug(0,"调用函数 accept 出错![%d]",errno);
sleep(2);
continue;
}

pConn=(CONNINFO *)malloc(sizeof(CONNINFO)+1);
memset((char *)pConn,0,sizeof(pConn));
pConn->sockfd=newsock;
sprintf(pConn->ipaddr, "%s", inet_ntoa(addr.sin_addr));

rc=pthread_create(&tThd,NULL,&serverproc,pConn);
if ( rc ){
etsDebug(0,"创建三级接受线程出错![%d]",errno);
close(newsock);
}


}
}



void * serverproc(void *Conn)
{
pConn=*(CONNINFO *)Conn;
free(Conn);

/* 通过父线程发送SIGUSR1 结束本线程 **/
signal(SIGUSR1, pExit);

my_pthread_id = pthread_self();
pthread_detach(pthread_self());
memset(&slTermbuf, 0x00, sizeof(TERM_BUF));
memset(alMsgbuf, 0x00, sizeof(alMsgbuf));

ilSockfd = pConn.sockfd;
memset(ipaddr,0x00,sizeof(ipaddr));

etsDebug(2,"接收到二级连接请求socket[%d] IP=[%s]",ilSockfd,pConn.ipaddr);


iThno=0;
for(;;){
iDownflag=0;
ilMsglen=0;
ilMsglen = etsTcprcv(ilSockfd,alMsgbuf,JGMSGLEN);
etsDebughex(5, alMsgbuf,JGMSGLEN);
if ( ilMsglen<=0 ){
/*前台退出发来的信号,读出的数据不对。关连接*/
close(ilSockfd);
etsDebug(0,"关闭连接[%d],接受二级报文错误![%d] errno=[%d] IP=[%s]",ilSockfd,ilMsglen,errno,pConn.ipaddr);
pthread_exit(NULL);

}
send_back(alMsgbuf,ilMsglen);
memcpy(aMsgflag,alMsgbuf,FLAG_LEN);
memcpy(acoil_no,alMsgbuf+FLAG_LEN,COIL_NO_LEN);
proc_message(aMsgflag,acoil_no);
alMsgbuf[ilMsglen]='\0';
etsDebug(0, "接收二级报文成功 IP=[%s] socket=[%d]len[%d]alMsgbuf=[%s]", pConn.ipaddr,ilSockfd,ilMsglen,alMsgbuf);


}
}

void etsQuitw()
{
int i;
int ilRc;
etsDebug(0,"Exit.......main");
close(igSocket);
for (i=igSocket+1;i<10;i++){
ilRc=close(i);
etsDebug(0,"关闭socket[%d] errno[%d][%s]",i,errno,strerror);
if (ilRc) break;
}
exit(0);
}


对方网络断开后 报etsDebug(0,"关闭连接[%d],接受二级报文错误![%d] errno=[%d] IP=[%s]",ilSockfd,ilMsglen,errno,pConn.ipaddr);
...全文
268 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,127

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧