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.

TMS320DM648-NDK开发传输数据-send函数异常退出问题

Other Parts Discussed in Thread: TMS320DM648

硬件:TMS320DM648

软件:CCS5.4 NDK2.0.0

问题描述:在使用TCP/IP协议栈的时候,创建socket后,循环使用send函数持续发送数据。但是发送过一段时间后send函数返回错误并退出,错误类型为EWOULDBLOCK,请问这种错误是因为什么导致的(内存不够?),以及怎么解决?

1. Socket配置代码:

// Create the main TCP socket
stcp = socket(AF_INET, SOCK_STREAM, 0);
if( stcp == INVALID_SOCKET )
{
	printf("Fail socket, %d\n", fdError());
	goto leave;
}

// Set Pmort = 9528, IP address = IPAddrSend
IPAddr = inet_addr(RemoteIPAddr);
bzero( &sin1, sizeof(struct sockaddr_in) );
sin1.sin_family = AF_INET;
sin1.sin_len    = sizeof( sin1 );
sin1.sin_addr.s_addr = IPAddr;
sin1.sin_port   = htons(9528);

to.tv_sec  = 5;
to.tv_usec = 0;
setsockopt( stcp, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
setsockopt( stcp, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );

uart_write("connect to the server:");uart_write(RemoteIPAddr);
while(connect( stcp, (PSA) &sin1, sizeof(sin1) ) < 0)
{
	printf("Fail connect, %d\n", fdError());
}
uart_write("Success!\n");

2. Send任务

while(1)
{
	if( ((bytes = send( stcp, pBuf , (int)FRAME_SIZE, 0 )) < 0) )
	{
		printf("send failed (%d)\n",fdError());
		goto leave;
	}
	if(FRAME_SIZE != bytes)
	{
		printf("send bytes (%d)\n",bytes);
	}
	totalBytes += bytes;

	// Get TSK_idle time to compute CPU load half-way through
	// number of packets transfers
	if ( loop == count)
	{
		THRLOAD_getTskTime(&TSK_idle, &tskTime, &totalTime);
	}

}

3. 发送任务出错执行的代码段: (send(...)函数内 --> SockSend(....)函数内)

// Check blocking condition
if( SizeCopy < size && !AddCopy )
{
    // We need to block (protocol did not handle the data )

    // Can't block a non-blocking socket
    // If we timeout, we have an error and break the loop
    if( (ps->StateFlags & SS_NBIO) || (flags & MSG_DONTWAIT) ||
        !FdWaitEvent( ps, FD_EVENT_WRITE, ps->TxTimeout ) )
    {
        error = EWOULDBLOCK;
        break;
    }
}

就是在int SockSend( HANDLE   h, char     *pBuf, INT32    size, int      flags, INT32    *pRetSize )函数中(在sock.c文件),这个函数我的理解是:先检查发送缓冲区是否有空间然后进行数据copy。如果没有空间就进入到上述3中的代码段阻塞等待……如果超时就报错退出,如果产生了可以写的事件则继续copy数据去发送。

注:在发送时出现上述错误退出后,使用的NDK库并没有关闭socket,并且客户端再也无法通过套接字连接DSP了