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

/***************************************************************************************
****************************************************************************************
* 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. String是最基本的数据类型吗?

    不是, 基本数据类型包括:byte,short,int,long,float,double,boolean,char. 而String是类代表字符串,属于引用类型,所谓引用类型包括:类,接口,数组.. ...

  2. Boost汉字匹配 -- 宽字符

      原文链接:http://blog.csdn.net/sptoor/article/details/4930069 思路:汉字匹配,把字符都转换成宽字符,然后再匹配. 需要用到以下和宽字符有关的类: ...

  3. ReactNative-闪退日志集成

    根据现实情况,先虚拟个场景 客户:喂,小王,上周发布的新版本,用着用着闪退了呢,是不是有什么问题? 小王:奥?主任,能说一下进行了那些操作吗? 客户:具体的我也不是很清楚,下面具体使用的人反应上来的, ...

  4. 应该用H5编写APP还是用原生的呢?

    现观目前市场上的APP横行,不同行业.不同类目的APP在国内各大应用市场挤爆的满满了,那么作为一个程序员或者一个企业如何能从容的把握住制作一款实用又符合用户体验的APP呢? 自从接触APP也有四年多了 ...

  5. Bipolar transistor boosts switcher's current by 12 times

    The circuit in Figure 1 uses a minimal number of external parts to raise the maximum output current ...

  6. 2014 百度之星 题解 1004 Labyrinth

    Problem Description 度度熊是一仅仅喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上角的第一个格子才算走出迷宫,每一次仅仅能 ...

  7. 第一章 Actionscript学习基本知识笔记及flashdevelop软件的安装问题

    OOP:封装.继承.多态. Pubilc :完全公开. Internal:包内类成员可以互相访问. Private:仅当前类可以访问. Protected:当前类和当前类的子类可以访问. 被关键词fi ...

  8. ORACLE 查看CPU使用率最高的语句及一些性能查询语句

    select * from (select sql_text,sql_id,cpu_time from v$sql order by cpu_time desc) where rownum<=1 ...

  9. [Android Studio] Android Studio使用教程(二)

    以下是本次Google I/O大会发布的IDE Android Studio使用教程第二篇: 在Android Studio使用教程(一)中简要介绍了Android Studio的基本使用,包括安装. ...

  10. 将matlab的figure保存为pdf,避免图片太大缺失

    有时画的matlab图太大,或者有太多的子图,导致图太宽,如果直接保存成pdf的话,会导致左右边丢失,显示不下.一个有效又简单的办法是:   1.在matlab figure里面,Edit -> ...