问题描述
- libuv客户端开发,通过TCP/IP连接服务端出错!
-
使用libuv进行客户端开发,在Linux下测试没有问题,当移植到win7下面就报错了,错误信息如下:
具体代码:
....... void uv_write_cb(uv_write_t *req,int status) { if(status == -1) { cout<<"error on_write_end"<<endl; return; } //cout<<"write_end!!!"<<endl; req->handle->data = req->data; uv_read_start(req->handle,alloc_buffer,uv_read_cb); //req->handle->loop->stop_flag = 1; } ...... void connect_server(uv_connect_t *req,int status) { ...... char buffer[100]; uv_buf_t buf = uv_buf_init(buffer,sizeof(buffer)); buf.len = strlen(fileName)+1; buf.base = fileName; uv_stream_t* tcp = req->handle; uv_write_t write_req; write_req.data = req->data; int buf_count = 1; uv_write(&write_req,tcp,&buf,buf_count,client::uv_write_cb); ...... } int _tmain(int argc, _TCHAR* argv[]) { ...... _loop = uv_default_loop(); uv_tcp_t socket; uv_tcp_init(_loop,&socket); uv_connect_t connect; connect.data = (void*)this; //struct sockaddr dest; struct sockaddr_in dest; uv_ip4_addr(_ipaddr.c_str(),_port,&dest); //struct sockaddr addrInfo; uv_tcp_connect(&connect,&socket,(const struct sockaddr*)&dest,connect_server); uv_run(_loop,UV_RUN_DEFAULT); return 0; ...... }
解决方案
已经解决!
问题分析:
局部变量引起loop捕获不到事件。
解决方法:
把和uv相关的修改为全局的变量就没问题了。
时间: 2025-01-19 13:27:10