说明:本实例利用了libuv库中的pipe管道实现进程通信,类似与本地套接字。客户端连接服务器端建立连接后相互发送数据。
客户端使用了readline库实现控制台输入命令,客户端将命令字符串传给服务端。
服务端:
文件LocalServer.h
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define PIPENAME "pipe.sock"
extern "C"
{
#include "uv.h"
#include "uv-errno.h"
}
class LocalServer;
typedef struct{
uv_pipe_t server_client;
LocalServer *ts;
}UvPipe;
class LocalServer
{
public:
LocalServer();
~LocalServer();
int initLocalServer(uv_loop_t* loop);
static void remove_sock(int sig);
static void onPipeConnectionCb(uv_stream_t* server,int status);
void onPipeConnection(uv_stream_t* server,int status);
static void onPipeReadCb(uv_stream_t* client,ssize_t nread,const uv_buf_t* buf);
void onPipeRead(uv_stream_t* client,ssize_t nread,const uv_buf_t* buf);
static void allocPipeBufferCb(uv_handle_t* handle,size_t suggested_size,uv_buf_t* buf);
void allocPipeBuffer(uv_handle_t* handle,size_t suggested_size,uv_buf_t* buf);
private:
uv_loop_t* mLoop;
UvPipe pipeServer;
};
#include "LocalServer.h"
LocalServer::LocalServer()
{
}
LocalServer::~LocalServer()
{
}
int LocalServer::initLocalServer(uv_loop_t* loop)
{
if(loop==NULL)
{
cout<<"mllop is null "<0)
{
cout<<"data="<base<<" len"<len<base,"reload")==0)
{
cout<<"data="<base<base);
}
}
void LocalServer::allocPipeBuffer(uv_handle_t* handle,size_t suggested_size,uv_buf_t* buf)
{
buf->base=(char*)malloc(suggested_size);
buf->len=suggested_size;
}
void LocalServer::onPipeConnectionCb(uv_stream_t* server,int status)
{
UvPipe *pipe=(UvPipe*)server;
LocalServer *ts=pipe->ts;
ts->onPipeConnection(server,status);
}
void LocalServer::onPipeReadCb(uv_stream_t* client,ssize_t nread,const uv_buf_t* buf)
{
UvPipe *handle=(UvPipe*)client;
LocalServer *ts=handle->ts;
ts->onPipeRead(client,nread,buf);
}
void LocalServer::allocPipeBufferCb(uv_handle_t* handle,size_t suggested_size,uv_buf_t* buf)
{
UvPipe *pipe=(UvPipe*)handle;
LocalServer *ts=pipe->ts;
ts->allocPipeBuffer(handle,suggested_size,buf);
}
#include
#include
#include "LocalServer.h"
extern "C"
{
#include "uv.h"
}
using namespace std;
uv_loop_t mloop;
int main()
{
//mloop=(uv_loop_t*)malloc(sizeof(uv_loop_t));
uv_loop_init(&mloop);
LocalServer *server=new LocalServer();
server->initLocalServer(&mloop);
uv_run(&mloop,UV_RUN_DEFAULT);
return 0;
}
客户端:
文件:LocalClient.h
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define PIPENAME "pipe.sock"
extern "C"
{
#include "uv.h"
#include "uv-errno.h"
}
class LocalClient;
typedef struct{
uv_pipe_t server_client;
LocalClient *ts;
}UvPipe;
class LocalClient
{
public:
LocalClient();
~LocalClient();
void setStr(const char *str);
int initPipeClient();
static void on_connect_cb(uv_connect_t* req,int status);
void on_connect(uv_connect_t* req,int status);
static void write_server_cb(uv_write_t* req,int status);
void write_server(uv_write_t* req,int status);
private:
uv_loop_t *loop;
UvPipe pipeClient;
uv_connect_t *pipeConnect;
char cmd[20];
};
#include "LocalClient.h"
LocalClient::LocalClient()
{
}
LocalClient::~LocalClient()
{
}
void LocalClient::setStr(const char *str)
{
memset(cmd,0,20);
strcpy(cmd,str);
}
int LocalClient::initPipeClient()
{
loop=uv_default_loop();
pipeClient.ts=this;
uv_pipe_init(loop,(uv_pipe_t*)&pipeClient,0);
pipeConnect=(uv_connect_t*)malloc(sizeof(uv_connect_t));
uv_pipe_connect(pipeConnect,(uv_pipe_t*)&pipeClient,PIPENAME,on_connect_cb);
uv_run(loop,UV_RUN_DEFAULT);
return 0;
}
void LocalClient::on_connect_cb(uv_connect_t* req,int status)
{
UvPipe* pipe=(UvPipe*)req->handle;
LocalClient* ts=pipe->ts;
ts->on_connect(req,status);
}
void LocalClient::on_connect(uv_connect_t* req,int status)
{
if(status<0)
{
cout<<"New conect error..."<handle,&buf,1,write_server_cb);
if(ret<0)
{
cout<<"Write error..."<handle,&buf,1,write_server_cb);
if(ret<0)
{
cout<<"Write error..."<ts;
ts->write_server(req,status);
}
void LocalClient::write_server(uv_write_t* req,int status)
{
if(status<0)
{
cout<<"Write error..."<
#include
#include
#include
#include
#include "LocalClient.h"
using namespace std;
int main()
{
LocalClient *clit=new LocalClient();
clit->setStr("-r");
clit->initPipeClient();
return 0;
}