Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

iOS BSD Socket

用tcp请求网页的实验,预期是接收到长度0时,就跳出循环。问题是打印接收到长度215后,就卡住了,等了1分钟才会打印收到长度0并跳出。为什么会等1分钟呢?

`

const char* msg = str.UTF8String;
ssize_t len = send(self.client, msg, strlen(msg), 0);
NSLog(@"发送了长度:%ld", len);

uint8_t buf[1024];
ssize_t count = 1;

do {
    
    count = recv(self.client, buf, sizeof(buf), 0);
    
    NSLog(@"收到长度:%zd", count);
    
} while (count != 0);

NSLog(@"skip");

`

打印:
`

2021-01-23 16:48:26.243796+0800 sock[1058:34645] 发送了长度:39
2021-01-23 16:48:26.262762+0800 sock[1058:34645] 收到长度:1024
2021-01-23 16:48:26.267321+0800 sock[1058:34645] 收到长度:1024
2021-01-23 16:48:26.278972+0800 sock[1058:34645] 收到长度:215

(这里等了很久,多次尝试都是差1分钟)

2021-01-23 16:49:26.255403+0800 sock[1058:34645] 收到长度:0
2021-01-23 16:49:26.256017+0800 sock[1058:34645] skip`

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...