P2P穿透(ENet丢包重传)

资源链接:http://download.csdn.net/download/yuanchunsi/10159049


P2P穿透成功后,获得对端IP和端口通过ENet进行连接传输数据,解决了UDP丢包视频卡顿的问题!



服务端:

/*
 * ===========================================================================
 *
 *       Filename:  enetc.c
 *    Description:  
 *        Version:  1.0
 *        Created:  2017年12月11日 14时10分03秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:   (ycs), 
 *        Company:  
 *
 * ===========================================================================
 */

#include<string.h>
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include"enet/enet.h"
//全局变量
pthread_t pthread_S;
pthread_t pthread_R;
ENetPeer *peer = NULL;
ENetHost *host = NULL;
ENetEvent event; 

//模拟媒体数据结构
typedef struct code_{
  char data[128];
  int size ;
}tmp_t;

//发送数据线程,模拟项目框架
void *run_s(void*data){

  pthread_detach(pthread_self()); 
  static int count = 0; 
  while(1) {
    sleep(1);
    if(NULL != peer) {
    ENetPacket *packet = enet_packet_create(NULL,78,ENET_PACKET_FLAG_RELIABLE);//ENET_PACKET_FLAG_RELIABLE:这个flag表示支持丢包重发
    char tmp[78];
    snprintf(tmp,78,"%s count: %d","I am server",count++);//发送带有seq的数据模拟RTP的序列号,确保ENet发送无异常
    //strcpy((char*)packet->data,tmp);
	memcpy((char*)packet->data,tmp,78);//此处很重要,媒体数据不能用strcpy!!!

    enet_peer_send(peer,2,packet);
    enet_host_flush(host);
    }
  }


}
//接收数据线程,模拟项目框架
void *run_R(void*data){

  pthread_detach(pthread_self()); 
  static int count = 0; 
  while (enet_host_service (host, &event, 5000)>=0)  
  {
    printf("event type == %d\n",event.type);

    if(event.type == ENET_EVENT_TYPE_CONNECT){
      peer = event.peer;
      char ip[256];
      ENetAddress remote = event.peer->address;
      enet_address_get_host_ip(&remote
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值