一个报文发送的问题

lcq_6075217 2007-12-29 10:17:17
void * serverproc(void *Conn)
{

CONNINFO pConn;
TERM_BUF slTermbuf; /* 报文结构 */
pthread_t my_pthread_id;

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);
}

alMsgbuf[ilMsglen]='\0';
if (strncmp(alMsgbuf,"",FLAG_LEN)=0)
{
continue;
}

}
}


有服务器发送报文,客户端接受报文,这是客户端程序。
服务器端重启以后,报 关闭连接[%d],接受二级报文错误! 的错误信息,怎样实现自动连接。

...全文
172 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcq_6075217 2008-05-14
  • 打赏
  • 举报
回复
即是客户端也是服务器端 双向通信
taoyi921 2008-05-14
  • 打赏
  • 举报
回复
上面是客户端程序?为什么要accept(igSocket,(struct sockaddr *)&addr,&len);
lcq_6075217 2008-05-13
  • 打赏
  • 举报
回复
char aYLip[32];
int iYLport;
int iYLSockfd=-1;

int connect_server()
{
CONNECT:
if (iYLSockfd<0)
{
iYLSockfd=etsConnectser(aYLip,iYLport);
if (iYLSockfd<0)
{
etsDebug(0, "调用etsConnectser 出错etsConnectSerTime[%s][%d][%d]iTimeout[%d] ,errno:%d",aYLip,iYLport, iYLSockfd,iTimeout,errno );
etsDebug(1, "错误代码etsConnectSerTime[%s]",strerror(errno));
sleep(3);
goto CONNECT;
}
else
{
etsDebug(0,"向二级[%s:%d]建立连接成功",aYLip,iYLport);
}
}
else
{
/*检测Socket是否可用*/
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;
}




main(int argc, char *argv[])
{
char alPortname[21]; /* 通信端名称 */
short ilQid; /* 邮箱号 */
short ilQid_fore; /* 前置邮箱号 */
struct sockaddr_in addr;
int len;

CONNINFO *pConn;
pthread_t tThd;
int AcceptNum=0;
int newsock;
TERM_BUF alMsgbuf;
char ipaddr[16];
struct linger ilLinger;
int one=1;

/*建立服务端连接并侦听客户端*/
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)
{
int ilSockfd,sockfd;
char alMsgbuf[MSGMAXLEN]; /* 报文 */
short ilMsglen; /* 报文长度 */
char aMsglen[8+1];
char jym[6+1];
unsigned char aRealBuf[3096];/*解压缩后报文*/
unsigned int iLen;/*解压缩后报文的长度*/
struct timeb tb;
struct timeval rwto;
int rc,iDownflag=0;
int i,iThno=0;
char ipaddr[40];
char aMsgflag[FLAG_LEN+1];
char acoil_no[COIL_NO_LEN+1];

CONNINFO pConn;
TERM_BUF slTermbuf; /* 报文结构 */
pthread_t my_pthread_id;

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]",conn.ipaddr,ilSockfd,ilMsglen,alMsgbuf);
}
}

我把一些主要的逻辑贴了出来,楼上说的哪些问题我都已经编译通过了,不好意思没有发好。
taoyi921 2008-05-13
  • 打赏
  • 举报
回复
你的代码好乱,好像还有不少语法问题.
如: if (strncmp(alMsgbuf,"",FLAG_LEN)=0)
这里应该是==
还有connect_server() 后面没有分号
还有connect_server里面的iYLSockfd没有定义,而且这个socket没有传递给 serverproc,肯定不能重连,serverproc中的ilSockfd也没有定义,你还是先编译通过了吧.
lcq_6075217 2008-05-11
  • 打赏
  • 举报
回复
int connect_server()
{
CONNECT:
if (iYLSockfd<0)
{
iYLSockfd=etsConnectser(aYLip,iYLport);
if (iYLSockfd<0)
{
etsDebug(0, "调用etsConnectser 出错etsConnectSerTime[%s][%d][%d]iTimeout[%d] ,errno:%d",aYLip,iYLport, iYLSockfd,iTimeout,errno );
etsDebug(1, "错误代码etsConnectSerTime[%s]",strerror(errno));
sleep(3);
goto CONNECT;
}
else
{
etsDebug(0,"向二级[%s:%d]建立连接成功",aYLip,iYLport);
}
}
else
{
/*检测Socket是否可用*/
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;
}



void * serverproc(void *Conn)
{

CONNINFO pConn;
TERM_BUF slTermbuf; /* 报文结构 */
pthread_t my_pthread_id;

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); */

connect_server()
}

alMsgbuf[ilMsglen]='\0';
if (strncmp(alMsgbuf,"",FLAG_LEN)=0)
{
continue;
}

}
}
这样能实现自动重连吗

taoyi921 2007-12-29
  • 打赏
  • 举报
回复
你的connect在哪?
服务器端重启,关闭连接可以不用退出线程,而是在for循环中继续尝试连接(当然要记得连接不上时的处理),这样服务器重启后就可以自动连上了

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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