2 davil dev davil_dev 于 2013.11.21 17:41 提问

libuv客户端开发,通过TCP/IP连接服务端出错!

使用libuv进行客户端开发,在Linux下测试没有问题,当移植到win7下面就报错了,错误信息如下:
Valid XHTML

具体代码:

.......
   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;
       ......
    }

1个回答

davil_dev
davil_dev   2013.11.26 11:20
已采纳

已经解决!
问题分析:
局部变量引起loop捕获不到事件。
解决方法:
把和uv相关的修改为全局的变量就没问题了。

Csdn user default icon
上传中...
上传图片
插入图片