相同的代码不起作用,请告诉错误 [英] same code doesn't work,please tell the error
本文介绍了相同的代码不起作用,请告诉错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将代码no1放入中断的应用程序中时,将两个代码设为1号代码可以正常工作,你能告诉我错误吗?我已经在代码2中提到了在返回0"处中断的代码. >
这是两个代码:-
made two code the code no 1 works fine when i put the code no1 in an application it breaks,can u tell me the error.I have mentioned in code no 2 where it breaks at "return 0".
Here are two code:-
ServerMediaSession* sms[2];
UsageEnvironment* env;
Boolean reuseFirstSource = False;
Boolean iFramesOnly = False;
TaskScheduler* scheduler;
UserAuthenticationDatabase* authDB;
char const* streamName[2];
char const* inputAddressStr[2];
int inputPortNum[2];
int i = 0,j = 0;
DWORD WINAPI START_STREAM();
char const* descriptionString[2] ={"Session streamed by \"testOnDemandRTSPServer1\"","Session streamed by \"testOnDemandRTSPServer2\""};
RTSPServer* rtspServer;
char* url[2];
HANDLE Th1;
DWORD Tid1=NULL;
int main()
{
streamName[0] = "test";
inputAddressStr[0] = "239.255.42.42";
inputPortNum[0] = 4444;
streamName[1] = "stream2";
inputAddressStr[1] = "237.255.42.42";
inputPortNum[1] = 5043;
scheduler = BasicTaskScheduler::createNew();
env = BasicUsageEnvironment::createNew(*scheduler);
UserAuthenticationDatabase* authDB = NULL;
rtspServer = RTSPServer::createNew(*env,554, authDB);
while(1)
{
printf("\nEnter any key to STRAT_STREAMER\n");
getch();
Th1=CreateThread(NULL,0(LPTHREAD_START_ROUTINE)START_STREAM,NULL,0,&Tid1);
if(Th1 == NULL)
*env<< "\nTh1 failed..\n";
}
return 0;
}
DWORD WINAPI START_STREAM()
{
Boolean const inputStreamIsRawUDP = False;
printf("\nIn Start Stream\n");
sms[i] = ServerMediaSession::createNew(*env, streamName[i], streamName[i],descriptionString[i]);
sms[i]->addSubsession(MPEG2TransportUDPServerMediaSubsession::createNew(*env,inputAddressStr[i],inputPortNum[i],inputStreamIsRawUDP));
rtspServer->addServerMediaSession(sms[i]);
url[i] = rtspServer->rtspURL(sms[i]);
printf("\n\"%s \" stream, from a UDP Transport Stream input source\n\t(",streamName[i]);
//*env[i] << "\n\"" << streamName[i] << "\" stream, from a UDP Transport Stream input source \n\t(";
if (inputAddressStr[i] != NULL)
{
printf("\nIP multicast address %s,",inputAddressStr[i]);
//*env[i] << "IP multicast address " << inputAddressStr[i] << ",";
}
else
{
printf("unicast;");
//*env[i] << "unicast;";
}
printf("port %d \n",inputPortNum[i]);
//*env[i] << " port " << inputPortNum[i] << ")\n";
printf("Play this stream using the URL \" %s \"",url[i]);
//*env[i] << "Play this stream using the URL \"" << url[i] << "\"\n";
delete[] url[i];
if(j == 0)
{
if (rtspServer->setUpTunnelingOverHTTP(http_port[i]) || rtspServer->setUpTunnelingOverHTTP(http_port[i]) || rtspServer->setUpTunnelingOverHTTP(http_port[i]))
{
printf("\n\n\n(We use port %d for optional RTSP-over-HTTP tunneling.)\n",rtspServer->httpServerPortNum());
}
else
{
printf("\n(RTSP-over-HTTP tunneling is not available.)");
}
j++;
}
else
printf("\n\n\n(We use port %d for optional RTSP-over HTTPtunneling.)\n",rtspServer->httpServerPortNum());
i++;
if(i == 1)
env->taskScheduler().doEventLoop(); // does not return
return 0;
}
这是我的代码2:-
Here is mine code no 2:-
ServerMediaSession* sms[50];
UsageEnvironment* env;
Boolean reuseFirstSource = False;
Boolean iFramesOnly = False;
TaskScheduler* scheduler;
UserAuthenticationDatabase* authDB;
char const* descriptionString ="Session streamed by \"testOnDemandRTSPServer1\"";
RTSPServer* rtspServer;
char* url[50]
static int k=0,j=0,z=0,w=0;
char const* descriptionString ="Session streamed by \"testOnDemandRTSPServer1\"";
RTSPServer* rtspServer;
char* url[50];
char const* streamName[2];
char const* inputAddressStr[2];
int inputPortNum[2];
int main()
{
StartRtspServer();
b.OnRun();
}
BOOL CFIServerDlg::OnRun()
{
OnStartFIServer();
return TRUE; // ret
}
int StartRtspServer()
{
char buffer[2500];
wLog->WriteDebugLog("Entering in StartRtspServer().");
// Begin by setting up our usage environment:
scheduler = BasicTaskScheduler::createNew();
env = BasicUsageEnvironment::createNew(*scheduler);
UserAuthenticationDatabase* authDB = NULL;
portNumBits rtspServerPortNum = 554;
rtspServer = RTSPServer::createNew(*env,rtspServerPortNum, authDB);
if (rtspServer == NULL)
{
sprintf(buffer,"Failed to create RTSP server with Port:%d %s",rtspServerPortNum,env->getResultMsg());
wLog->WriteErrorLog(buffer);
*env << "Failed to create RTSP server: " <<env->getResultMsg()<<"\n";
return 1;
}
else
{
wLog->WriteInfoLog("Created RTSP server..");
*env << "Created RTSP server.."<<"\n.";
return 0;
}
void CFIServerDlg::OnStartFIServer()
{
wLog->WriteDebugLog("Entering in OnStartFIServer()");
int success = m_FINetCom->StartFIServer();
if(success == 0)
{
m_FINetCom->StartFICom();
}
}
DWORD CFINetCom::StartFIServer()
{
//AL VARIABLES ARE DEFINED NO ISSUE REGARDING VARIABLES
m_ThreadHandle[2] = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)MessageHandleThread, this, 0, &ThreadId3);
if(m_ThreadHandle[2] == NULL)
{
wLog->WriteErrorLog("StartFIServer() Error while creating MessageHandleThread thread.");
cout<<"Debug: StartFIServer() Error while creating MessageHandleThread thread.\n";
}
}
*************************************************************************
*
* NAME : MessageHandleThread()
*
* DESCRIPTION : The message handle thread for handling various messages comming from
client and streamer.
*
* PARAMETERS : CFINetCom*
*
* RETURN : DWORD
*
**************************************************************************/
DWORD WINAPI CFINetCom::MessageHandleThread(CFINetCom* MyServ)
{
case START_STREAM:
{
HANDLE th1;
DWORD tid=NULL;
th1 = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)StartStreamingThread,&ip_port, 0, &tid);
if(th1 == NULL)
{
wLog->WriteDebugLog("StartFIServer() Error while creating MessageReplyThread .");
cout<<"Debug: StartFIServer() Error while creating MessageReplyThread .\n";
}
break;
}
DWORD WINAPI StartStreamingThread(Parameters ip_port)
{
streamName[0] = "test";
inputAddressStr[0] = "239.255.42.42";
inputPortNum[0] = 4444;
streamName[1] = "stream2";
inputAddressStr[1] = "237.255.42.42";
inputPortNum[1] = 5043;
Boolean const inputStreamIsRawUDP = False;
sms[w] = ServerMediaSession::createNew(*env, streamName[w], streamName[w],descriptionString);
sms[w]->addSubsession(MPEG2TransportUDPServerMediaSubsession::createNew(*env, inputAddressStr[w], inputPortNum[w], inputStreamIsRawUDP));
rtspServer->addServerMediaSession(sms[w]);
url[w] = rtspServer->rtspURL(sms[w]);
if (inputAddressStr[w] != NULL)
{
sprintf(buffer,"IP multicast address %s",inputAddressStr[w]);
wLog->WriteDebugLog(buffer);
*env << "IP multicast address " << inputAddressStr[w] << ",";
}
else
{
//printf("\nunicast;\n");
wLog->WriteDebugLog("unicast;");
*env << "unicast;";
}
delete[] url[w];
if(j==0)
{
if (rtspServer->setUpTunnelingOverHTTP(TunnelingPort) || rtspServer->setUpTunnelingOverHTTP(TunnelingPort) || rtspServer->setUpTunnelingOverHTTP(TunnelingPort))
{
sprintf(buffer,"(We use port %d for optional RTSP-over-HTTP tunneling.)", rtspServer->httpServerPortNum());
wLog->WriteDebugLog(buffer);
*env << "\n(We use port " << rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling.)\n";
}
else
{
wLog->WriteDebugLog("(RTSP-over-HTTP tunneling is not available.)");
*env << "\n(RTSP-over-HTTP tunneling is not available.)\n";
}
j++;
}
else
{
printf("(We use port %d for optional RTSP-over-HTTP tunneling.)", rtspServer->httpServerPortNum());
sprintf(buffer,"(We use port %d for optional RTSP-over-HTTP tunneling.)", rtspServer->httpServerPortNum());
wLog->WriteDebugLog(buffer);
}
w++;
wLog->WriteDebugLog("Leaving StartStreamingThread");
if(w == 1)
env->taskScheduler().doEventLoop(); // does not return
return 0; // only to prevent compiler warning,IT BREAKS HERE
}
推荐答案
您的代码有多个问题....
1)在主函数中没有代码会等待您生成的线程.
main函数代表程序的主线程.如果退出,则所有线程将自动终止.
在代码1中,您通过在while(1)中保持主线程繁忙来进行管理.
但是对于代码2,没有这样的规定.您的主线程不会等待生成的线程完成.
2)您没有从 StartFIServer()函数返回任何内容
因此,后续调用中的成功将是一个垃圾值
There are multiple problems with your code ....
1 ) There is no code which will wait for your spawned threads in main function.
main function represents main thread of program. If it exits , all threads will be terminated automatically.
In your Code 1 ,you have managed it by keeping main thread busy in while(1).
But for code 2 there is no such provision. Your main thread will not wait for your spawned threads to finish.
2 ) Your are not returning anything from StartFIServer() function
Hence success in following call would be a garbage value
int success = m_FINetCom->StartFIServer();
这篇关于相同的代码不起作用,请告诉错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文