当前位置:首页 » 操作系统 » cppcheck源码

cppcheck源码

发布时间: 2022-05-14 01:59:24

1. 俄罗斯方块的源程序

#include<stdio.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4b
#define RIGHT 0x4d
#define ESC 0x01
#define P 0x19
#define ENTER 0x1c
#define PAGEUP 0x49
#define PAGEDOWN 0x51
#define add_D 0x0d
#define add_d 0x4e
#define sub_D 0x0c
#define sub_d 0x4a
#define RST 0x13
#define PAUSE 0x19

long score;
int brick_width=20;
char str[9]={'0','0','0','0','0','0','0','0','\0'}; /*积分数组!*/
int m_speed=0;
int difficulty=0;

/************************************************/
/*以下定义的是砖块的形状*/
int brick [6][4][4][4]=
{
{
{{0,1,0,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}},
{{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
{{0,1,0,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}},
{{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}
},
{
{{0,1,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}},
{{0,1,0,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}},
{{0,0,0,0},{1,1,1,0},{0,1,0,0},{0,0,0,0}},
{{0,1,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}}
},
{
{{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}},
{{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}},
{{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}},
{{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}}
},
{
{{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}},
{{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}},
{{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}},
{{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}}
},
{
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}
},
{
{{1,1,1,0},{1,0,0,0},{0,0,0,0},{0,0,0,0}},
{{1,1,0,0},{0,1,0,0},{0,1,0,0},{0,0,0,0}},
{{0,0,1,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}},
{{1,0,0,0},{1,0,0,0},{1,1,0,0},{0,0,0,0}}
}
};

/*以下定义的是游戏窗口的矩阵,在游戏进行时,通过检查此矩阵来获知砖块的堆积情况*/
int board[22][12]={
{2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,2,2,2,2,2,2,2,2,2,2,2}
};
/*以下是程序中用到汉字的点阵——————————————————*/
char e_16[]={
/* 以下是 '俄' 的 16点阵宋体 字模,32 byte */
0x10,0x20,0x19,0xA8,0x16,0x24,0x22,0x24,
0x3F,0xFE,0x62,0x20,0xA2,0x24,0x22,0xA6,
0x23,0x2C,0x26,0x28,0x2A,0x10,0x22,0x30,
0x22,0x4A,0x2E,0x8A,0x24,0x04,0x00,0x00,
};
char luo_16[]={
/* 以下是 '罗' 的 16点阵宋体 字模,32 byte */
0x00,0x00,0x3F,0xFC,0x22,0x44,0x22,0x44,
0x22,0x44,0x3F,0xFC,0x22,0x00,0x03,0xFC,
0x06,0x08,0x0C,0x08,0x12,0x10,0x61,0x20,
0x00,0xC0,0x03,0x00,0x0C,0x00,0xF0,0x00,
};
char si_16[]={
/* 以下是 '斯' 的 16点阵宋体 字模,32 byte */
0x22,0x0C,0x22,0x70,0xFF,0x40,0x22,0x40,
0x3E,0x40,0x22,0x7E,0x22,0x48,0x3E,0x48,
0x22,0x48,0x22,0x48,0xFF,0x48,0x00,0x48,
0x24,0x88,0x62,0x88,0x83,0x08,0x00,0x08,
};
char fang_16[]={
/* 以下是 '方' 的 16点阵宋体 字模,32 byte */
0x02,0x00,0x01,0x00,0x00,0x80,0xFF,0xFE,
0x02,0x00,0x02,0x00,0x03,0xF0,0x02,0x10,
0x04,0x10,0x04,0x10,0x08,0x10,0x08,0x10,
0x10,0x10,0x20,0x90,0xC0,0x60,0x00,0x00,
};
char kuai_16[]={
/* 以下是 '块' 的 16点阵宋体 字模,32 byte */
0x20,0x80,0x20,0x80,0x20,0x80,0x23,0xF8,
0xF8,0x88,0x20,0x88,0x20,0x88,0x20,0x88,
0x2F,0xFE,0x20,0x80,0x39,0x40,0xE1,0x20,
0x42,0x18,0x02,0x0E,0x04,0x04,0x08,0x00,
};
char kai_16[]={
/* 以下是 '开' 的 16点阵宋体 字模,32 byte */
0x00,0x00,0x7F,0xFE,0x04,0x20,0x04,0x20,
0x04,0x20,0x04,0x20,0xFF,0xFE,0x04,0x20,
0x04,0x20,0x04,0x20,0x08,0x20,0x08,0x20,
0x10,0x20,0x20,0x20,0x40,0x20,0x00,0x00,
};
char shi_16[]={
/* 以下是 '始' 的 16点阵宋体 字模,32 byte */
0x10,0x20,0x18,0x30,0x10,0x20,0x10,0x48,
0xFC,0x84,0x25,0xFE,0x24,0x84,0x44,0x00,
0x44,0xFC,0x28,0x84,0x18,0x84,0x14,0x84,
0x22,0x84,0x42,0xFC,0x80,0x84,0x00,0x00,
};
char tui_16[]={
/* 以下是 '退' 的 16点阵宋体 字模,32 byte */
0x40,0x00,0x27,0xF0,0x24,0x10,0x07,0xF0,
0x04,0x10,0x04,0x10,0xE7,0xF0,0x24,0x88,
0x24,0x50,0x24,0x20,0x25,0x10,0x26,0x18,
0x24,0x08,0x50,0x00,0x8F,0xFE,0x00,0x00,
};
char chu_16[]={
/* 以下是 '出' 的 16点阵宋体 字模,32 byte */
0x01,0x00,0x01,0x00,0x21,0x04,0x21,0x04,
0x21,0x04,0x21,0x04,0x3F,0xFC,0x21,0x04,
0x01,0x00,0x21,0x04,0x21,0x04,0x21,0x04,
0x21,0x04,0x3F,0xFC,0x20,0x04,0x00,0x00,
};
char you_16[]={
/* 以下是 '游' 的 16点阵宋体 字模,32 byte */
0x44,0x20,0x22,0x20,0x22,0x20,0x0F,0xBE,
0x84,0x40,0x54,0xBC,0x57,0x08,0x25,0x10,
0x25,0x10,0x25,0x7E,0xC5,0x10,0x49,0x10,
0x49,0x10,0x49,0x10,0x55,0x50,0x42,0x20,
};
char xi_16[]={
/* 以下是 '戏' 的 16点阵宋体 字模,32 byte */
0x00,0x40,0x00,0x50,0x7E,0x48,0x02,0x48,
0x44,0x40,0x25,0xFE,0x18,0x40,0x08,0x44,
0x0C,0x28,0x14,0x30,0x12,0x20,0x22,0x50,
0x41,0x92,0x06,0x0A,0x00,0x06,0x00,0x02,
};
char xia_16[]={
/* 以下是 '下' 的 16点阵宋体 字模,32 byte */
0x00,0x04,0x7F,0xFE,0x01,0x00,0x01,0x00,
0x01,0x00,0x01,0xC0,0x01,0x60,0x01,0x30,
0x01,0x20,0x01,0x00,0x01,0x00,0x01,0x00,
0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
};
char ge_16[]={
/* 以下是 '个' 的 16点阵宋体 字模,32 byte */
0x01,0x00,0x01,0x00,0x02,0x80,0x02,0x40,
0x04,0x20,0x09,0x18,0x11,0x0E,0x61,0x04,
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,
0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
};
char tu_16[]={
/* 以下是 '图' 的 16点阵宋体 字模,32 byte */
0x00,0x00,0x7F,0xFC,0x44,0x04,0x47,0xE4,
0x44,0x44,0x4A,0x84,0x71,0x04,0x42,0xC4,
0x4C,0x3C,0x71,0x14,0x40,0x84,0x42,0x04,
0x41,0x84,0x41,0x04,0x7F,0xFC,0x40,0x04,
};
char xing_16[]={
/* 以下是 '形' 的 16点阵宋体 字模,32 byte */
0x00,0x04,0x7F,0x86,0x12,0x0C,0x12,0x10,
0x12,0x20,0x12,0x08,0xFF,0xCC,0x12,0x18,
0x12,0x20,0x12,0x44,0x12,0x86,0x12,0x0C,
0x22,0x10,0x22,0x20,0x42,0x40,0x80,0x80,
};
char fen_16[]={
/* 以下是 '分' 的 16点阵宋体 字模,32 byte */
0x08,0x80,0x0C,0x80,0x08,0x40,0x10,0x20,
0x10,0x30,0x20,0x18,0x40,0x0E,0x9F,0xE4,
0x04,0x20,0x04,0x20,0x04,0x20,0x04,0x20,
0x08,0x20,0x10,0xA0,0x20,0x40,0x40,0x00,
};
char shu_16[]={
/* 以下是 '数' 的 16点阵宋体 字模,32 byte */
0x08,0x20,0x49,0x30,0x2A,0x20,0x1C,0x20,
0xFF,0x7E,0x1C,0x44,0x2B,0x44,0x48,0xC4,
0x08,0x28,0xFF,0x28,0x12,0x10,0x34,0x10,
0x0C,0x28,0x32,0x4E,0xC0,0x84,0x00,0x00,
};
char bd_16[]={
/* 以下是 ':' 的 16点阵宋体 字模,32 byte */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,
0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,
};
char su_16[]={
/* 以下是 '速' 的 16点阵宋体 字模,32 byte */
0x00,0x80,0x40,0x80,0x2F,0xFC,0x20,0x80,
0x00,0x80,0x07,0xF8,0xE4,0x88,0x24,0x88,
0x27,0xF8,0x21,0xA0,0x22,0x98,0x2C,0x88,
0x20,0x80,0x50,0x80,0x8F,0xFE,0x00,0x00,
};
char _16[]={
/* 以下是 '度' 的 16点阵宋体 字模,32 byte */
0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,
0x22,0x20,0x2F,0xFC,0x22,0x20,0x23,0xE0,
0x20,0x00,0x27,0xF8,0x22,0x10,0x21,0x20,
0x20,0xC0,0x41,0x30,0x46,0x0E,0x98,0x04,
};
char cong_16[]={
/* 以下是 '从' 的 16点阵宋体 字模,32 byte */
0x08,0x40,0x08,0x40,0x08,0x40,0x08,0x40,
0x08,0x40,0x08,0x40,0x08,0x40,0x08,0xA0,
0x14,0xA0,0x12,0x90,0x12,0x90,0x21,0x08,
0x23,0x0C,0x42,0x06,0x84,0x04,0x08,0x00,
};
char xin_16[]={
/* 以下是 '新' 的 16点阵宋体 字模,32 byte */
0x10,0x00,0x08,0x0E,0x7F,0x70,0x22,0x40,
0x14,0x40,0xFF,0x7E,0x08,0x48,0x08,0x48,
0xFF,0x48,0x08,0x48,0x2C,0x48,0x2A,0x88,
0x4A,0x88,0x89,0x08,0x2A,0x08,0x10,0x08,
};
char nan_16[]={
/* 以下是 '难' 的 16点阵宋体 字模,32 byte */
0x00,0xA0,0x00,0x90,0xF9,0x10,0x09,0xFE,
0x8B,0x20,0x4B,0x20,0x2D,0xFC,0x11,0x20,
0x19,0x20,0x29,0xFC,0x25,0x20,0x45,0x20,
0x81,0x20,0x01,0xFE,0x01,0x00,0x01,0x00,
};
char xuan_16[]={
/* 以下是 '选' 的 16点阵宋体 字模,32 byte */
0x00,0x40,0x22,0x40,0x12,0x40,0x13,0xF8,
0x04,0x40,0x00,0x40,0xF7,0xFC,0x11,0x20,
0x11,0x20,0x12,0x24,0x12,0x24,0x14,0x1C,
0x10,0x00,0x28,0x00,0x47,0xFE,0x00,0x00,
};
char ze_16[]={
/* 以下是 '择' 的 16点阵宋体 字模,32 byte */
0x20,0x00,0x23,0xF8,0x21,0x10,0xFC,0xA0,
0x20,0x40,0x20,0xA0,0x2B,0x58,0x30,0x46,
0x63,0xF8,0xA0,0x40,0x20,0x40,0x27,0xFE,
0x20,0x40,0x20,0x40,0xA0,0x40,0x40,0x40,
};

/* 函数申明 */
int getkey();
void draw_brick(int kind,int stat,int start_x,int start_y,int *position);
void display_brick(int *position,int xexcursion,int yexcursion);
void draw_freezebrick(int *space);
void draw_mainboard();
int is_movable(int *position,int *space,int dir);
void freeze(int *position,int *space);
int check_bottom(int *space,int hei);
void ERASE(int top,int flag);
int move_down(int *position,int *space,int *top,int *flag,int speed);
void begin();
void rand_select(int *kind,int *stat);
void updata_data(int *space,int *top,int flag);
void preview(int kind_new,int stat_new);
void convert_score(long score,char *str);
void drawmat(char *,int,int,int,int);
/**************************************************/

/**********************************************************/
void drawmat(char *mat,int matsize,int x,int y,int color)
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{
int i, j, k, n;
n = (matsize - 1) / 8 + 1;
for(j = 0; j < matsize; j++)
for(i = 0; i < n; i++)
for(k = 0;k < 8; k++)
if(mat[j * n + i] & (0x80 >> k)) /*测试为1的位则显示*/
putpixel(x + i * 8 + k, y + j, color);
}

/*初始化游戏板巨阵!*/
void init_data(int **space)
{
int hei,wid,i,j;
*space=(int *)malloc(264*sizeof(int));
/*分配的空间,"264" 是数组的长度*/
for(i=0;i<difficulty;i++)
for(j=0;j<12;j++)
board[21-i][j]=random(2);
for(hei=0;hei<22;hei++)
for(wid=0;wid<12;wid++)
*(*space+hei*12+wid)=board[hei][wid];

/*初始化*/
score=0;
}

void convert_score(long score,char *str)
{
int i;
long SCORE;
SCORE=score;
for(i=7;i>=0;i--)
{
str[i]='0'+SCORE%10;
SCORE /=10;
}
}
/**************************************************/

/*获取键盘扫描码 */
int getkey()
{
union REGS key;
key.h.ah=0;
int86(0x16,&key,&key);
return key.h.ah;
}

/*****************************************************/

/*画方格图形*/
void mydraw(int start_x,int start_y,int brick_width,int color)
{
settextstyle(4,0,5);
setfillstyle(6,8);
setcolor(color);
rectangle(start_x,start_y,start_x+brick_width,start_y+brick_width);
rectangle(start_x+2,start_y+2,start_x+brick_width-2,start_y+brick_width-2);
outtextxy(start_x+7,start_y+7,"*");
}

/******************************************************/

/*
用mydraw函数和position数组在屏幕上画方块*/
void draw_brick(int kind,int stat,int start_x,int start_y,int *position)
{
int wid,hei,x,y,i=0;
void *buf;
setviewport(100,40,300,440,1);
buf=malloc(400);
for(i=0;i<8;i+=2)
{
x=position[i];y=position[i+1];
getimage(x,y,x+brick_width,y+brick_width,buf);
putimage(x,y,buf,XOR_PUT);
}
i=0;
for(hei=0;hei<4;hei++)
{
y=start_y+hei*brick_width;
for(wid=0;wid<4;wid++)
if(brick[kind][stat][hei][wid]==1)
{
x=start_x+wid*brick_width;
position[i++]=x;
position[i++]=y;
position[10]= x > position[10] ? x : position[10];
position[11]= y > position[11] ? y : position[11];
mydraw(x,y,brick_width,10);
}
}
position[8]=start_x;
position[9]=start_y;

free(buf); /* 释放 "buf" 所占的内存*/
}
void display_score(char *pstr) /*显示积分和速度(10000分加一级,最大9级)*/
{
void *buf;int n,j;char speed[2];
buf=malloc(3200);
itoa(m_speed,&speed,10);/*需要改进*/
setviewport(479,139,560,170,1);
getimage(0,0,80,40,buf);
putimage(0,0,buf,XOR_PUT);
settextstyle(0,0,1);
setcolor(WHITE);
outtextxy(0,0,pstr);
outtextxy(0,22,speed);
if(m_speed<=9)
{
n=atoi(pstr);
m_speed+=n/10000;
}
free(buf);

}

2. 大家在linux上用什么软件看cpp的源码

如果是只有几个cpp文件,用文本编辑器软件查看源码就可以了,根据个人喜好来选择,常见的编辑器比如gedit、VScode、Sublime Text都可以;如果是一个项目,里面有很多源码文件,经常需要在不同的源码文件之间切换,用文本编辑器软件查看就没有那么方便了,这时推荐用专门的源码阅读软件Source Insight,这个软件只有Windows版,但是它在Linux的Wine环境中运行的非常好,所以在Linux上安装一个wine就可以用这个软件了,这个软件可以很方便的跳转到变量、函数、类的定义处,还能前进、后退,阅读源码非常方便。

3. 我下载了一个小游戏源码,里面有好几个CPP文件这是为什么

不是的,一个程序一个的那是main函数,多个cpp文件是对整个程序分块编程,这样你看主程序的时候会比较清晰,而不是一大团代码挤在一块,并且这么多cpp文件中只有一个main函数

4. 如何用c++输出该程序本身的源代码

//这个很简单,比如xxx.cpp文件

#include<stdio.h>
FILE*a=fopen("xxx.cpp","r");
intmain()
{charc;
/*
很多代码。。。
*
/

while(!feof(a))
{
c=fgetc(a);
putchar(c);
}
getchar();
}

//这样就可以了

5. 有什么工具可以检测 C++ 代码抄袭吗

本文浅谈一下C++内存泄漏的检测,首先我们需要知道程序有没有内存泄露,然后定位到底是哪行代码出现内存泄露了,这样才能将其修复。最简单的方法当然是借助于专业的检测工具,比较有名如BoundsCheck工具,功能非常强大,相信做C++开发的人都离不开它。此外就是不使用任何工具,而是自己来实现对内存泄露的监控,分如下两种情况:一.在MFC中检测内存泄漏假如是用MFC的程序的话,很简单。默认的就有内存泄露检测的功能。我们用VS2005生成了一个MFC的对话框的程序,发现他可以自动的检测内存泄露.不用我们做任何特殊的操作.仔细观察,发现在每个CPP文件中,都有下面的代码:#ifdef_DEBUG#definenewDEBUG_NEW#endifDEBUG_NEW这个宏定义在afx.h文件中,就是它帮助我们定位内存泄漏。在含有以上代码的cpp文件中分配内存后假如没有删除,那么停止程序的时候,VisualStudio的Output窗口就会显示如下的信息了:Detectedmemoryleaks!Dumpingobjects->d:\code\mfctest\mfctest.cpp(80):{157}normalblockat0x003AF170,4byteslong.Data:00000000Objectmpcomplete.在Output窗口双击粗体字那一行,那么IDE就会打开该文件,定位到该行,很容易看出是哪出现了内存泄露。二.检测纯C++的程序内存泄露我试了下用VisualStudio建立的Win32ConsoleApplication和Win32Project项目,结果都不能检测出内存泄露。下面一步一步来把程序的内存泄露检测的机制建立起来。首先,我们需要知道C运行库的Debug版本提供了许多检测功能,使得我们更容易的Debug程序。在MSDN中有专门的章节讲这个,叫做DebugRoutines,建议大家先看看里面的内容吧。我们会用到里面很重要的几个函数。

6. c++编写的.cpp源代码怎么执行

菜单build目录下面点击build solution,然后点击Debug下的start就可以了

7. 在VC++6.0中编译下面的代码后怎么解决出现的那个问题

  1. 确保两个源代码文件在同一目录下;

  2. 确认文件都已经加入到同一工程中;

  3. 确认工程文件已保存。

8. cppcheck怎么安装和实用

首先先下载 成功后:
编译:
g++ -o cppcheck -Ilib cli/*.cpp lib/*.cpp
安装:
make install
cppcheck默认安装会报错如下:
cppcheck: unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.
(information) Failed to load std.cfg. Your Cppcheck installation is broken, please re-install. The Cppcheck binary was compiled without CFGDIR set. Either the std.cfg should be available in cfg or the CFGDIR should be configured.
因此我直接不用他们的make,而是直接使用g++参数编译:g++ -o cppcheck -Ilib cli/*.cpp lib/*.cpp
然后就是检查了
cppcheck -j 3 --enable=all src/
使能检查规则:
默认:--enable=error
--enable=all
--enable=unusedFuntion path
--enable=style
规则定义:
error:出现的错误
warning:为了预防bug防御性编程建议信息
style:编码格式问题(没有使用的函数、多余的代码等)
portablity:移植性警告。该部分如果移植到其他平台上,可能出现兼容性问题
performance:建议优化该部分代码的性能
information:一些有趣的信息,可以忽略不看的。
保存结果到文件中:重定向“>”
-j 3 使用3个线程,如果代码工程太大,可以使用15-20个,自己随意发挥,不过还是参考cpuinfo
如果是Windows的版本呢,就这样使用:
1、将Cppcheck绿色版cppcheck.rar解压
2、打开cppcheckgui.exe;
3、菜单 check->directory;
4、选择源代码目录,确认;
5、Cppcheck自动开始走查;

9. 求一份c语言的RTP音频传输源码

1.项目前期工作(配置好环境)
2.发送端文件编写(见下面的send.cpp)
3.接收端文件编写(见下面的receive.cpp)
4.编译文件
(1)发送端
g++-osendsend.cpp-I/usr/local/include/jrtplib3/-ljrtp
(2)接收端
g++-oreceivereceive.cpp-I/usr/local/include/jrtplib3/-ljrtp
附录:
(1)send.cpp
[cpp]
#include"rtpsession.h"
#include"rtppacket.h"
#include"rtpudpv4transmitter.h"
#include"rtpipv4address.h"
#include"rtpsessionparams.h"
#include"rtperrors.h"
#include"rtpmemorymanager.h"
#ifndefWIN32
#include<netinet/in.h>
#include<arpa/inet.h>
#else
#include<winsock2.h>
#endif//WIN32
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<string>

//
//.Ifso,itdisplaysanerror
//messageandexists.
//

voidcheckerror(intrtperr)
{
if(rtperr<0)
{
std::cout<<"ERROR:"<<RTPGetErrorString(rtperr)<<std::endl;
exit(-1);
}
}

//
//Themainroutine
//

#ifdefRTP_SUPPORT_THREAD

classMyMemoryManager:publicRTPMemoryManager
{
public:
MyMemoryManager()
{
mutex.Init();
alloccount=0;
freecount=0;
}
~MyMemoryManager()
{
std::cout<<"alloc:"<<alloccount<<"free:"<<freecount<<std::endl;
}
void*AllocateBuffer(size_tnumbytes,intmemtype)
{
mutex.Lock();
void*buf=malloc(numbytes);
std::cout<<"Allocated"<<numbytes<<"bytesatlocation"<<buf<<"(memtype="<<memtype<<")"<<std::endl;
alloccount++;
mutex.Unlock();
returnbuf;
}

voidFreeBuffer(void*p)
{
mutex.Lock();
std::cout<<"Freeingblock"<<p<<std::endl;
freecount++;
free(p);
mutex.Unlock();
}
private:
intalloccount,freecount;
JMutexmutex;
};

#else

classMyMemoryManager:publicRTPMemoryManager
{
public:
MyMemoryManager()
{
alloccount=0;
freecount=0;
}
~MyMemoryManager()
{
std::cout<<"alloc:"<<alloccount<<"free:"<<freecount<<std::endl;
}
void*AllocateBuffer(size_tnumbytes,intmemtype)
{
void*buf=malloc(numbytes);
std::cout<<"Allocated"<<numbytes<<"bytesatlocation"<<buf<<"(memtype="<<memtype<<")"<<std::endl;
alloccount++;
returnbuf;
}

voidFreeBuffer(void*p)
{
std::cout<<"Freeingblock"<<p<<std::endl;
freecount++;
free(p);
}
private:
intalloccount,freecount;
};

#endif//RTP_SUPPORT_THREAD

intmain(void)
{
#ifdefWIN32
WSADATAdat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif//WIN32

MyMemoryManagermgr;
RTPSessionsess(&mgr);
uint16_tportbase,destport;
uint32_tdestip;
std::stringipstr;
intstatus,i,num;

//First,we'

std::cout<<"Enterlocalportbase:"<<std::endl;
std::cin>>portbase;
std::cout<<std::endl;

std::cout<<"EnterthedestinationIPaddress"<<std::endl;
std::cin>>ipstr;
destip=inet_addr(ipstr.c_str());
if(destip==INADDR_NONE)
{
std::cerr<<"BadIPaddressspecified"<<std::endl;
return-1;
}

//Theinet_,but
//,soweuseacallto
//ntohl
destip=ntohl(destip);

std::cout<<"Enterthedestinationport"<<std::endl;
std::cin>>destport;

std::cout<<std::endl;
std::cout<<":"<<std::endl;
std::cin>>num;

//Now,we'llcreateaRTPsession,setthedestination,sendsome
//packetsandpollforincomingdata.

;
RTPSessionParamssessparams;

//IMPORTANT:,otherwise
//
//Inthiscase,we',sowe'll
//putthetimestampunitto(1.0/10.0)
sessparams.SetOwnTimestampUnit(1.0/10.0);

sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status=sess.Create(sessparams,&transparams);
checkerror(status);

RTPIPv4Addressaddr(destip,destport);

status=sess.AddDestination(addr);
checkerror(status);

for(i=1;i<=num;i++)
{
printf(" Sendingpacket%d/%d ",i,num);

//sendthepacket
status=sess.SendPacket((void*)"1234567890",10,0,false,10);
checkerror(status);

sess.BeginDataAccess();

//checkincomingpackets
if(sess.GotoFirstSourceWithData())
{
do
{
RTPPacket*pack;

while((pack=sess.GetNextPacket())!=NULL)
{
//Youcanexaminethedatahere
printf("Gotpacket! ");

//wedon'tlongerneedthepacket,so
//we'lldeleteit
sess.DeletePacket(pack);
}
}while(sess.GotoNextSourceWithData());
}

sess.EndDataAccess();

#ifndefRTP_SUPPORT_THREAD
status=sess.Poll();
checkerror(status);
#endif//RTP_SUPPORT_THREAD

RTPTime::Wait(RTPTime(1,0));
}

sess.BYEDestroy(RTPTime(10,0),0,0);

#ifdefWIN32
WSACleanup();
#endif//WIN32
return0;
}
(2)receive.cpp
[cpp]viewplain
#include"rtpsession.h"
#include"rtppacket.h"
#include"rtpudpv4transmitter.h"
#include"rtpipv4address.h"
#include"rtpsessionparams.h"
#include"rtperrors.h"
#ifndefWIN32
#include<netinet/in.h>
#include<arpa/inet.h>
#else
#include<winsock2.h>
#endif//WIN32
#include"rtpsourcedata.h"
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<string>

//
//.Ifso,itdisplaysanerror
//messageandexists.
//

voidcheckerror(intrtperr)
{
if(rtperr<0)
{
std::cout<<"ERROR:"<<RTPGetErrorString(rtperr)<<std::endl;
exit(-1);
}
}

//
//Thenewclassroutine
//

classMyRTPSession:publicRTPSession
{
protected:
voidOnNewSource(RTPSourceData*dat)
{
if(dat->IsOwnSSRC())
return;

uint32_tip;
uint16_tport;

if(dat->GetRTPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTPDataAddress());
ip=addr->GetIP();
port=addr->GetPort();
}
elseif(dat->GetRTCPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTCPDataAddress());
ip=addr->GetIP();
port=addr->GetPort()-1;
}
else
return;

RTPIPv4Addressdest(ip,port);
AddDestination(dest);

structin_addrinaddr;
inaddr.s_addr=htonl(ip);
std::cout<<"Addingdestination"<<std::string(inet_ntoa(inaddr))<<":"<<port<<std::endl;
}

voidOnBYEPacket(RTPSourceData*dat)
{
if(dat->IsOwnSSRC())
return;

uint32_tip;
uint16_tport;

if(dat->GetRTPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTPDataAddress());
ip=addr->GetIP();
port=addr->GetPort();
}
elseif(dat->GetRTCPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTCPDataAddress());
ip=addr->GetIP();
port=addr->GetPort()-1;
}
else
return;

RTPIPv4Addressdest(ip,port);
DeleteDestination(dest);

structin_addrinaddr;
inaddr.s_addr=htonl(ip);
std::cout<<"Deletingdestination"<<std::string(inet_ntoa(inaddr))<<":"<<port<<std::endl;
}

voidOnRemoveSource(RTPSourceData*dat)
{
if(dat->IsOwnSSRC())
return;
if(dat->ReceivedBYE())
return;

uint32_tip;
uint16_tport;

if(dat->GetRTPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTPDataAddress());
ip=addr->GetIP();
port=addr->GetPort();
}
elseif(dat->GetRTCPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTCPDataAddress());
ip=addr->GetIP();
port=addr->GetPort()-1;
}
else
return;

RTPIPv4Addressdest(ip,port);
DeleteDestination(dest);

structin_addrinaddr;
inaddr.s_addr=htonl(ip);
std::cout<<"Deletingdestination"<<std::string(inet_ntoa(inaddr))<<":"<<port<<std::endl;
}
};

//
//Themainroutine
//

intmain(void)
{
#ifdefWIN32
WSADATAdat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif//WIN32

MyRTPSessionsess;
uint16_tportbase;
std::stringipstr;
intstatus,i,num;

//First,we'

std::cout<<"Enterlocalportbase:"<<std::endl;
std::cin>>portbase;
std::cout<<std::endl;

std::cout<<std::endl;
std::cout<<"Numberofsecondsyouwishtowait:"<<std::endl;
std::cin>>num;

//Now,we'llcreateaRTPsession,setthedestination
//andpollforincomingdata.

;
RTPSessionParamssessparams;

//IMPORTANT:,otherwise
//
//Inthiscase,we'.
sessparams.SetOwnTimestampUnit(1.0/8000.0);

sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status=sess.Create(sessparams,&transparams);
checkerror(status);

for(i=1;i<=num;i++)
{
sess.BeginDataAccess();

//checkincomingpackets
if(sess.GotoFirstSourceWithData())
{
do
{
RTPPacket*pack;

while((pack=sess.GetNextPacket())!=NULL)
{
//Youcanexaminethedatahere
printf("Gotpacket! ");

//wedon'tlongerneedthepacket,so
//we'lldeleteit
sess.DeletePacket(pack);
}
}while(sess.GotoNextSourceWithData());
}

sess.EndDataAccess();

#ifndefRTP_SUPPORT_THREAD
status=sess.Poll();
checkerror(status);
#endif//RTP_SUPPORT_THREAD

RTPTime::Wait(RTPTime(1,0));
}

sess.BYEDestroy(RTPTime(10,0),0,0);

#ifdefWIN32
WSACleanup();
#endif//WIN32
return0;
}

热点内容
洗过的海带存储方法 发布:2024-10-10 11:19:00 浏览:239
64H的源码 发布:2024-10-10 11:06:01 浏览:152
mc服务器怎么增加back指令 发布:2024-10-10 10:58:48 浏览:256
termux如何搭建服务器 发布:2024-10-10 10:18:05 浏览:737
中国石化的电话服务密码是多少 发布:2024-10-10 10:16:46 浏览:42
婚纱店宣传片视频脚本 发布:2024-10-10 10:08:55 浏览:869
android写入文件 发布:2024-10-10 10:08:11 浏览:435
怎么打开文件夹的路径 发布:2024-10-10 10:08:06 浏览:61
ec服务器怎么有小提示 发布:2024-10-10 10:08:04 浏览:495
我的世界迪士尼神奇宝贝服务器地址 发布:2024-10-10 09:03:02 浏览:559