资源链接: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