C++ rtmp livestream 流媒体

海阔天空 张翼飞翔

我的学习笔记。--我喜欢这里,因为这里安静,无广告骚扰。
随笔 - 82, 文章 - 2, 评论 - 126, 引用 - 0
数据加载中……

VC++ 6.0小知识点滴

//获取/设置当前目录 临时目录
 char strDir[255];
 GetCurrentDirectory(sizeof(strDir),strDir);
 SetCurrentDirectory(strDir);
 GetTempPath(sizeof(strDir),strDir);


//获取程序执行完整路径
char strFile[BUFSIZ] = {0};
::GetModuleFileName(NULL, strFile, sizeof(strFile));
 

//获取命令行参数
 AfxGetApp()->m_lpCmdLine; 

//获取当前计算机IP 和计算机名
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
char* GetIP(char* strBuf)
{
 WSADATA wsaData;
 char name[155];
 char *ip;
 PHOSTENT hostinfo;
 if ( WSAStartup( MAKEWORD(2,0), &wsaData ) == 0 )
 { 
  if( gethostname ( name, sizeof(name)) == 0) { //这里获取计算机名
   if((hostinfo = gethostbyname(name)) != NULL) { //这些就是获得IP的函数
    ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
    strcpy(strBuf,ip);
    //printf("%s\n",ip);
   }
  }
  WSACleanup( );
 }
 return strBuf;
}


//获取当前用户名
 CHAR szUserName[80]; 
 DWORD dwResult, cchBuff = 80; 
 
 // Call the WNetGetUser function.
 //
 dwResult = GetUserName((LPSTR) szUserName, &cchBuff); 

//读写注册表
 char strBuf[BUFSIZ];
 HKEY   hKeyDir;  
 HKEY   hMakeKey;
 DWORD dwSize;
 DWORD dwValue = 0x02;
  
 RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE",0,KEY_WRITE|KEY_READ,&hKeyDir);
 
 LONG lRet = RegQueryValueEx(hKeyDir, "workdir", 0, REG_NONE, (LPBYTE)strBuf, &dwSize);  
 if(lRet   ==   ERROR_SUCCESS)  //成功
 {
  RegSetValueEx(hKeyDir,"keyNamesz",0,REG_SZ,(LPBYTE)"value",BUFSIZ);
  RegSetValueEx(hKeyDir,"keyNamedw",0,REG_DWORD,(BYTE*)&dwValue,sizeof(DWORD));    
  RegCreateKey(hKeyDir,"makeKey",&hMakeKey);
 }

//开机自动运行
 HKEY   hKeyAutoRun;
 unsigned char   strRun[BUFSIZ] = "I:\\LiveDesktop.exe"; 
 RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_WRITE|KEY_READ,&hKeyAutoRun);  
 RegSetValueEx(hKeyAutoRun,"LiveDesktop",0,REG_SZ,strRun,strlen((char*)strRun));

//进程互斥运行
//在App::InitInstance()中写入
 HANDLE m_hReceiveMap = OpenFileMapping(FILE_MAP_READ, FALSE, "DataMap");
 if (m_hReceiveMap == NULL)
 {  
  HANDLE hRecvMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, 1000000, "DataMap");
 }
 else
 {
  AfxMessageBox("本程序已经在运行中!");
  return FALSE;
 }

//关闭窗口
char* strApp = "cmd.exe";
CWnd* wnd = =FindWindow(NULL,strApp );
HWND hWnd = wnd->GetSafeHwnd();
::SendMessage(hWnd,0x0010, 0, 0);


//关闭进程
#include <Tlhelp32.h>
BOOL CloseProcess(char* strExeFile)
{
 HANDLE hProcessSnap = NULL;
 BOOL bRet = FALSE;
 PROCESSENTRY32 pe32 = {0};
 
 // 做一个系统中当前进程列表的快照  
 hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 if (hProcessSnap == INVALID_HANDLE_VALUE)
  return (FALSE);
 
 // 初始化PROCESSENTRY32结构  
 pe32.dwSize = sizeof(PROCESSENTRY32);
 
 // 遍历进程列表,结构体pe32保存了进程的ID,进程名,优先级等信息,你可以利用这个结构来判断是不是你想要操作的进程。  
 if (Process32First(hProcessSnap, &pe32))
 {
  do
  {
   //在这里你可以对当前枚举到的进程进行处理,如果是你需要结果的进程,就
   if(strcmp( pe32.szExeFile,strExeFile) == 0)
   {
    HANDLE hProcess=OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
    if(hProcess)
    {
     TerminateProcess(hProcess,0);
     bRet = TRUE;
     break;
    }
   }
  }
  while (Process32Next(hProcessSnap, &pe32));   
 }
 
 // Do not forget to clean up the snapshot object.  
 CloseHandle (hProcessSnap);
 return (bRet);
}


//判断文件或文件夹是否存在
 HANDLE hFind = FindFirstFile("c:\\windows", &wfd);  
  if(hFind!=INVALID_HANDLE_VALUE)//find
  {
   return TRUE;
  }

//Edit控件自动下翻
 UpdateData(FALSE); 
 m_Report.SetSel(m_strMsg.GetLength(), m_strMsg.GetLength()); 

//按钮撤消 /禁止
CButton* pButton = (CButton*)GetDlgItem(IDOK);
pButton->DestroyWindows();//撤消
pButton->EnableWindow(FALSE);//禁止

//文件读写
<io.h>
FILE* fp = fopen("fname","rb");// "w"重新写覆盖 "r"只读 "a"尾部添加 "b"二进制,否则读\r\n得到\n
int size = _filelength(_fileno()fp);
char ch = fgetc(fp);
fwrite(buf,size,1,fp);
fclose(fp);

//常量修饰
char* const str; //str不能改变 例如不能 str = str2
const char* str; //str指向的数据不能改变 例如不能 str[0] = 'X'

其他内存常量区域的数据也不能改变 例如:
#define STR "string"
char* str2 = "string";

//结构的4种定义方法
struct tagSTRUCT1{//定义一个结构
 int n;
};

typedef struct tagSTRUCT2{//给结构一个别名
 int n;
}STRUCT2;

typedef struct tagSTRUCT3{//二种别名
 int n;
}STRUCT3, *PSTRUCT3;

typedef struct{//只定义别名
 int n;
}STRUCT4, *PSTRUCT4;


//函数指针的使用 
int Fun(char c,int n)
{
 return c+n;
}

mian()
{
//使用函数指针
 int (*pFun)(char,int);
 pFun = Fun;
 pFun('A',1);
//强制转换函数指针
 void* pVoid = Fun;
 pFun = (int (*)(char,int))pVoid;
}
Socket 服务端

/** **********************************************************************
/* 
/* 作者: zyf
/* 博客: 
http://www.cnweblog.com/fly2700/
                                                     
/***********************************************************************
*/


#include 
< stdio.h >
#include 
< winsock.h >
#pragma comment(lib,
" Wsock32.lib " )

void *  ThreadRecv( void *
 param)
{
    SOCKET
*  pSockConn  =  (SOCKET *
)param;

    
char  recvbuffer[ 128
];   
    
int  nRecv  =   0
;
    
while (SOCKET_ERROR  !=
 nRecv)
    
{
        nRecv 
=  recv(  * pSockConn, recvbuffer,  128 0
);   
        printf(
" socket=%d,recv=%d\n " , *
pSockConn,nRecv);
        
if (SOCKET_ERROR  ==
 nRecv)
        
{
            
break
;
        }
            
        send( 
* pSockConn, recvbuffer, nRecv,  0
);
    }

    
    
// 关闭Socket   
    printf( " socket=%d,threadClose\n " , * pSockConn);
    closesocket( 
*
pSockConn);   
    delete pSockConn;

    
return
 NULL;
}


void  main()    
{     
    printf(
" server start.\n "
);

    
// 版本协商    

    WORD wVersionRequested;     
    WSADATA wsaData;    
    
int
 err;   

    sockaddr_in addrClient;    
    
int  len  =   0
;  
    
// int nRecv = 0;

    BOOL bRuning  =  TRUE;

    
// char sendbuffer[128] = {0};    


    wVersionRequested 
=  MAKEWORD( 1 , 1 );  // 0x0101    
    err  =  WSAStartup(wVersionRequested, & wsaData);    
    
if ( 0   !=
 err)   
    
{   
        
return
;   
    }
   

    
if (LOBYTE(wsaData.wVersion) != 1   ||  HIBYTE(wsaData.wVersion) != 1
)    
    
{   
        WSACleanup();   
        
return
;    
    }
   
  
    
// 创建Socket     

    SOCKET sockSvr  =  socket(AF_INET,SOCK_STREAM, 0 );   
  
    
// 创建IP地址和端口    

    SOCKADDR_IN addrSvr;   
    addrSvr.sin_addr.S_un.S_addr 
=
 htonl(INADDR_ANY);   
    addrSvr.sin_family 
=
 AF_INET;   
    addrSvr.sin_port 
=  htons( 6000
);   
  
    
// 绑定端口监听   

     int  ret  =  bind(sockSvr,(SOCKADDR * ) & addrSvr,sizeof(SOCKADDR));    
    
if  ( 0   !=
 ret)
    
{
        printf(
" bind failue.\n "
);
        closesocket(sockSvr);   
        WSACleanup();
        
return
 ;
    }

    
    listen(sockSvr,
5 );  //
     while
(bRuning)   
    
{                     
        printf(
" listen.\n "
);  
        SOCKET
*  pSockConn   =   new
 SOCKET();
        len 
=
 sizeof(sockaddr); 
        
// 阻塞方法,获得一个客户Socket连接  

         * pSockConn  =  accept(sockSvr, (sockaddr * ) & addrClient,  & len);  
        printf(
" accept.\n "
);         
        CreateThread(NULL, 
0 , (LPTHREAD_START_ROUTINE)ThreadRecv, pSockConn,  0
, NULL);     
    }
   
  
    closesocket(sockSvr);  
    
// 释放Winsock资源

    WSACleanup();
}
  



TCP Socket 客户端
/** **********************************************************************
/* 
/* 作者: zyf
/* 博客: 
http://www.cnweblog.com/fly2700/
                                                 
/***********************************************************************
*/


#include 
< stdio.h >
#include 
< winsock.h >
#pragma comment(lib,
" Wsock32.lib " )

#define    NO_FLAGS_SET 
0

unsigned 
int  g_socket;    

BOOL Connect(
const   char *
 address, UINT port)
{
    WSADATA Data;
    SOCKADDR_IN destSockAddr;

    
int
 ret;

    ret 
=  WSAStartup(MAKEWORD( 1 1 ),  &
Data);
    
if  ( 0   !=
 ret)
    
{
        printf( 
" ERROR: WSAStartup unsuccessful.\n "
);
        
return
 FALSE;
    }


    
// 创建连向服务器的套接字   
    g_socket = socket(AF_INET, SOCK_STREAM,  0 );
    
    
// 创建地址信息   

    destSockAddr.sin_addr.S_un.S_addr  =  inet_addr(address);
    destSockAddr.sin_port 
=
 htons(port);
    destSockAddr.sin_family 
=
 AF_INET;
        
    
// 连接服务器 

    printf( " Trying to connect to ip:%s\n " ,address);
    ret 
=  connect(g_socket,(LPSOCKADDR)  &
destSockAddr, sizeof(destSockAddr));    
    
if  (ret  ==
 SOCKET_ERROR)
    
{
        printf(
" Connect error=%d.\n "
,GetLastError());
        WSACleanup();
        
return
 FALSE;
    }


    
return  TRUE;
}



int  Send( char *  buf,  int  len)
{
    
int  nSend  =
 send(g_socket, buf, len, NO_FLAGS_SET);
    printf(
" socket=%d,send=%d\n "
,g_socket, nSend);
    
//
char bufRecv[BUFSIZ] = {0};
    
// int nRecv = recv(g_socket, bufRecv, sizeof(bufRecv), NO_FLAGS_SET);

     /*
    for(int i=0; i<nRecv; i++)
    {
        printf("%c",bufRecv[i]);
    }
    printf("recv=%d",nRecv);
    
*/

    
return  nSend;
}



int  main()
{
    
char  strSend1[]  =   { " hellow1 " }
;
    
char  strSend2[]  =   { " hellow2 " }
;

    printf(
" client start.\n "
);
    
if ( ! Connect( " 127.0.0.1 " 6000 )) // 服务器127.0.0.1 端口6000

     {
        printf(
" connect failue.\n "
);
        
return   0
;
    }

    
// 连续发生二个小包 会有粘包现象。
    Send(strSend1, sizeof(strSend1));
    Send(strSend2, sizeof(strSend2));

    printf(
" end.\n "
);
    getchar();
    
    
return   0
;
}


//DLL 进程间的共享变量

#pragma data_seg("flag_data")
   int count=0;
#pragma data_seg()
#pragma comment(linker,"/SECTION:flag_data,RWS")

程序中:
   if(count>1)
     {
      MessageBox("已经启动了一个应用程序","Warning",MB_OK);
      return FLASE;
}
   count++;

posted on 2008-03-31 12:09 ZhangEF 阅读(288) 评论(0)  编辑  收藏 所属分类: Code