64,654
社区成员
发帖
与我相关
我的任务
分享
int slen = sizeof(struct sockaddr);
struct timeval timeo;
socklen_t iLen = sizeof(timeo);
bool MyIni(int & cid,const string & ip,const int & port)
{
if(cid>0)
{
close(cid);
cid = 0;
}
struct sockaddr_in c_addr;
bzero(&c_addr,sizeof(c_addr));
c_addr.sin_family=AF_INET;
c_addr.sin_port=htons(port);
c_addr.sin_addr.s_addr = inet_addr(ip.c_str());
if((cid=socket(AF_INET,SOCK_STREAM,0))==-1) return false;
fd_set set;
FD_ZERO(&set);
FD_SET(cid,&set);
timeval tm;
tm.tv_sec = 1;
tm.tv_usec = 50000;
if(select(cid+1, NULL, &set, NULL, &timeo)==-1){close(cid);cid=0;return false;}
signal(SIGPIPE, SIG_IGN);
if(connect(cid,(struct sockaddr *)(&c_addr),slen)==-1)
{
close(cid);
cid = 0;
return false;
}
return true;
}
bool SendFF(int & cid,const string & msg,int & l,const int & fn,const string & ip,const int & port,string & str)
{
if(cid<=0 && !MyIni(cid,ip,port)) return false;
if (setsockopt(cid,SOL_SOCKET,SO_SNDTIMEO,&timeo,iLen) == -1) return false;
if((l=send(cid,msg.c_str(),msg.length(),MSG_NOSIGNAL))<=0)
{
if(!MyIni(cid,ip,port)) return false;
if((l=send(cid,msg.c_str(),msg.length(),MSG_NOSIGNAL))<=0) return false;
}
char buf[50];
str = "";
int len=0,k=its(fn).length();
if (setsockopt(cid,SOL_SOCKET,SO_RCVTIMEO,&timeo,iLen) == -1) return false;
while((l = recv(cid,buf,50,0))>0)
{
str += string(buf,l);
len += l;
if(len >= k) break;
}
if(str.length()>k) str = str.substr(0,k);
if(atoi(str.c_str()) == fn) return true;
else return false;
}
bool SendF(int & cid,const string & msg,int & l,const int & fn,const string & ip,const int & port,string & str)
{
if(!SendFF(cid,msg,l,fn,ip,port,str))
{
if(cid>0)
{
close(cid);
cid=0;
}
return false;
}
return true;
}
int slen = sizeof(struct sockaddr);
struct timeval timeo;
socklen_t iLen = sizeof(timeo);
bool MyIni(struct sockaddr_in & c_addr,int & cid,const string & ip,const int & port)
{
if(cid>0)
{
close(cid);
cid = 0;
}
bzero(&c_addr,sizeof(c_addr));
c_addr.sin_family=AF_INET;
c_addr.sin_port=htons(port);
c_addr.sin_addr.s_addr = inet_addr(ip.c_str());
if((cid=socket(AF_INET,SOCK_STREAM,0))==-1) return false;
fd_set set;
FD_ZERO(&set);
FD_SET(cid,&set);
timeval tm;
tm.tv_sec = 1;
tm.tv_usec = 50000;
select(cid+1, NULL, &set, NULL, &timeo);
signal(SIGPIPE, SIG_IGN);
if(connect(cid,(struct sockaddr *)(&c_addr),slen)==-1)
{
close(cid);
cid = 0;
return false;
}
return true;
}
bool SendFF(struct sockaddr_in & c_addr,int & cid,const string & msg,int & l,const int & fn,const string & ip,const int & port,string & str)
{
if(cid<=0 && !MyIni(c_addr,cid,ip,port)) return false;
if (setsockopt(cid,SOL_SOCKET,SO_SNDTIMEO,&timeo,iLen) == -1) return false;
if((l=send(cid,msg.c_str(),msg.length(),MSG_NOSIGNAL))<=0)
{
if(!MyIni(c_addr,cid,ip,port)) return false;
if((l=send(cid,msg.c_str(),msg.length(),MSG_NOSIGNAL))<=0) return false;
}
char buf[50];
str = "";
int len=0,k=its(fn).length();
if (setsockopt(cid,SOL_SOCKET,SO_RCVTIMEO,&timeo,iLen) == -1) return false;
while((l = recv(cid,buf,50,0))>0)
{
str += string(buf,l);
len += l;
if(len >= k) break;
}
if(str.length()>k) str = str.substr(0,k);
if(atoi(str.c_str()) == fn) return true;
else return false;
}
bool SendF(struct sockaddr_in & c_addr,int & cid,const string & msg,int & l,const int & fn,const string & ip,const int & port,string & str)
{
if(!SendFF(c_addr,cid,msg,l,fn,ip,port,str))
{
if(cid>0)
{
close(cid);
cid=0;
}
return false;
}
return true;
}
void * SendTo(void * args)
{
pthread_detach(pthread_self());
if(Server36) return NULL;
long k=(long)args;
int i,cid=0,j,np=0,port=atoi(vSendTo[k][2].c_str());
string ss,s,ip=vSendTo[k][1];
struct sockaddr_in c_addr;
timeo.tv_sec = 1;
while(true)
{
while(np==sPos) usleep(20000);
s="";i=0;
while(np!=sPos)
{
s += ","+its(DataSend[np][0].length())+","+DataSend[np][0]+","+its(DataSend[np][1].length())+","+DataSend[np][1];
i++;
if(++np>=IPDLEN) np = 0;
if(s.length()>512000) break;
}
s = its(i)+s;
ss = s+CheckCode;
for(j=ss.length();j<96;j++) ss += " ";
ss += "end"+s;
if(!SendF(c_addr,cid,ss,j,i,ip,port,s))
{
usleep(20000);
if(!SendF(c_addr,cid,ss,j,i,ip,port,s))
{
usleep(20000);
if(!SendF(c_addr,cid,ss,j,i,ip,port,s)) continue;
}
}
}
}
局部变量传什么引用?
bool SendFF(int & cid,
void * SendTo(void * args)
{
pthread_detach(pthread_self());
long k=(long)args;
int i,cid=0,j,np=0,port=atoi(vSendTo[k][2].c_str());
string ss,s,ip=vSendTo[k][1];
timeo.tv_sec = 1;
while(true)
{
while(np==sPos) usleep(20000);
s="";i=0;
while(np!=sPos)
{
s += ","+its(DataSend[np][0].length())+","+DataSend[np][0]+","+its(DataSend[np][1].length())+","+DataSend[np][1];
i++;
if(++np>=IPDLEN) np = 0;
if(s.length()>512000) break;
}
s = its(i)+s;
ss = its(s.length())+","+CheckCode;
if(!SendF(cid,ss,j,i,ip,port,s))
{
usleep(20000);
if(!SendF(cid,ss,j,i,ip,port,s))
{
usleep(20000);
if(!SendF(cid,ss,j,i,ip,port,s)) continue;
}
}
}
}