Meissi-jian commented on PR #12997:
URL: https://github.com/apache/nuttx/pull/12997#issuecomment-2296337947

   testcase:
   ```
   #define BUFFER_SIZE 1024
   int clientSocket;
   
   void* thread1_handle(void *p)
   {
     clientSocket = socket(AF_INET, SOCK_STREAM, 0);
     if (clientSocket == -1) {
         perror("Failed to create socket");
         exit(EXIT_FAILURE);
     }
   
     struct sockaddr_in serverAddress;
     serverAddress.sin_family = AF_INET;
     serverAddress.sin_port = htons(89);
     serverAddress.sin_addr.s_addr = inet_addr("120.26.1.167");
     
     usleep(100000);
     if (connect(clientSocket, (struct sockaddr *)&serverAddress, 
sizeof(serverAddress)) == -1) {
         perror("Failed to connect to server");
         exit(EXIT_FAILURE);
     }
   
     char *request = "GET /1.mp3 HTTP/1.1\r\nHost: 120.26.1.167:89\r\n\r\n";
     if (send(clientSocket, request, strlen(request), 0) == -1) {
         perror("Failed to send request");
         exit(EXIT_FAILURE);
     }
   
     char buffer[BUFFER_SIZE];
     FILE *file = fopen("/data/1.data", "wb");
     if (file == NULL) {
         perror("Failed to open file");
         exit(EXIT_FAILURE);
     }
   
     int bytesRead;
     while ((bytesRead = recv(clientSocket, buffer, BUFFER_SIZE, 0)) > 0) {
         fwrite(buffer, 1, bytesRead, file);
     }
   
     if (bytesRead == -1) {
         perror("Failed to receive data");
         exit(EXIT_FAILURE);
     }
   
     fclose(file);
     //close(clientSocket);
   
     printf("File downloaded successfully\n");
   
     return 0;
   }
   
   void* thread2_handle(void *arg)
   {
       usleep(200000);  // set the time to wait
       printf("Thread 2 is running\n");
       close(clientSocket);
       pthread_cancel(*(pthread_t*)arg);  // cancel thread1
       pthread_exit(NULL);  
   }
   
   int main(int argc, FAR char *argv[])
   {
     clientSocket = 0;
     int ret = 0;
     pthread_t thread1;
     pthread_t thread2;
     pthread_attr_t attr;
     pthread_attr_init(&attr);
     pthread_attr_setstacksize(&attr, 4096);
   
     pthread_create(&thread1, &attr, thread1_handle, NULL);
     pthread_create(&thread2, &attr, thread2_handle, &thread1);
   
       ret = pthread_join(thread2, NULL);
       if (ret != 0) {
           printf("Failed to join thread2\n");
           return 1;
       }
   
     printf("Thread 2 finished, Thread 1 is canceled\n");
   
     return 0;
   }
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to