int HttpRequest::ConnectToServer()
{
QString m_sUdpIpAddress
= "127.0.0.1";
int m_nPort = 8000;
#ifndef Q_OS_ANDROID
WSADATA wsa;
SOCKET nSocketID;
qDebug("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
qDebug("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
#else
int nSocketID= -1;
#endif
struct sockaddr_in servaddr;
qDebug("Initialised.\n");
if((nSocketID = socket(AF_INET , SOCK_STREAM , 0 )) < 0)
{
#if defined(Q_OS_ANDROID)
qDebug("Could not create socket : %d" , errno);
#else
qDebug("Could not create socket : %d" , WSAGetLastError());
#endif
}
qDebug("Socket created.\n");
servaddr.sin_addr.s_addr = inet_addr(IPSTR);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
//Connect to remote server
if (::connect(nSocketID,(struct sockaddr *)&servaddr , sizeof(servaddr)) < 0)
{
qDebug("create %s connect failed!\n", IPSTR );
#if defined(Q_OS_ANDROID)
::close(nSocketID);
#else
closesocket(nSocketID);
#endif
return -2;
}
qDebug("Connected to Server\n");
char sendStr[HTTP_SEND_STR_LEN];
memset(sendStr, 0, HTTP_SEND_STR_LEN );
QString m_sGetUrl
= "GET /bcid="+QString::number(m_sBCID
)+ " HTTP/1.0 Host:"+IPSTR
+ " Connection: Keep-Alive";
qDebug() << "m_sGetUrl:"<< m_sGetUrl;
int nRet = send(nSocketID,m_sGetUrl.toLatin1(),strlen(m_sGetUrl.toLatin1()) , 0);
if( nRet < 0)
{
qDebug() << "send failed %s!\n"<< m_sGetUrl ;
#if defined(Q_OS_ANDROID)
::close(nSocketID);
#else
closesocket(nSocketID );
#endif
return -4;
}
qDebug("Data Send\n");
unsigned char pBuffIn[MAX_RECV_BUF_LEN*5] = {0};
if((ofp_serv[m_nBcidIndex] = fopen("test_video.ts","wb")) == NULL)
{
qDebug("Output File couldnot be opened\n");
return -5;
}
// init
//this should be called everytime when the channel started
Init(ServiceCtrlHdr,SrLyrOut,m_nBcidIndex);
int nOffset = 0;
FILE *pInFile = fopen("input.bin","wb");
if(pInFile == NULL)
{
qDebug() << "Error in Opening Input File" ;
}
bool bIsGetOk = false;
char tmp[3]="\r\n";
char httpHeader[100] = {'0'};
char index[101];
while ((!bIsGetOk) && (m_bStop == false))
{
nRet = recv(nSocketID, (char *)pBuffIn, (sizeof(pBuffIn)), 0) ;
if( !bIsGetOk )
{
char *p = strstr( (char *)pBuffIn, tmp );
if( p )
{
memset( httpHeader, 0, 100 );
memcpy( httpHeader,(char *) pBuffIn, (p-(char *)pBuffIn) );
char Space[] = {' '};
strncat ( index, httpHeader, 1 ) ;
char *tmp =index;
if( strcmp(httpHeader,"HTTP/") && strcmp(httpHeader, "OK") )
{
bIsGetOk = true;
}
else
{
printf("Get failed: %s !\n", &tmp[1] );
#if defined(Q_OS_ANDROID)
::close(nSocketID);
#else
closesocket(nSocketID);
#endif
m_bStop = true;
bIsGetOk = true;
return -5;
}
}
}
}
while (!m_bStop)
{
nRet = recv(nSocketID, (char *)&pBuffIn[nOffset], (sizeof(pBuffIn) - nOffset), 0) ;
nOffset += nRet;
if (nRet > 0 && nOffset >= MAX_RECV_BUF_LEN)
{
qDebug("Bytes received length: %d\n", nOffset);
int nCounter = (nOffset/MAX_RECV_BUF_LEN);
for(int i = 0; i < nCounter; i++)
{
if(pInFile != NULL)
{
fwrite(pBuffIn,1,MAX_RECV_BUF_LEN,pInFile);
}
ServiceLayer(&ServiceCtrlHdr[m_nBcidIndex], pBuffIn,&SrLyrOut[m_nBcidIndex],m_nBcidIndex);
int nBytes = (SrLyrOut[m_nBcidIndex].pAudioInputWriteBuff - SrLyrOut[m_nBcidIndex].pAudioInputReadBuff);
if(nBytes)
{
qDebug() << "Bytes Written:" << nBytes;
fwrite(SrLyrOut[m_nBcidIndex].pAudioInputReadBuff,sizeof(char),nBytes,ofp_serv[m_nBcidIndex]);
qint64 nWrittenBytes = 0;
nWrittenBytes
= m_nUdpSocket
->writeDatagram
((char*)SrLyrOut
[m_nBcidIndex
].
pAudioInputReadBuff,nBytes,
QHostAddress(m_sUdpIpAddress
),m_nPort
);
qDebug() << "nWrittenBytes:" << nWrittenBytes;
SrLyrOut[m_nBcidIndex].pAudioInputWriteBuff = (unsigned char *)SrLyrOut[m_nBcidIndex].mp3AACBuffer;
}
int nRem = (nOffset - MAX_RECV_BUF_LEN);
nOffset = 0;
if(nRem > 0)
{
memcpy((char *)&pBuffIn[nOffset],(char *)&pBuffIn[MAX_RECV_BUF_LEN],nRem);
nOffset += nRem;
}
}
qDebug() << "After loop nOffset:" << nOffset;
}
else if( nRet < 0)
{
#if defined(Q_OS_ANDROID)
qDebug("Could not create socket : %d" , errno);
#else
qDebug("Could not create socket : %d" , WSAGetLastError());
#endif
m_bStop = true;
}
}
if(ofp_serv[m_nBcidIndex] != NULL)
{
fclose(ofp_serv[m_nBcidIndex]);
}
}
int HttpRequest::ConnectToServer()
{
QString m_sUdpIpAddress = "127.0.0.1";
int m_nPort = 8000;
#ifndef Q_OS_ANDROID
WSADATA wsa;
SOCKET nSocketID;
qDebug("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
qDebug("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
#else
int nSocketID= -1;
#endif
struct sockaddr_in servaddr;
qDebug("Initialised.\n");
if((nSocketID = socket(AF_INET , SOCK_STREAM , 0 )) < 0)
{
#if defined(Q_OS_ANDROID)
qDebug("Could not create socket : %d" , errno);
#else
qDebug("Could not create socket : %d" , WSAGetLastError());
#endif
}
qDebug("Socket created.\n");
servaddr.sin_addr.s_addr = inet_addr(IPSTR);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
//Connect to remote server
if (::connect(nSocketID,(struct sockaddr *)&servaddr , sizeof(servaddr)) < 0)
{
qDebug("create %s connect failed!\n", IPSTR );
#if defined(Q_OS_ANDROID)
::close(nSocketID);
#else
closesocket(nSocketID);
#endif
return -2;
}
qDebug("Connected to Server\n");
char sendStr[HTTP_SEND_STR_LEN];
memset(sendStr, 0, HTTP_SEND_STR_LEN );
QString m_sGetUrl = "GET /bcid="+QString::number(m_sBCID)+ " HTTP/1.0 Host:"+IPSTR+ " Connection: Keep-Alive";
qDebug() << "m_sGetUrl:"<< m_sGetUrl;
int nRet = send(nSocketID,m_sGetUrl.toLatin1(),strlen(m_sGetUrl.toLatin1()) , 0);
if( nRet < 0)
{
qDebug() << "send failed %s!\n"<< m_sGetUrl ;
#if defined(Q_OS_ANDROID)
::close(nSocketID);
#else
closesocket(nSocketID );
#endif
return -4;
}
qDebug("Data Send\n");
unsigned char pBuffIn[MAX_RECV_BUF_LEN*5] = {0};
if((ofp_serv[m_nBcidIndex] = fopen("test_video.ts","wb")) == NULL)
{
qDebug("Output File couldnot be opened\n");
return -5;
}
// init
//this should be called everytime when the channel started
Init(ServiceCtrlHdr,SrLyrOut,m_nBcidIndex);
int nOffset = 0;
FILE *pInFile = fopen("input.bin","wb");
if(pInFile == NULL)
{
qDebug() << "Error in Opening Input File" ;
}
bool bIsGetOk = false;
char tmp[3]="\r\n";
char httpHeader[100] = {'0'};
char index[101];
while ((!bIsGetOk) && (m_bStop == false))
{
nRet = recv(nSocketID, (char *)pBuffIn, (sizeof(pBuffIn)), 0) ;
if( !bIsGetOk )
{
char *p = strstr( (char *)pBuffIn, tmp );
if( p )
{
memset( httpHeader, 0, 100 );
memcpy( httpHeader,(char *) pBuffIn, (p-(char *)pBuffIn) );
char Space[] = {' '};
strncat ( index, httpHeader, 1 ) ;
char *tmp =index;
if( strcmp(httpHeader,"HTTP/") && strcmp(httpHeader, "OK") )
{
bIsGetOk = true;
}
else
{
printf("Get failed: %s !\n", &tmp[1] );
#if defined(Q_OS_ANDROID)
::close(nSocketID);
#else
closesocket(nSocketID);
#endif
m_bStop = true;
bIsGetOk = true;
return -5;
}
}
}
}
while (!m_bStop)
{
nRet = recv(nSocketID, (char *)&pBuffIn[nOffset], (sizeof(pBuffIn) - nOffset), 0) ;
nOffset += nRet;
if (nRet > 0 && nOffset >= MAX_RECV_BUF_LEN)
{
qDebug("Bytes received length: %d\n", nOffset);
int nCounter = (nOffset/MAX_RECV_BUF_LEN);
for(int i = 0; i < nCounter; i++)
{
if(pInFile != NULL)
{
fwrite(pBuffIn,1,MAX_RECV_BUF_LEN,pInFile);
}
ServiceLayer(&ServiceCtrlHdr[m_nBcidIndex], pBuffIn,&SrLyrOut[m_nBcidIndex],m_nBcidIndex);
int nBytes = (SrLyrOut[m_nBcidIndex].pAudioInputWriteBuff - SrLyrOut[m_nBcidIndex].pAudioInputReadBuff);
if(nBytes)
{
qDebug() << "Bytes Written:" << nBytes;
fwrite(SrLyrOut[m_nBcidIndex].pAudioInputReadBuff,sizeof(char),nBytes,ofp_serv[m_nBcidIndex]);
qint64 nWrittenBytes = 0;
nWrittenBytes = m_nUdpSocket->writeDatagram((char*)SrLyrOut[m_nBcidIndex].pAudioInputReadBuff,nBytes,QHostAddress(m_sUdpIpAddress),m_nPort);
qDebug() << "nWrittenBytes:" << nWrittenBytes;
SrLyrOut[m_nBcidIndex].pAudioInputWriteBuff = (unsigned char *)SrLyrOut[m_nBcidIndex].mp3AACBuffer;
}
int nRem = (nOffset - MAX_RECV_BUF_LEN);
nOffset = 0;
if(nRem > 0)
{
memcpy((char *)&pBuffIn[nOffset],(char *)&pBuffIn[MAX_RECV_BUF_LEN],nRem);
nOffset += nRem;
}
}
qDebug() << "After loop nOffset:" << nOffset;
}
else if( nRet < 0)
{
#if defined(Q_OS_ANDROID)
qDebug("Could not create socket : %d" , errno);
#else
qDebug("Could not create socket : %d" , WSAGetLastError());
#endif
m_bStop = true;
}
}
if(ofp_serv[m_nBcidIndex] != NULL)
{
fclose(ofp_serv[m_nBcidIndex]);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks