资源简介
本程序实现了linux环境下的udp打洞功能,通过udp打洞实现P2P通信。
代码片段和文件信息
/*************************************************************************
> File Name: my_debug.c
> Author: chad
> Mail: linczone@163.com
************************************************************************/
#include
#include
//#define _XOPEN_SOURCE /* glibc2 needs this */
//在makefile文件添加-D_GNU_SOURCE -D__USE_XOPEN即可
#include
#include
#include
#include
#include
#include
#include “my_debug.h“
/*
global variants
*/
//int debuglevels = DEBUG_LEVEL_NONE;
int __my_allow_debug_levels[MY_SECTION_END];
extern int errno;
//延时useconds 微秒
int my_usleep(int usec )
{
if( usec < 1 || usec > 999999 )
return -1;
struct timespec delay;
struct timespec rem;
memset( &delay 0 sizeof(delay));
memset( &rem 0 sizeof(rem));
delay.tv_nsec = usec*1000;
do{
if( !nanosleep( &delay &rem ))
{
break;
}
if( errno == EINTR ) {
printf(“my_usleep:rem.tv_sec=%ld\n“(int long)rem.tv_sec);
printf(“my_usleep:rem.tv_nsec=%ld\n“rem.tv_nsec);
delay.tv_sec = rem.tv_sec;
delay.tv_nsec = rem.tv_nsec;
} else {
break;
}
}while(1);
return 0;
}
//延时n秒
int my_sleep( int sec )
{
if( sec < 1 || sec > 999999999 )
return -1;
struct timespec delay;
struct timespec rem;
memset( &delay 0 sizeof(delay));
memset( &rem 0 sizeof(rem));
delay.tv_sec = sec;
do{
if( !nanosleep( &delay &rem ) )
{
break;
}
if( errno == EINTR ) {
printf(“my_sleep:rem.tv_sec=%ld\n“(int long)rem.tv_sec);
printf(“my_sleep:rem.tv_nsec=%ld\n“rem.tv_nsec);
delay.tv_sec = rem.tv_sec;
delay.tv_nsec = rem.tv_nsec;
} else {
break;
}
}while(1);
return 0;
}
//得到本地时间
void get_time( SYSTEM_TIME *tm )
{
struct tm tm_t;
time_t tt;
tt = time( NULL );
localtime_r( &tt &tm_t );
tm->year = tm_t.tm_year + 1900 - 2000;
tm->month = tm_t.tm_mon + 1;
tm->week = tm_t.tm_wday;//day of week
tm->day = tm_t.tm_mday;
tm->hour = tm_t.tm_hour;
tm->minute = tm_t.tm_min;
tm->second = tm_t.tm_sec;
}
int mystrtime( char *timebuf char* formt )
{
struct tm tm_t;
time_t t_time;
t_time = time( NULL );
localtime_r(&t_time &tm_t);
strftime(timebuf 100 formt &tm_t );
return 0;
}
/*
标准输出重定向
如果检测到/tiandao/stdout.txt文件存在,则将标准输出重定向该该文件
如果检测到该文件不存在则再重定向回标准输出
该函数为非线程安全函数
*/
int out_redirect( const char *path_name )
{
static int filefg =0;
static int oldstdout;
FILE *fpout;
if( !path_name || !path_name[0] )
return -1;
//测试文件是否存在
if( access( path_name F_OK|W_OK ) < 0 ) {
if( filefg ) {//如果文件已经打开
filefg = 0;
dup2( oldstdout1);
close(oldstdo
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-05-27 14:15 p2p-test\
目录 0 2015-05-27 14:11 p2p-test\.deps\
文件 2981 2015-04-21 10:10 p2p-test\.deps\my_debug.Po
文件 2718 2015-04-21 10:10 p2p-test\.deps\read_cmd.Po
文件 4452 2015-04-21 10:10 p2p-test\.deps\udp_client.Po
文件 3648 2015-04-21 10:10 p2p-test\.deps\udp_server.Po
文件 173 2015-04-18 11:37 p2p-test\ARM_MAKEFILE
文件 19921 2015-04-17 16:36 p2p-test\Makefile
文件 164 2015-04-17 16:35 p2p-test\Makefile.am
文件 20203 2015-04-17 16:35 p2p-test\Makefile.in
文件 34611 2015-04-17 16:35 p2p-test\aclocal.m4
文件 34420 2015-04-18 11:15 p2p-test\arm_udp_linux
目录 0 2015-05-27 14:11 p2p-test\autom4te.cache\
文件 165732 2015-04-17 16:35 p2p-test\autom4te.cache\output.0
文件 165732 2015-04-17 16:35 p2p-test\autom4te.cache\output.1
文件 6218 2015-04-17 16:35 p2p-test\autom4te.cache\requests
文件 33495 2015-04-17 16:35 p2p-test\autom4te.cache\traces.0
文件 26375 2015-04-17 16:35 p2p-test\autom4te.cache\traces.1
文件 2841 2015-04-17 16:36 p2p-test\config.h
文件 2606 2015-04-17 16:38 p2p-test\config.h.in
文件 16081 2015-04-17 16:38 p2p-test\config.log
文件 33152 2015-04-17 16:36 p2p-test\config.status
文件 165073 2015-04-17 16:35 p2p-test\configure
文件 799 2015-04-17 16:35 p2p-test\configure.in
文件 32 2015-04-17 16:35 p2p-test\depcomp
文件 0 2015-04-17 16:35 p2p-test\install-sh
文件 0 2015-04-17 16:35 p2p-test\missing
文件 7116 2015-05-27 14:14 p2p-test\my_debug.c
文件 5802 2015-05-27 14:14 p2p-test\my_debug.h
文件 17768 2015-04-21 10:10 p2p-test\my_debug.o
文件 2706 2015-05-27 14:14 p2p-test\read_cmd.c
............此处省略10个文件信息
评论
共有 条评论