上一篇已经做过注释,这一篇直接上代码

/***************************************************************************************
****************************************************************************************
* FILE      : server_test.cc
* Description   : 
*             
* Copyright (c) 2012 by Liu Yanyun(E-mail:liuyun827@foxmail.com). 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:liuyun827@foxmail.com). 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      : client_test.cc
* Description   : 
*             
* Copyright (c) 2012 by Liu Yanyun(E-mail:liuyun827@foxmail.com). 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域。

对于数据包式的通信,只需要各自打开自身的地址,然后就可以根据地址收发数据了;而在上一篇中对于字节流方式服务器端要先打开进行监听,然后客户端去连接,服务器端接受连接以后才可以进行通信。

转:sock_ev——linux平台socket事件框架(基于数据报的测试程序) .的更多相关文章

  1. 转:sock_ev——linux平台socket事件框架(基于字节流的测试程序) .

    原文:http://blog.csdn.net/gdutliuyun827/article/details/8257186 由于工作与学习的需要,写了一个socket的事件处理框架,在公司写的已经使用 ...

  2. 转:sock_ev——linux平台socket事件框架(socket API的封装) .

    把linux平台提供的有关socket操作的API进行封装是有必要的:基于stream操作的流程与基于dgram操作的流程略有不同,分别放在两个类中,但两者又有很多相似的操作,因此写一个基类,让其继承 ...

  3. 转:sock_ev——linux平台socket事件框架(event loop) .

    上一篇我们封装了三种事件监听方式,如果分别提供给客户端使用,有点不方便,也不利于统一管理:我们再封装一层EventLoop. /************************************ ...

  4. 转:sock_ev——linux平台socket事件框架(event dispatcher) .

    最近比较忙,好久没更新了:今天我们看一下事件的监听方式,在linux下面事件的监听方式有三种select.poll.epoll,性能上面epoll最高,如果仅是最多监听十多个描述符,用啥无所谓,如果是 ...

  5. 转:sock_ev——linux平台socket事件框架(socket代理类) .

    前面分析了对socket基本操作的封装,并按照数据的传送方式写了两个类,本篇将写一个代理类提供给库的使用者使用的类. /**************************************** ...

  6. 转:sock_ev——linux平台socket事件框架(uri地址的解析) .

    在第一篇中,已经说明,传递的socket地址采取以下形式: [cpp] view plaincopyprint?stream://192.168.2.10:8080   dgram://192.168 ...

  7. 转:sock_ev——linux平台socket事件框架(logTrace) .

    写代码要有调试log,采用syslog的输出:一般会输出到"/var/log/messages" /**************************************** ...

  8. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-介绍

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

  9. 基于.NET平台常用的框架整理<转载>

    转载来自:http://www.cnblogs.com/hgmyz/p/5313983.html 基于.NET平台常用的框架整理   自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大 ...

随机推荐

  1. ArrayList源码阅读----JDK1.8

    //定义一个默认的长度10 private static final int DEFAULT_CAPACITY = 10; //定义空的数组 private static final Object[] ...

  2. ACM -- 算法小结(九)DP之Humble numbers

         DP -- Humble numbers  //一开始理解错题意了,题意是是说一些只有唯一一个质因数(质因数只包括2,3,5,7)组成的数组,请找出第n个数是多少 //无疑,先打表,否则果断 ...

  3. php 对二维数组的某个字段公用排序的方法

    function array_sort($arr ,$keys,$order=0){ if(!is_array($arr)){ return false; } $keysvalue=array(); ...

  4. YII 关联查询

    通过外键自己关联自己

  5. 普通主板设置BIOS实现电脑插电自动启动

    说明: 1.为什么要实现这种功能,很多时候在民间都基本用普通PC来做小型服务器,公司的私服等等,而这些普通PC在民用电环境中经常会停电,一停就会导致服务器不能自动来电重启,所以这个功能来点开机是必须的 ...

  6. Ubuntu 16.09开启iptables的日志实现调试

    1.先配置日志文件输出 参考:http://www.cnblogs.com/EasonJim/p/8413535.html 2.配置日志打点 参考:http://www.cnblogs.com/Eas ...

  7. gitignore : VisualStudio.gitignore

    https://github.com/github/gitignore/blob/master/VisualStudio.gitignore ## Ignore Visual Studio tempo ...

  8. Android SDK Manager 代理服务器设置

    http://blog.csdn.net/star_splendid/article/details/6939063 自己机子更新的话,速度1KB/s 实在是等不及了~找方法吧 http://www. ...

  9. artDialog双击会关闭对话框的修改

    artDialog,一个jquery的对话框插件,很好用的说 但是在使用时发现鼠标双击时会自半对话框,查看源码发现有个监听鼠标双击的事件如下图: 简单的方法就是把该行去掉,为了扩展可改成如下图: 这样 ...

  10. visual studio 2008试用版的评估期(万能破解)

    教程 http://jingyan.baidu.com/article/a3a3f811ee87268da2eb8ae7.html 参考: http://blog.chinaunix.net/uid- ...