我对套接字中的 recv() 函数有疑问(在 linux Raspberri Pi 上)
为什么我的程序停在:
if ((numbytes = recv(fd, odp, 100, 0)) == -1) {
printf("\n error while test recv 1");
perror("recv");
reconnect = 1;
}
是的,有一个错误:“资源暂时不可用”
当我看见:
printf("\n error while test recv 1");
我想处理重新连接稍后制作的内容。但是我在终端窗口上看到我的程序停止:
error while test recv 1
我试过:
signal(SIGPIPE, SIG_IGN);
比:
signal(SIGPIPE, my_function);
但它停在任何一个。
一些代码:
int main(int argc, char *argv[])
{
while(1) {
if(reconnect){
close(fd);
fd = createSocket(argv[1]);
reconnect=0;
}
reconnect = connectionTest(fd);
}
int connectionTest(int *fd) {
numbytes=send(fd, buf, 100,0);
if ((numbytes = recv(fd, reply, 100, 0)) == -1) {
/* HERE IT STOPS */
perror("recv");
printf("\n error while test recv 1");
reconnect = 1;
}
return reconnect;
}
int createSocket(char *server_addr){
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
int set = 1;
signal(SIGPIPE, SIG_IGN);
printf("connect to: %s", server_addr);
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(server_addr, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
else printf("socketd created! \n");
int set = 1;
setsockopt(sockfd, SOL_SOCKET, MSG_NOSIGNAL, (void *)&set, sizeof(int));
if (setsockopt( sockfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&set, sizeof(int)) < 0 )
perror("setsockopt failed \n");
struct timeval timeout;
timeout.tv_sec = 4;
timeout.tv_usec = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0 )
perror("setsockopt failed \n");
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0 )
perror("setsockopt failed \n");
printf("Try to connect \n");
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("client: connect");
}
else {
printf("i have connection");
break;
}
}
printf("next1");
if (p == NULL) {
fprintf(stderr, "client: failed to connect\n");
return 2;
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
s, sizeof s);
printf("client: connecting to %s\n", s);
freeaddrinfo(servinfo); // all done with this structure
return sockfd;
}