- 论坛徽章:
- 0
|
- class Keep
- {
- public:
- Keep();
- ~Keep();
-
- int big_endian(void);
-
- int SetTime(int t); //Set 频率
- /*通知接口*/
- /*keep Server */
- int ListenServerInital();
- void ListenServer();
- /*keep Camera*/
- int UdpCamera(void * pVoid=NULL);
- static void * KeepListen(void *Arg);
-
- int ListenInital();
- void Listen();
- int CameraInital();
- int CheckMessage(struct UdpAnswerMsg * Msg);
- void PrintfCamera(struct UdpAnswerMsg * Msg);
- /*
- * 模拟IpCamera
- */
- int ModeInital();
-
- void ModeCamera();
- int ModeCheck(struct UdpSearchMsg * Msg);
- void PrintModeDate(struct UdpSearchMsg * Msg);
-
- /*Keep Camera*/
- int ListenSocket;
- int SendtoSocket;
-
- struct sockaddr_in ServerAddres; //localhost
-
- struct sockaddr_in BroadAddress; //broaddress
-
- struct sockaddr_in CameraAddress; //CameraAddress
- struct UdpSearchMsg SearchMsg;
- struct UdpSearchMsg *pSearchMsg;
- struct UdpAnswerMsg AnswerMsg;//复制接收缓存到这
- struct UdpAnswerMsg *pAnswerMsg; //指向接收缓存
- char SendTmp[27]; //Find Camera
- /*对外地址*/
- char RecvTmp[88]; //Declaer Camera
-
- int ENABLE; //
- // setscokopt
- int DISBLE; //
- int state; //
- int time; //频率
- socklen_t socklen; //recvfrom
- /*大小端*/
- int nBig_End;
-
- protected:
- private:
- /*Keep Server*/
-
-
-
- };
复制代码
- #include "CKeep.h"
- //pthread_t ThreadID_CameraAlive;
- //Keep App;
- pthread_t g_thread;
- void* FKeepListen(void *arg)
- {
-
- Keep objKeep;
-
- if(-1==objKeep.ListenInital())
- {
- perror("fKeep Failure Because ListenInital Failue");
-
- exit(-1) ;
- }
- MSG("fKeep ListenInital Sucess");
-
-
- objKeep.Listen(); //Begin Send and Listen Camera UDP
-
- }
- int main()
- {
-
- if(-1==pthread_create(&g_thread,NULL,FKeepListen,NULL))
- {
- perror("pthread run Listen Failue");
- perror("Program exit");
- exit(-1);
- }
- pthread_join(g_thread,NULL);
- }
复制代码 问题:
上面的程序能正常运行,但是
在 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
}- int Keep::ListenInital()
- {
-
- bzero(&(ServerAddres),sizeof(ServerAddres));
- bzero(&(BroadAddress),sizeof(BroadAddress));
- bzero(&(CameraAddress),sizeof(CameraAddress));
- ServerAddres.sin_family=AF_INET;
- BroadAddress.sin_family=AF_INET;
- if(this->nBig_End == 1)
- {
-
- ServerAddres.sin_port = PORT;
- ServerAddres.sin_addr.s_addr = inet_addr(LOCALHOST);
- BroadAddress.sin_port = PORT;
-
- BroadAddress.sin_addr.s_addr = inet_addr(BROADCAST);
-
-
- }
- else if(this->nBig_End == -1)
- {
-
- ServerAddres.sin_port = htons(PORT);
- ServerAddres.sin_addr.s_addr = inet_addr(LOCALHOST);
- BroadAddress.sin_port = htons(PORT);
-
- BroadAddress.sin_addr.s_addr = inet_addr(BROADCAST);
- }
-
- ListenSocket=socket(AF_INET,SOCK_DGRAM,0);
-
- SendtoSocket=socket(AF_INET,SOCK_DGRAM,0);
-
- if(ERROR== ListenSocket || ERROR== SendtoSocket)
- {
-
-
- perror("ListenInital Socket Failure:");
-
-
- }
- printf("KeepListen Inital Sucess\n");
- //set send to broadcast of socket at ListenSocket
- if(setsockopt(SendtoSocket,SOL_SOCKET,SO_BROADCAST,&ENABLE,sizeof(ENABLE))< 0)
- {
-
- perror("ListenInital Setsockopt to SendtoSocket Fialure:");
-
- //return -1;
- }
-
-
- if(bind(SendtoSocket,(struct sockaddr*)&(ServerAddres),sizeof(ServerAddres))<0)
- {
- perror("SendtoScoket bind to LOCALHOST Failure:");
-
- //return -1;
- }
-
- if(bind(ListenSocket,(struct sockaddr*)&BroadAddress,sizeof(BroadAddress))<0)
- {
- perror("ListenSocket bind to BROADCAST Failure:");
-
- //return -1;
- }
- bzero(&(SearchMsg),sizeof(struct UdpSearchMsg));
- SearchMsg.Protocol[0]='M';
- SearchMsg.Protocol[1]='O';
- SearchMsg.Protocol[2]='_';
- SearchMsg.Protocol[3]='I';
- SearchMsg.Len=4;
- SearchMsg.Keep3=4;
- if(1 == nBig_End)
- {
- SearchMsg.Content = 1;
- }
- else
- {
- SearchMsg.Content = htonl(1);
-
- }
-
- //SearchMsg.Content=1;
- printf("Keep::ListenInital Sucess\n");
- return 0;
-
- }
- /*
- * thread run and listen UDP
- */
- void Keep::Listen()
- {
- int nLen;
-
-
- while(1)
-
- {
- sleep(3); //3
-
- nLen=sendto(SendtoSocket,(char *)&(SearchMsg),sizeof(struct UdpSearchMsg),0,(struct sockaddr*)&BroadAddress,sizeof(BroadAddress));
-
-
- if(nLen <=0)
- {
- perror("Sendto Failure:");
- //return ;
- }
- MSG("Sendto sucess");
-
- nLen=recvfrom(ListenSocket,&(RecvTmp),sizeof(RecvTmp),0,(struct sockaddr*)&CameraAddress,(socklen_t *)&socklen);
- MSG("Recvform is sucess");
-
-
- struct UdpAnswerMsg * pAnswerMsg = (struct UdpAnswerMsg *)&RecvTmp;
-
- if(-1 == CheckMessage(pAnswerMsg))
- {
-
- MSG("CheckMessage Failure");
- continue ;
- }
- /*Event*/
- // App.Event(CameraUpdata,pAnswerMsg,sizeof(struct UdpAnswerMsg));
-
- }
- }
复制代码 |
|