为什么我的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;
}
}
服务器有返回数据
展开
 我来答
qiaodamo
2012-05-08 · TA获得超过912个赞
知道小有建树答主
回答量:337
采纳率:100%
帮助的人:272万
展开全部
recv本身就是阻塞函数,必须在收到消息之后才能返回,你没收到消息,说明对方没有给你发过来,或者网络故障等等都可能。

不知道你怎么判断服务器有返回数据的,我觉得你需要判断服务器发送是否成功
523914437
2012-05-14 · 超过20用户采纳过TA的回答
知道答主
回答量:98
采纳率:0%
帮助的人:72.2万
展开全部
把本机IP地址改为127.0.0.1
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式