Results 1 to 3 of 3

Thread: check avaliable data

  1. #1
    Join Date
    Sep 2014
    Posts
    94
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default check avaliable data

    i am writing udp data on port 8000. i want to check the data is avaliable or not how can i check......


    Qt Code:
    1. int HttpRequest::ConnectToServer()
    2. {
    3. QString m_sUdpIpAddress = "127.0.0.1";
    4.  
    5. int m_nPort = 8000;
    6.  
    7. #ifndef Q_OS_ANDROID
    8. WSADATA wsa;
    9. SOCKET nSocketID;
    10. qDebug("\nInitialising Winsock...");
    11. if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    12. {
    13. qDebug("Failed. Error Code : %d",WSAGetLastError());
    14. return 1;
    15. }
    16. #else
    17. int nSocketID= -1;
    18. #endif
    19.  
    20. struct sockaddr_in servaddr;
    21.  
    22. qDebug("Initialised.\n");
    23.  
    24. if((nSocketID = socket(AF_INET , SOCK_STREAM , 0 )) < 0)
    25. {
    26. #if defined(Q_OS_ANDROID)
    27. qDebug("Could not create socket : %d" , errno);
    28. #else
    29. qDebug("Could not create socket : %d" , WSAGetLastError());
    30. #endif
    31. }
    32.  
    33. qDebug("Socket created.\n");
    34.  
    35. servaddr.sin_addr.s_addr = inet_addr(IPSTR);
    36. servaddr.sin_family = AF_INET;
    37. servaddr.sin_port = htons(PORT);
    38.  
    39. //Connect to remote server
    40. if (::connect(nSocketID,(struct sockaddr *)&servaddr , sizeof(servaddr)) < 0)
    41. {
    42. qDebug("create %s connect failed!\n", IPSTR );
    43. #if defined(Q_OS_ANDROID)
    44. ::close(nSocketID);
    45. #else
    46. closesocket(nSocketID);
    47. #endif
    48. return -2;
    49. }
    50.  
    51. qDebug("Connected to Server\n");
    52.  
    53.  
    54. char sendStr[HTTP_SEND_STR_LEN];
    55. memset(sendStr, 0, HTTP_SEND_STR_LEN );
    56.  
    57. QString m_sGetUrl = "GET /bcid="+QString::number(m_sBCID)+ " HTTP/1.0 Host:"+IPSTR+ " Connection: Keep-Alive";
    58. qDebug() << "m_sGetUrl:"<< m_sGetUrl;
    59.  
    60. int nRet = send(nSocketID,m_sGetUrl.toLatin1(),strlen(m_sGetUrl.toLatin1()) , 0);
    61. if( nRet < 0)
    62. {
    63. qDebug() << "send failed %s!\n"<< m_sGetUrl ;
    64. #if defined(Q_OS_ANDROID)
    65. ::close(nSocketID);
    66. #else
    67. closesocket(nSocketID );
    68. #endif
    69. return -4;
    70. }
    71. qDebug("Data Send\n");
    72.  
    73. unsigned char pBuffIn[MAX_RECV_BUF_LEN*5] = {0};
    74.  
    75. if((ofp_serv[m_nBcidIndex] = fopen("test_video.ts","wb")) == NULL)
    76. {
    77. qDebug("Output File couldnot be opened\n");
    78. return -5;
    79. }
    80.  
    81. // init
    82. //this should be called everytime when the channel started
    83. Init(ServiceCtrlHdr,SrLyrOut,m_nBcidIndex);
    84. int nOffset = 0;
    85.  
    86. FILE *pInFile = fopen("input.bin","wb");
    87. if(pInFile == NULL)
    88. {
    89. qDebug() << "Error in Opening Input File" ;
    90. }
    91. bool bIsGetOk = false;
    92. char tmp[3]="\r\n";
    93. char httpHeader[100] = {'0'};
    94. char index[101];
    95. while ((!bIsGetOk) && (m_bStop == false))
    96. {
    97. nRet = recv(nSocketID, (char *)pBuffIn, (sizeof(pBuffIn)), 0) ;
    98. if( !bIsGetOk )
    99. {
    100. char *p = strstr( (char *)pBuffIn, tmp );
    101. if( p )
    102. {
    103. memset( httpHeader, 0, 100 );
    104. memcpy( httpHeader,(char *) pBuffIn, (p-(char *)pBuffIn) );
    105. char Space[] = {' '};
    106. strncat ( index, httpHeader, 1 ) ;
    107. char *tmp =index;
    108. if( strcmp(httpHeader,"HTTP/") && strcmp(httpHeader, "OK") )
    109. {
    110. bIsGetOk = true;
    111. }
    112. else
    113. {
    114. printf("Get failed: %s !\n", &tmp[1] );
    115. #if defined(Q_OS_ANDROID)
    116. ::close(nSocketID);
    117. #else
    118. closesocket(nSocketID);
    119. #endif
    120. m_bStop = true;
    121. bIsGetOk = true;
    122. return -5;
    123. }
    124. }
    125. }
    126. }
    127. while (!m_bStop)
    128. {
    129. nRet = recv(nSocketID, (char *)&pBuffIn[nOffset], (sizeof(pBuffIn) - nOffset), 0) ;
    130.  
    131. nOffset += nRet;
    132.  
    133. if (nRet > 0 && nOffset >= MAX_RECV_BUF_LEN)
    134. {
    135. qDebug("Bytes received length: %d\n", nOffset);
    136. int nCounter = (nOffset/MAX_RECV_BUF_LEN);
    137. for(int i = 0; i < nCounter; i++)
    138. {
    139. if(pInFile != NULL)
    140. {
    141. fwrite(pBuffIn,1,MAX_RECV_BUF_LEN,pInFile);
    142. }
    143. ServiceLayer(&ServiceCtrlHdr[m_nBcidIndex], pBuffIn,&SrLyrOut[m_nBcidIndex],m_nBcidIndex);
    144.  
    145. int nBytes = (SrLyrOut[m_nBcidIndex].pAudioInputWriteBuff - SrLyrOut[m_nBcidIndex].pAudioInputReadBuff);
    146.  
    147. if(nBytes)
    148. {
    149. qDebug() << "Bytes Written:" << nBytes;
    150. fwrite(SrLyrOut[m_nBcidIndex].pAudioInputReadBuff,sizeof(char),nBytes,ofp_serv[m_nBcidIndex]);
    151. qint64 nWrittenBytes = 0;
    152. nWrittenBytes = m_nUdpSocket->writeDatagram((char*)SrLyrOut[m_nBcidIndex].pAudioInputReadBuff,nBytes,QHostAddress(m_sUdpIpAddress),m_nPort);
    153. qDebug() << "nWrittenBytes:" << nWrittenBytes;
    154. SrLyrOut[m_nBcidIndex].pAudioInputWriteBuff = (unsigned char *)SrLyrOut[m_nBcidIndex].mp3AACBuffer;
    155. }
    156. int nRem = (nOffset - MAX_RECV_BUF_LEN);
    157. nOffset = 0;
    158. if(nRem > 0)
    159. {
    160. memcpy((char *)&pBuffIn[nOffset],(char *)&pBuffIn[MAX_RECV_BUF_LEN],nRem);
    161. nOffset += nRem;
    162. }
    163. }
    164. qDebug() << "After loop nOffset:" << nOffset;
    165. }
    166. else if( nRet < 0)
    167. {
    168. #if defined(Q_OS_ANDROID)
    169. qDebug("Could not create socket : %d" , errno);
    170. #else
    171. qDebug("Could not create socket : %d" , WSAGetLastError());
    172. #endif
    173. m_bStop = true;
    174. }
    175. }
    176. if(ofp_serv[m_nBcidIndex] != NULL)
    177. {
    178. fclose(ofp_serv[m_nBcidIndex]);
    179. }
    180. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 8th November 2016 at 18:26. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2014
    Posts
    23
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: check avaliable data

    Please use appropriate tags for formatting.
    Your code isn't readable in it's current shape.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: check avaliable data

    Quote Originally Posted by Killian View Post
    Please use appropriate tags for formatting.
    Your code isn't readable in it's current shape.
    Indeed, had to fix this once more.
    One would assume a user with 80 postings would know how to properly post code by now.

    Also moving to a more appropriate forum, that is not even using Qt networking classes

    Cheers,
    _

Similar Threads

  1. Can we use md5 or sha to check the dll
    By Awareness in forum Installation and Deployment
    Replies: 2
    Last Post: 17th August 2013, 04:31
  2. Replies: 12
    Last Post: 24th July 2012, 09:19
  3. best way to check for data type
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2012, 10:40
  4. How to Check If an URL is Available
    By Furkan in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2010, 18:25
  5. Plz check it
    By evgenM in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2007, 09:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.