Advertisement
Guest User

fprintf/fscanf bug code

a guest
Mar 16th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1.  
  2. #include <cyg/infra/diag.h>
  3. #include <cyg/hal/hal_diag.h>
  4. #include <cyg/hal/at91sam9_common_reg.h>
  5. #include <cyg/hal/hal_io.h>
  6. #include <cyg/io/spi_at91.h>
  7. #include <cyg/hal/hal_arch.h>
  8. #include <cyg/kernel/kapi.h>
  9. #include <cyg/hal/hal_cache.h>
  10. #include <cyg/hal/at91sam9_io.h>
  11. #include <assert.h>
  12. #include <math.h>
  13.  
  14.        
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <netdb.h>
  23.  
  24.  
  25. #define STACKSIZE    (CYGNUM_HAL_STACK_SIZE_MINIMUM + 32*1024)
  26.  
  27. static unsigned char cs_test_stack[STACKSIZE];
  28. static cyg_handle_t cs_test_handle;
  29. static cyg_thread   cs_test_thread;
  30.  
  31.  
  32. static void cs_test_thread_func(cyg_addrword_t data);
  33.  
  34.  
  35. void start_cs_test()
  36. {
  37.   cyg_thread_create(12, &cs_test_thread_func, 0, "CS Test Thread",
  38.                     cs_test_stack, STACKSIZE,
  39.                     &cs_test_handle, &cs_test_thread);
  40.   cyg_thread_resume(cs_test_handle);
  41.  
  42.  
  43. }
  44.  
  45. static void
  46. cs_test_thread_func(cyg_addrword_t data)
  47. {
  48.   int servSock, clntSock;
  49.   struct sockaddr_in serv_addr;
  50.   FILE *term;
  51.   int num;
  52.  
  53.   init_all_network_interfaces();
  54.   servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  55.   assert(servSock > 0);
  56.  
  57.   bzero((char *) &serv_addr, sizeof(serv_addr));
  58.  
  59.   serv_addr.sin_family = AF_INET;
  60.   serv_addr.sin_addr.s_addr=htonl(INADDR_ANY); //listen on every interface
  61.   serv_addr.sin_port = htons(23); //we listen on port 23
  62.  
  63.   /* Bind to the local address */
  64.   if (bind(servSock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  65.     {
  66.       diag_printf("Could not bind to local adress\n");
  67.       assert(false);
  68.     }
  69.  
  70.   if (listen(servSock, 1) < 0)
  71.     {
  72.         diag_printf("listen() failed");
  73.     assert(false);
  74.     }
  75.   while(1)
  76.     {
  77.  
  78.      /* Wait for a client to connect */
  79.       if ((clntSock = accept(servSock, 0,0)) < 0)
  80.     {
  81.     diag_printf("accept() failed");
  82.     assert(false);
  83.     }
  84.  
  85.       term=fdopen(clntSock, "r+");
  86.       assert(term!=0);
  87.  
  88.       while(1)
  89.     {
  90.       fprintf(term, "Type a number\n");
  91.       diag_printf("fscanf %d\n", fscanf(term, "%d",&num));
  92.       diag_printf("fprintf1 %d\n",fprintf(term, "Number was %d\n",num));
  93.       diag_printf("fprintf2 %d\n",fprintf(term, "Again - Number was %d\n",num));
  94.  
  95.     }
  96.     }
  97.    
  98.  
  99.  
  100. }
  101.  
  102.  
  103. void cyg_user_start(void);
  104. void cyg_user_start(void)
  105. {
  106.   start_cs_test();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement