This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

请教下tcp client 的问题



我的目的是连接tcp服务器 ,tcp要求是非阻塞的,然后循环判断有没有服务器的数据,如果有的话就处理。现在的现象是连接到服务器之后能正常通信,但是过了几十秒之后,服务器发送消息就接收不到了。还有就是反复接收服务器消息大概10几次之后 整个程序就卡死在sl_Recv()这里,下面是主要的代码,大家帮我看看谢谢了。

int BsdTcpClient(unsigned short usPort)
{

sAddr.sin_family = SL_AF_INET;
sAddr.sin_port = sl_Htons((unsigned short)usPort);
sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)severip);

iAddrSize = sizeof(SlSockAddrIn_t);
SlSockNonblocking_t enableOption;
enableOption.NonblockingEnabled = 1;
// creating a TCP socket
iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
if( iSockID < 0 )
{
//ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
printf("SOCKET_CREATE_ERROR\n");
}

printf("iSockID : %d\n" ,iSockID);
// connecting to TCP server
iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
if( iStatus < 0 )
{
// error
sl_Close(iSockID);
//ASSERT_ON_ERROR(CONNECT_ERROR);
printf("CONNECT_ERROR\n");
}

iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING,
(_u8 *)&enableOption, sizeof(enableOption));
if( iSockID < 0 )
{
printf("udp noblock error\n");
}
MAP_UtilsDelay(80000000);
printf("connect tcp sever success\n");
return SUCCESS;
}

int main()

{

iStatus = sl_Recv(iNewSockID, tcpbuf, sizeof(tcpbuf), 0);
if(iStatus < 0)
{
//printf("2");
}
else if(iStatus == 0)
{

}
else
{

printf("recv sever message\n");
for(i=0 ;i<iStatus ;i++)
 {

printf("0x%02x ",tcpbuf[i]);
 }

}

}