免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2287 | 回复: 1
打印 上一主题 下一主题

函数中 Permission denied [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-09 10:41 |只看该作者 |倒序浏览
  1. class Keep
  2. {
  3. public:

  4.                 Keep();

  5.                 ~Keep();


  6.        
  7.                 int big_endian(void);

  8.                
  9.                 int SetTime(int t); //Set 频率

  10.                 /*通知接口*/

  11.                 /*keep Server */
  12.                 int ListenServerInital();

  13.                 void ListenServer();       

  14.                 /*keep Camera*/

  15.                 int UdpCamera(void * pVoid=NULL);

  16.                 static void * KeepListen(void *Arg);
  17.        
  18.                 int ListenInital();

  19.                 void Listen();

  20.                  int CameraInital();

  21.                 int CheckMessage(struct UdpAnswerMsg * Msg);

  22.                 void PrintfCamera(struct UdpAnswerMsg * Msg);               

  23.                 /*
  24.                  * 模拟IpCamera
  25.                  */

  26.                 int  ModeInital();
  27.                
  28.                 void ModeCamera();

  29.                 int  ModeCheck(struct UdpSearchMsg * Msg);

  30.                 void PrintModeDate(struct UdpSearchMsg * Msg);
  31.        
  32.                 /*Keep Camera*/
  33.                 int ListenSocket;   

  34.                 int SendtoSocket;
  35.        
  36.                 struct sockaddr_in ServerAddres; //localhost
  37.                
  38.                 struct sockaddr_in BroadAddress; //broaddress       
  39.                
  40.                 struct sockaddr_in CameraAddress;        //CameraAddress

  41.                 struct UdpSearchMsg SearchMsg;

  42.                 struct UdpSearchMsg *pSearchMsg;

  43.                 struct UdpAnswerMsg AnswerMsg;//复制接收缓存到这

  44.                 struct UdpAnswerMsg *pAnswerMsg; //指向接收缓存

  45.                 char SendTmp[27]; //Find Camera
  46.                 /*对外地址*/
  47.                 char RecvTmp[88]; //Declaer Camera
  48.                
  49.                 int ENABLE; //
  50.                                         // setscokopt        
  51.                 int DISBLE; //

  52.                 int state;  //  

  53.                 int time;   //频率

  54.                 socklen_t socklen;  //recvfrom

  55.                 /*大小端*/
  56.                 int nBig_End;
  57.                

  58. protected:

  59. private:
  60.                 /*Keep Server*/
  61.                

  62.                

  63.        

  64. };
复制代码



  1. #include "CKeep.h"

  2. //pthread_t ThreadID_CameraAlive;

  3. //Keep App;

  4. pthread_t g_thread;

  5. void* FKeepListen(void *arg)
  6. {
  7.        
  8.         Keep objKeep;

  9.        
  10.         if(-1==objKeep.ListenInital())
  11.         {
  12.                 perror("fKeep Failure Because ListenInital Failue");
  13.                
  14.                 exit(-1) ;
  15.         }

  16.         MSG("fKeep ListenInital Sucess");
  17.        
  18.        
  19.         objKeep.Listen(); //Begin Send and Listen Camera UDP

  20.        
  21. }

  22. int main()
  23. {

  24.        
  25.         if(-1==pthread_create(&g_thread,NULL,FKeepListen,NULL))
  26.         {
  27.                 perror("pthread run Listen Failue");

  28.                 perror("Program exit");

  29.                 exit(-1);
  30.         }

  31.         pthread_join(g_thread,NULL);

  32. }
复制代码
问题:

  上面的程序能正常运行,但是

在  FKeepListen(void *arg) 中 改为:

{
        Keep objKeep = new Keep;
       
        if(-1==objKeep->ListenInital())
        {
                perror("fKeep Failure Because ListenInital Failue");
               
                exit(-1) ;
        }

        MSG("fKeep ListenInital Sucess");
       
       
        objKeep->Listen(); //Begin Send and Listen Camera UDP

}
  1. int Keep::ListenInital()
  2. {
  3.        
  4.         bzero(&(ServerAddres),sizeof(ServerAddres));

  5.         bzero(&(BroadAddress),sizeof(BroadAddress));

  6.         bzero(&(CameraAddress),sizeof(CameraAddress));

  7.         ServerAddres.sin_family=AF_INET;

  8.         BroadAddress.sin_family=AF_INET;

  9.         if(this->nBig_End  == 1)
  10.         {

  11.                
  12.                 ServerAddres.sin_port = PORT;        

  13.                 ServerAddres.sin_addr.s_addr = inet_addr(LOCALHOST);

  14.                 BroadAddress.sin_port = PORT;
  15.        
  16.                 BroadAddress.sin_addr.s_addr = inet_addr(BROADCAST);
  17.                
  18.                
  19.         }
  20.         else if(this->nBig_End == -1)
  21.         {
  22.             
  23.                 ServerAddres.sin_port = htons(PORT);        

  24.                 ServerAddres.sin_addr.s_addr = inet_addr(LOCALHOST);

  25.                 BroadAddress.sin_port = htons(PORT);
  26.        
  27.                 BroadAddress.sin_addr.s_addr = inet_addr(BROADCAST);
  28.     }


  29.                                
  30.         ListenSocket=socket(AF_INET,SOCK_DGRAM,0);

  31.        
  32.         SendtoSocket=socket(AF_INET,SOCK_DGRAM,0);

  33.        
  34.         if(ERROR== ListenSocket || ERROR== SendtoSocket)
  35.         {
  36.                
  37.                
  38.                 perror("ListenInital Socket Failure:");
  39.                
  40.                
  41.         }

  42.         printf("KeepListen Inital Sucess\n");



  43.         //set send to broadcast of socket at ListenSocket
  44.         if(setsockopt(SendtoSocket,SOL_SOCKET,SO_BROADCAST,&ENABLE,sizeof(ENABLE))< 0)
  45.         {
  46.                
  47.                 perror("ListenInital Setsockopt to SendtoSocket Fialure:");
  48.                
  49.                 //return -1;       
  50.         }
  51.        
  52.             
  53.         if(bind(SendtoSocket,(struct sockaddr*)&(ServerAddres),sizeof(ServerAddres))<0)
  54.         {
  55.                 perror("SendtoScoket bind to LOCALHOST Failure:");
  56.                
  57.                 //return -1;
  58.         }

  59.        
  60.         if(bind(ListenSocket,(struct sockaddr*)&BroadAddress,sizeof(BroadAddress))<0)
  61.         {
  62.                 perror("ListenSocket bind to BROADCAST Failure:");
  63.                
  64.                 //return -1;
  65.         }


  66.         bzero(&(SearchMsg),sizeof(struct UdpSearchMsg));  

  67.         SearchMsg.Protocol[0]='M';
  68.         SearchMsg.Protocol[1]='O';
  69.         SearchMsg.Protocol[2]='_';
  70.         SearchMsg.Protocol[3]='I';


  71.     SearchMsg.Len=4;

  72.     SearchMsg.Keep3=4;

  73.         if(1 == nBig_End)
  74.         {
  75.                 SearchMsg.Content = 1;
  76.         }
  77.         else
  78.         {
  79.                 SearchMsg.Content = htonl(1);  
  80.                
  81.         }
  82.                        
  83.         //SearchMsg.Content=1;

  84.         printf("Keep::ListenInital Sucess\n");


  85.         return 0;
  86.        
  87. }
  88. /*
  89. * thread run and listen UDP
  90. */
  91. void Keep::Listen()
  92. {


  93.                 int nLen;

  94.        
  95.                
  96.                 while(1)
  97.                
  98.                 {

  99.                         sleep(3); //3

  100.                
  101.                         nLen=sendto(SendtoSocket,(char *)&(SearchMsg),sizeof(struct UdpSearchMsg),0,(struct sockaddr*)&BroadAddress,sizeof(BroadAddress));
  102.                
  103.                        
  104.                         if(nLen <=0)
  105.                         {
  106.                                 perror("Sendto Failure:");

  107.                                 //return ;
  108.                         }


  109.                         MSG("Sendto sucess");

  110.                
  111.                         nLen=recvfrom(ListenSocket,&(RecvTmp),sizeof(RecvTmp),0,(struct sockaddr*)&CameraAddress,(socklen_t *)&socklen);


  112.                         MSG("Recvform is sucess");       
  113.                        
  114.                
  115.                         struct UdpAnswerMsg * pAnswerMsg  = (struct UdpAnswerMsg *)&RecvTmp;

  116.                        
  117.                         if(-1 == CheckMessage(pAnswerMsg))
  118.                         {                 
  119.                                
  120.                                 MSG("CheckMessage Failure");

  121.                                 continue ;
  122.                         }                                                                               

  123.                         /*Event*/
  124. //                        App.Event(CameraUpdata,pAnswerMsg,sizeof(struct UdpAnswerMsg));

  125.                        
  126.                 }


  127. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-05-09 10:53 |只看该作者
  1. class Keep
  2. {
  3.      int Start()
  4.      static void* StartThread(void *arg);
  5.      int Threadfun();

  6. }
  7. int Keep::Start()
  8. {
  9.     pthread_create(,,Keep:StartThread,this)
  10. }
  11. void * Keep::StartThread(void * arg)
  12. {
  13.     keep *pKeep =(Keep*)arg;
  14.     pKeep->ThreadFun();
  15. }
  16. int Treadfun()
  17. {
  18. ...
  19. |
复制代码

C++ 在linux下这么启动线程有没有问题?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP