转:sock_ev——linux平台socket事件框架(基于数据报的测试程序) .
上一篇已经做过注释,这一篇直接上代码
[cpp] view
plaincopyprint?
/***************************************************************************************
****************************************************************************************
* FILE : server_test.cc
*
Description :
*
* Copyright (c) 2012 by Liu Yanyun(E-mail:[email protected]). All Rights
Reserved.
* Without
permission, shall not be used for any commercial purpose
*
*
History:
* Version
Name
Date
Description
0.1 Liu
Yanyun 2012/12/03
Initial Version
****************************************************************************************
****************************************************************************************/
#include "socket.h"
#include
"log_trace.h"
#include "socket_addr.h"
#include
"socket_base.h"
#include "event_loop.h"
#include
<iostream>
using namespace std;
int main()
{
Socket *ser =
Socket::create();
bool ret =
ser->open("dgram://unix.domain.ipc.1");
char
buf[100] = {0};
char addr[128] = {0};
ser->recv(buf, sizeof(buf), addr);
printf("from:%s;buf:%s\n", addr, buf);
ser->send(buf, strlen(buf), addr);
Socket::destroy(ser);
return 0;
}
/***************************************************************************************
****************************************************************************************
*
FILE : server_test.cc
* Description :
*
* Copyright (c) 2012 by Liu
Yanyun(E-mail:[email protected]). All Rights
Reserved.
*
Without permission, shall not be used for any commercial purpose
*
*
History:
* Version Name
Date Description
0.1 Liu
Yanyun 2012/12/03 Initial Version
****************************************************************************************
****************************************************************************************/
#include "socket.h"
#include "log_trace.h"
#include
"socket_addr.h"
#include "socket_base.h"
#include
"event_loop.h"
#include <iostream>
using namespace std;
int main()
{
Socket *ser = Socket::create();
bool ret
= ser->open("dgram://unix.domain.ipc.1");
char buf[100] = {0};
char addr[128] = {0};
ser->recv(buf, sizeof(buf), addr);
printf("from:%s;buf:%s\n", addr, buf);
ser->send(buf, strlen(buf), addr);
Socket::destroy(ser);
return 0;
}
[cpp] view
plaincopyprint?
/***************************************************************************************
****************************************************************************************
* FILE : client_test.cc
*
Description :
*
* Copyright (c) 2012 by Liu Yanyun(E-mail:[email protected]). All Rights
Reserved.
* Without
permission, shall not be used for any commercial purpose
*
*
History:
* Version
Name
Date
Description
0.1 Liu
Yanyun 2012/12/03
Initial Version
****************************************************************************************
****************************************************************************************/
#include "socket.h"
#include
"log_trace.h"
#include "socket_addr.h"
#include
"socket_base.h"
#include <iostream>
using namespace std;
int main()
{
Socket *clt = Socket::create();
bool ret =
clt->open("dgram://unix.domain.ipc.2");
ret = ret;
char buf[100] = {"hello"};
char addr[128]
= {0};
clt->send(buf, strlen(buf),
"dgram://unix.domain.ipc.1");
clt->recv(buf,
sizeof(buf), addr);
printf("from:%s;buf:%s\n", addr,
buf);
sleep(2);
Socket::destroy(clt);
return
0;
}
/***************************************************************************************
****************************************************************************************
*
FILE : client_test.cc
* Description :
*
* Copyright (c) 2012 by Liu
Yanyun(E-mail:[email protected]). All Rights
Reserved.
*
Without permission, shall not be used for any commercial purpose
*
*
History:
* Version Name
Date Description
0.1 Liu
Yanyun 2012/12/03 Initial Version
****************************************************************************************
****************************************************************************************/
#include "socket.h"
#include "log_trace.h"
#include
"socket_addr.h"
#include "socket_base.h"
#include <iostream>
using namespace std;
int main()
{
Socket *clt = Socket::create();
bool ret
= clt->open("dgram://unix.domain.ipc.2");
ret = ret;
char buf[100] = {"hello"};
char addr[128] = {0};
clt->send(buf, strlen(buf), "dgram://unix.domain.ipc.1");
clt->recv(buf, sizeof(buf), addr);
printf("from:%s;buf:%s\n", addr, buf);
sleep(2);
Socket::destroy(clt);
return
0;
}起两个shell结果为:
./server_test
from:dgram://unix.domain.ipc.2;buf:hello
./client_test
from:dgram://unix.domain.ipc.1;buf:hello
上面是使用unix域套接字进程测试;修改地址为ip:port形式可以转为inet域。
对于数据包式的通信,只需要各自打开自身的地址,然后就可以根据地址收发数据了;而在上一篇中对于字节流方式服务器端要先打开进行监听,然后客户端去连接,服务器端接受连接以后才可以进行通信。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。