W5500堵塞接收,关闭socket错误

发布于 2021-01-14 19:53:24

int main(void)
{

int ret = 0;
int sock, bytes_received;
struct server_addr;

/* 通过网卡名称获取网卡对象,名称可以通过 ifconfig 命令查看 */
struct netdev *netdev = netdev_get_by_name("W5500");
if (netdev == RT_NULL)
{
        rt_kprintf("not find network interface device name().\n");
        return 0;
}    
/* 设置默认网卡对象 */

netdev_set_default(netdev);

if(sock != RT_NULL)
{

    closesocket(sock);
    sock = RT_NULL;

}

        
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
        /* 创建socket失败 */
        rt_kprintf("Socket error");
        sock = RT_NULL;
}

/* 初始化预连接的服务端地址 */
struct hostent *host = gethostbyname("192.168.1.203");
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(9600);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
/* 连接到服务端 */
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
     /* 连接失败 */
    rt_kprintf("Connect fail!");
    closesocket(sock);
    /*释放接收缓冲 */
    return 0;
}

//开启线程
dt_test_isWhile = 1;
dt_test_runing = 1;
rt_thread_t tid;
tid = rt_thread_create("os_test", dt_WIZ_test_thread_entry, &sock,
                                        1024, (RT_THREAD_PRIORITY_MAX / 3), 20);
RT_ASSERT(tid != RT_NULL);
rt_thread_startup(tid);

rt_thread_mdelay(10000);

closesocket(sock);

}

void dt_WIZ_test_thread_entry(void *parameter)
{

int sock = *(int*)(parameter);
char *dev_data_buff = rt_calloc(1,BUFSZ);
int dev_bytes_received;
int ret,i=5;
if (dev_data_buff == RT_NULL)
{
        rt_kprintf("No memory");
        return;
}
while(dt_test_isWhile)
{    
            //接收设备数据
            dev_bytes_received = recv(sock, dev_data_buff, BUFSZ - 1, 0);
            rt_thread_mdelay(10);  //等待dt_pass_through_stop关闭sock,防止多次关闭同一个sock
            //cmd_printf(1,dev_data_buff,dev_bytes_received);   //打印发送数据
            //发送设备数据到网络
            if(dev_bytes_received>0)
            {
                ret = send(sock, dev_data_buff, dev_bytes_received, 0);
                
                if (ret <= 0)
                {
                        /* 接收失败,关闭这个连接 */
                    if(sock!=RT_NULL)
                        closesocket(sock);
                    rt_kprintf("thr_web_sock send error %d,close the socket.",ret);
                }
            }
    }    
        dt_test_isWhile = 0;
}
dt_test_runing = 0;
rt_kprintf("quit.");

}

问题:在main线程创建线程,w5500开启堵塞接收recv(sock, dev_data_buff, BUFSZ - 1, 0);,main函数中延时closesocket(sock);此事会出错wiz_recvfrom函数的wiz_do_event_changes中rt_wqueue_wakeup死掉。
目的:在一个堵塞的socket中接收线程中要怎么跳出堵塞函数除了关闭socket

查看更多

关注者
0
被浏览
206
hijxyz
hijxyz 2021-01-15

不支持多线程操作的socket的

2 个回答
kid
kid 2021-01-19

我也遇到过这个问题, 关不掉socket, 又无法通信, 改源码强行关闭, 时间久了也会出问题, 后来换了 lan8720 , 数个bug 一同消失不见了, 缺点也很明显, 片上使用的内存增加了不少

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览