|
最近在MTK 6225平台上用SOCKET方式和互联网通讯,当执行连接函数时,始终返回-2(SOC_WOULDBLOCK)错误。请路过的兄弟指点指点。是否在创建SOCKET前还要进行一些别的初始化才能正常和互联网通讯
{
kal_int8 handle, result, i = 0;
kal_uint8 option;
sockaddr_struct addr;
S32 len;
U8 buf[200], buf1[200];
while(i++ < 10)
{
handle = soc_create(PF_INET, SOCK_STREAM, 0, MOD_MMI, 10);//z 10);
if (handle < 0)
{
soc_close((kal_int8) handle);
sprintf((S8*)buf1, (const char*)"create = %d, error.", handle);
asciitounicode((S8*)buf1, (S8*)buf);
DisplayPopup((U8*)buf, IMG_GLOBAL_ERROR, 1, 1000, 20);
// return;
}
// sprintf((S8*)buf1, (const char*)"socket id = %d", handle);
// asciitounicode((S8*)buf1, (S8*)buf);
// DisplayPopup((U8*)buf, IMG_GLOBAL_OK, 1, 1000, 20);
// result = soc_getsockopt(handle, SOC_NBIO, &option, sizeof(kal_uint8));
// option = result;
option = KAL_TRUE;
result = soc_setsockopt(handle, SOC_NBIO, &option, sizeof(kal_uint8));
if (result != SOC_SUCCESS)
{
soc_close((kal_int8) handle);
sprintf((S8*)buf1, (const char*)"SOC_NBIO = %d, error.", result);
asciitounicode((S8*)buf1, (S8*)buf);
DisplayPopup((U8*)buf, IMG_GLOBAL_ERROR, 1, 1000, 20);
// return;
}
option = SOC_READ | SOC_WRITE | SOC_ACCEPT | SOC_CONNECT | SOC_CLOSE;
result = soc_setsockopt(handle, SOC_ASYNC, &option, sizeof(kal_uint8));
if (result != SOC_SUCCESS)
{
soc_close((kal_int8) handle);
sprintf((S8*)buf1, (const char*)"SOC_ASYNC = %d, error.", result);
asciitounicode((S8*)buf1, (S8*)buf);
DisplayPopup((U8*)buf, IMG_GLOBAL_ERROR, 1, 1000, 20);
// return;
}
addr.addr[0] = 221;//z 110;
addr.addr[1] = 212;//z 125;
addr.addr[2] = 125;//z 212;
addr.addr[3] = 110;//z 221;
addr.addr[4] = 0;
addr.addr_len = 4;
addr.port = 2020;
result = soc_connect((kal_int8) handle, (sockaddr_struct*)&addr);
if (result == SOC_SUCCESS)
{
len = Int16ToChat("FFFF56333010190001020657045522785126293702000034650951203480", (char *)buf);
result = soc_send(handle, buf, len, 0);
if(result == len)
{
asciitounicode("Success", (S8*)buf);
DisplayPopup((U8*)buf, IMG_GLOBAL_OK, 1, 1000, 20);
return;
}
else
{
soc_close((kal_int8) handle);
sprintf((S8*)buf1, (const char*)"send = %d, error.", result);
asciitounicode((S8*)buf1, (S8*)buf);
DisplayPopup((U8*)buf, IMG_GLOBAL_ERROR, 1, 1000, 20);
}
// return;
}
else
{
/* if(result == SOC_WOULDBLOCK)
{
len = Int16ToChat("FFFF56333010190001020657045522785126293702000034650951203480", (char *)buf);
result = soc_send(handle, buf, len, 0);
if(result == len)
{
asciitounicode("Success", (S8*)buf);
DisplayPopup((U8*)buf, IMG_GLOBAL_OK, 1, 1000, 20);
}
else
{
sprintf((S8*)buf1, (const char*)"send = %d, error.", result);
asciitounicode((S8*)buf1, (S8*)buf);
DisplayPopup((U8*)buf, IMG_GLOBAL_ERROR, 1, 1000, 20);
}
}
else*/
{
soc_close((kal_int8) handle);
sprintf((S8*)buf1, (const char*)"connect = %d, error.", result);
asciitounicode((S8*)buf1, (S8*)buf);
DisplayPopup((U8*)buf, IMG_GLOBAL_ERROR, 1, 1000, 20);
}
// return;
}
for(len = 0; len <2000000; len++);
}
} |
|