网络判断

在进行数据连接时候需要进行网络状态判断

//判断网络连接

+ (BOOL) isConnectionAvailable{

NSURL*url1 = [NSURLURLWithString:@"http://www.baidu.com"];

NSURLRequest*request = [NSURLRequestrequestWithURL:url1cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheDatatimeoutInterval:5];

NSHTTPURLResponse*response;

[NSURLConnectionsendSynchronousRequest:requestreturningResponse: &responseerror:nil];

if(response ==nil) {

returnNO;

}

else{

returnYES;

}

}

//直接通过BOOL值判断网络是不是真的连通!

-(BOOL)TryCheckNetCanUse {

intsockfd =socket(AF_INET,SOCK_STREAM,0);if(sockfd <0) {

// [self showNetworkStatus:@"未联网"];

returnNO;

}

if([selfsocket_set_non_blocking:sockfd]<0) {

// [self showNetworkStatus:@"未联网"];return NO;

}

inti =0;

while(i<5) {

i++;

//http://61.135.169.121/百度,端口80

if([selftry_connect:sockfdip:"61.135.169.121"port:80timeout:3] ==SUCCESS){

returnYES;

break;

}else{

returnNO;

}

}

returnYES;

}

//通过ip和port连接

-(int)try_connect:(int)sockfd ip:(constchar*)ipport:(unsignedshort)port timeout:(int) timeout{

structsockaddr_inserver_addr;

memset(&server_addr,0,sizeof(server_addr));

server_addr.sin_family=AF_INET;

server_addr.sin_port=htons(port);

if(inet_pton(AF_INET, ip, &server_addr.sin_addr) <0)

returnFAIL;

structtimevaltv;

fd_setwset;

if(connect(sockfd, (structsockaddr*)&server_addr,sizeof(server_addr)) <0) {

if(errno!=EINPROGRESS&&errno!=EWOULDBLOCK)

returnFAIL;

tv.tv_sec= timeout;

tv.tv_usec=0;

FD_ZERO(&wset);

FD_SET(sockfd, &wset);

intn =select(sockfd +1,NULL, &wset,NULL, &tv);

if(n <0) {/* select出错*/

returnFAIL;

}elseif(n ==0) {

/*超时*/

returnFAIL;

}else{

returnSUCCESS;

}

}

intflags;

//在connect成功之后,设成阻塞模式flags = fcntl(sockfd, F_GETFL,0);

flags &= ~O_NONBLOCK;

fcntl(sockfd,F_SETFL, flags);/***************************************************///设置不被SIGPIPE信号中断,物理链路损坏时才不会导致程序直接被Terminate

//在网络异常的时候如果程序收到SIGPIRE是会直接被退出的。

structsigactionsa;

sa.sa_handler=SIG_IGN;

sigaction(SIGPIPE, &sa,0);

//----这个bug折磨死我了----!/***************************************************/

unsignedcharbytes[]={83,83,72,45,50,46,48,45,79,112,101,110,83,83,72,95,53,46,50,13,10,0};

if(send(sockfd, bytes,strlen((char*)bytes),0)<0) {

returnFAIL;

}charbuff[128];

//设置接收时间超时

structtimevalRectimeout={3,0};//3s

intret=setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,(constchar*)&Rectimeout,sizeof(Rectimeout));//如果ret==0则为成功,-1为失败,这时可以查看errno来判断失败原因

if(ret <0) {

returnFAIL;

}

longretr =recv(sockfd, buff,128,0);

if(retr ==-1&&errno==EAGAIN){printf("Recvive timeout\n");

returnFAIL;}

NSLog(@"retr is---->%ld buff is ---> %s",retr,buff);if(retr >0) {

if(strncmp(buff,"SSH",3)==0) {NSLog(@" connect is --->%s",buff);

//[self showNetworkStatus:@"已经联网了"];close(sockfd);

returnSUCCESS;

}

}

close(sockfd);

// [self showNetworkStatus:@"未连接网络!"];

returnFAIL;

}

//检测阻塞还是非阻塞

-(int) socket_set_non_blocking:(int)socked{

intflags;

flags =fcntl(socked,F_GETFL,0);

if(flags == -1) {

return-1;

}

flags |=O_NONBLOCK;

if(fcntl(socked,F_SETFL, flags) == -1) {

return-1;

}

return0;

}

你可能感兴趣的:(网络判断)