为什么我的socket recv()一直等待,接收不到东西
structsockaddr_inClientAddr;structsockaddr_inServerAddr;intSendLen=0,RecvLen=0,errorn...
struct sockaddr_in ClientAddr;
struct sockaddr_in ServerAddr;
int SendLen = 0, RecvLen = 0, errorno = 0, i = 0, Msglen = 0,client_socket=-1,sin_size=0,new_fd=0;
char ResponeMsg[1024*10+1]={0};
char RequestMsg[1024*4] = "3A 21 01"; struct timeval tv = {0};
fd_set fs = {0};
int ret = 0, error = 0, len = 0, is_connect = -1, Flag = 0;
bzero(&ClientAddr, sizeof(ClientAddr)); ClientAddr.sin_family = AF_INET;
ClientAddr.sin_addr.s_addr = INADDR_ANY;//inet_addr("192.168.1.14"); //get host address
ClientAddr.sin_port = htons(3333); //get free port from system
bzero(&(ClientAddr.sin_zero),8);
printf("1\n");
if (-1 == client_socket)
{
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0)
{
return -1;
}
Flag = fcntl(client_socket, F_GETFL, 0);
fcntl(client_socket, F_SETFL, Flag|O_NONBLOCK);
bzero(&ServerAddr, sizeof(ServerAddr));
ServerAddr.sin_family = AF_INET; if (0 == inet_aton("192.168.1.203", &ServerAddr.sin_addr))
{
close(client_socket);
client_socket = -1;
return -2;
}
ServerAddr.sin_port = htons(3028);
bzero(&(ServerAddr.sin_zero),8);
errorno = connect(client_socket, (struct sockaddr *)&ServerAddr, sizeof(ServerAddr)); if (errorno < 0)
{
printf("sys errno = %d\n", errno);
if (errno == EINPROGRESS)
{
printf("select\n");
tv.tv_sec = 3;
tv.tv_usec = 0;
FD_ZERO(&fs);
FD_SET(client_socket, &fs);
ret = select(client_socket+1, NULL, &fs, NULL, &tv);
if (ret == -1)
{
perror("select()");
}
else if (ret)
{
is_connect = 1;
} else
{
printf("select timeout!\n");
goto link_err;
}
}
else
{
printf("connect other error!\n");
link_err :
close(client_socket);
client_socket = -1;
return -3;
}
}
else
{
is_connect = 1;
}
if (is_connect == 1)
{
SendLen = strlen(RequestMsg);
memset(ResponeMsg, 0, 1024*10+1);
fcntl(client_socket, F_SETFL, Flag);
if (send(client_socket, RequestMsg, SendLen, 0) > 0)
{ printf("send ok!\n"); for (i = 0; i < 5; i++)
{
printf("2\n");
if((RecvLen = recv(client_socket, ResponeMsg, 1024*10+1, 0))==-1)
{
perror("recv error!!");
printf("Recvlen = %d\n", RecvLen);
break;
}
printf("3\n");
ResponeMsg[RecvLen]='\0';
if (RecvLen > 0)
{
break;
}
} if (RecvLen == 0)
{
close(client_socket);
client_socket = -1;
return -4;
}
}
else
{
perror("send error!!");
close(client_socket);
client_socket = -1;
is_connect = -1;
return -6;
}
} printf("ResponMsg=%s\n",ResponeMsg);
close(client_socket);
client_socket = -1;
is_connect = -1;
return 0;
}
}
服务器有返回数据 展开
struct sockaddr_in ServerAddr;
int SendLen = 0, RecvLen = 0, errorno = 0, i = 0, Msglen = 0,client_socket=-1,sin_size=0,new_fd=0;
char ResponeMsg[1024*10+1]={0};
char RequestMsg[1024*4] = "3A 21 01"; struct timeval tv = {0};
fd_set fs = {0};
int ret = 0, error = 0, len = 0, is_connect = -1, Flag = 0;
bzero(&ClientAddr, sizeof(ClientAddr)); ClientAddr.sin_family = AF_INET;
ClientAddr.sin_addr.s_addr = INADDR_ANY;//inet_addr("192.168.1.14"); //get host address
ClientAddr.sin_port = htons(3333); //get free port from system
bzero(&(ClientAddr.sin_zero),8);
printf("1\n");
if (-1 == client_socket)
{
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0)
{
return -1;
}
Flag = fcntl(client_socket, F_GETFL, 0);
fcntl(client_socket, F_SETFL, Flag|O_NONBLOCK);
bzero(&ServerAddr, sizeof(ServerAddr));
ServerAddr.sin_family = AF_INET; if (0 == inet_aton("192.168.1.203", &ServerAddr.sin_addr))
{
close(client_socket);
client_socket = -1;
return -2;
}
ServerAddr.sin_port = htons(3028);
bzero(&(ServerAddr.sin_zero),8);
errorno = connect(client_socket, (struct sockaddr *)&ServerAddr, sizeof(ServerAddr)); if (errorno < 0)
{
printf("sys errno = %d\n", errno);
if (errno == EINPROGRESS)
{
printf("select\n");
tv.tv_sec = 3;
tv.tv_usec = 0;
FD_ZERO(&fs);
FD_SET(client_socket, &fs);
ret = select(client_socket+1, NULL, &fs, NULL, &tv);
if (ret == -1)
{
perror("select()");
}
else if (ret)
{
is_connect = 1;
} else
{
printf("select timeout!\n");
goto link_err;
}
}
else
{
printf("connect other error!\n");
link_err :
close(client_socket);
client_socket = -1;
return -3;
}
}
else
{
is_connect = 1;
}
if (is_connect == 1)
{
SendLen = strlen(RequestMsg);
memset(ResponeMsg, 0, 1024*10+1);
fcntl(client_socket, F_SETFL, Flag);
if (send(client_socket, RequestMsg, SendLen, 0) > 0)
{ printf("send ok!\n"); for (i = 0; i < 5; i++)
{
printf("2\n");
if((RecvLen = recv(client_socket, ResponeMsg, 1024*10+1, 0))==-1)
{
perror("recv error!!");
printf("Recvlen = %d\n", RecvLen);
break;
}
printf("3\n");
ResponeMsg[RecvLen]='\0';
if (RecvLen > 0)
{
break;
}
} if (RecvLen == 0)
{
close(client_socket);
client_socket = -1;
return -4;
}
}
else
{
perror("send error!!");
close(client_socket);
client_socket = -1;
is_connect = -1;
return -6;
}
} printf("ResponMsg=%s\n",ResponeMsg);
close(client_socket);
client_socket = -1;
is_connect = -1;
return 0;
}
}
服务器有返回数据 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询