Windows Network Programming debugging experience

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://blog.csdn.net/clever101

 

Recently, I want to debug a network program, which is generally a program that receives socket strings. The network is somewhat unstable. In many cases, the program can normally receive data, but occasionally fails. I found that the better method is to write a log file for analysis.

 

What content should be written into the log file? I thought about three aspects:

1. Time when data is received

2. Length of the received data (ensure that the content is correct only when the length is correct)

3. Content of received data

 

The following code retrieves the local time and writes the file:

  1. # Include <stdio. h>
  2. # Include <windows. h>
  3. Int main (INT argc, char * argv [])
  4. {
  5. File * fp = fopen ("log.txt", "A + ");
  6. If (null = FP)
  7. Return 1;
  8. Systemtime now;
  9. Getlocaltime (& now); // obtain the local time
  10. Char sztimebuf [25];
  11. Memset (sztimebuf, '', 25 );
  12. // Format the time
  13. Sprintf (sztimebuf, "/n % 02d/% 02d/% 02d % 02d: % 02d: % 02d", now. wyear, now. wmonth, now. wday, now. whour, now. wminute, now. wsecond );
  14. Fwrite (sztimebuf, FP); // write the file
  15. Fclose (FP );
  16. Return 0;
  17. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.