Posts

Force Internet connection to desired network adapter

Image
PROBLEM: Working in an IT company, you may be familiar with the situation where you have 2 network interfaces connecting to 2 different networks (or even a same network). Sometimes you want traffic going through one specific interface but not through the other. You are not sure and you switch off one interface to avoid the problem. That's okay but it's kind of annoying. SOLUTION: I faced the same problem. I did some research and found  this help. Let's summarize it. Windows decides which interface to handle which traffic based on a routing table. To see it issue command route print on cmd terminal. You will get something like below. Currently, I don't connect my wire cable so there is only one record for destination 0.0.0.0. If there are more than one with 0.0.0.0, then the one with the lowest metric will be used. (0.0.0.0 matches every IP address). Usually, if you want to route internet traffics, you should change the 0.0.0.0 record. For other networks,...

Measure time execution in a C program

There are many ways to measure time in a C program. I use  clock_gettime () since it supports high resolution clock which we can count on. Header #include <time.h> Time storage structure struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; Function int clock_gettime ( clockid_t clk_id, struct timespec *tp); is used to retrieve the time of the specified clock clk_id. There are several kind of clock ID but the two of them may be used the most. CLOCK_REALTIME : System-wide clock that measures real (i.e., wall-clock) time. CLOCK_MONOTONIC : Clock that cannot be set and represents monotonic time since some unspecified starting point. On Linux, that point corresponds to the number of seconds that the system has been running since it was booted. Below is an example of using the function. #include <stdio.h> #include <time.h> struct timespec time_subtract ( struct timespec t...

Python application crashes without throwing any exception

PROBLEM: Recently I have written a flask application and encountered a problem that the application randomly dies. My application is to provide a simple REST API to query some personal information. It serves both HTTP and HTTPS. I run the app by python command, just like below python my_app.py (Yeah, I know that command is for development only and should not be used in production) Everything went well for a couple of tests, til the rainy time, literally, there was a heavy rain outside. I sent a HTTPS request using postman and the above command just stopped. I tried several times, the issue did not happen permanently. Sometimes it crashed, sometimes it went well. There was not any information dumping out to screen. The command was just completed with a lame terminal's prompt. HOW I DEAL WITH IT: I was just a novice in python and was expecting for an exception that would tell me the exact reason why my application had been crashed. Well, there was nothing. I goog...

Viết cho lần đầu leo núi Bà Đen

Image
Còn nhớ lần đầu tiên tôi đi núi Bà Đen. Do nhóm bạn ban đầu rủ rê rơi rụng dần, cuối cùng chỉ còn có bốn mạng, một nữ và ba nam, chúng tôi quyết định tăng độ khó cung đường lên 4, là cung Ma Thiên Lãnh. Ai đã từng tìm hiểu chắc cũng biết được rằng dân du lịch bụi, dân phượt truyền tai nhau 5 cung đường leo núi với 5 độ khó khác nhau. Không biết vì lý do gì cả bọn không hề quan tâm đến độ khó mà chuyển đổi từ "độ khó" sang "độ dễ" và chê các cung đường khác... dễ, không thèm đi. Lịch trình là chúng tôi hẹn gặp ở bến xe An Sương, đi xe tốc hành về Tây Ninh và đến núi Bà Đen, leo núi cung đường Ma Thiên Lãnh. Chúng tôi sẽ cắm trại một đêm trên đỉnh núi và hôm sau sẽ đi bộ xuống đường ống nước. Khâu chuẩn bị hoàn toàn chu đáo, dù "plan gấp" chỉ hai ngày. Chuẩn bị gì thì mời xem tiếp sẽ rõ. Hẹn nhau sáu ba mươi ở bến xe nhưng "kẹt xe Sài Gòn" làm mọi người trễ hẳn một tiếng. Đến đây, tự nhiên nhớ lại vấn nạn giờ dây thun của giới trẻ Việt Nam...