2009年12月25日 星期五

socket keep alive time

You might know that,while setting up a TCP connection you can enable SO_KEEPALIVE, forcing the system to check on the connection routinely to see if the connection is still there, not broken by the any connection issues or so.
Yet, have you ever wondered, how often does the system go through this connection check?
This is how we change "keep alive time" for individual socket on linux systems, otherwise the system checks for the connection every 30 mins or so, by default, which is usually too long.
Notice: this is not a standard socket operation, which may not be used on other operating systems. (for example: windows)
Code:
int keepAlive = 1;

// set timeout params for SO_KEEPALIVE
keepTime = 5; //the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe
keepProbe = 1; //number of retry
keepInvl = 1; //the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
if( setsockopt(cliSocket, SOL_TCP, TCP_KEEPIDLE, &keepTime, sizeof(keepTime)) == -1
||setsockopt(cliSocket, SOL_TCP, TCP_KEEPCNT, &keepProbe, sizeof(keepProbe)) == -1
||setsockopt(cliSocket, SOL_TCP, TCP_KEEPINTVL, &keepInvl, sizeof(keepInvl)) == -1
||setsockopt(cliSocket,SOL_SOCKET,SO_KEEPALIVE, &keepAlive,sizeof(keepAlive)) == -1)
{
printf("Socket %d setKeepAlive timeout failed.\n", cliSocket);
close(cliSocket);
}

2009年12月24日 星期四

enalbing connect() to timeout

This is a very useful technique that you should try out while you are starting up a tcp connection.
Instead of "breaking out on error" or being "blocked" at the point of "connect() " when the remote server is temporary unavalible, this function will try to connect in a given period of time.
Code:
void connect_w_to(void) {
int res;
struct sockaddr_in addr;
long arg;
fd_set myset;
struct timeval tv;
int valopt;
socklen_t lon;

// Create socket
soc = socket(AF_INET, SOCK_STREAM, 0);
if (soc < 0) {
fprintf(stderr, "Error creating socket (%d %s)\n", errno, strerror(errno));
exit(0);
}

addr.sin_family = AF_INET;
addr.sin_port = htons(2000);
addr.sin_addr.s_addr = inet_addr("192.168.0.1");

// Set non-blocking
if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) {
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
exit(0);
}
arg |= O_NONBLOCK;
if( fcntl(soc, F_SETFL, arg) < 0) {
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
exit(0);
}
// Trying to connect with timeout
res = connect(soc, (struct sockaddr *)&addr, sizeof(addr));
if (res < 0) {
if (errno == EINPROGRESS) {
fprintf(stderr, "EINPROGRESS in connect() - selecting\n");
do {
tv.tv_sec = 15;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(soc, &myset);
res = select(soc+1, NULL, &myset, NULL, &tv);
if (res < 0 && errno != EINTR) {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
exit(0);
}
else if (res > 0) {
// Socket selected for write
lon = sizeof(int);
if (getsockopt(soc, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) {
fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno));
exit(0);
}
// Check the value returned...
if (valopt) {
fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt)
);
exit(0);
}
break;
}
else {
fprintf(stderr, "Timeout in select() - Cancelling!\n");
exit(0);
}
} while (1);
}
else {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
exit(0);
}
}
// Set to blocking mode again...
if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) {
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
exit(0);
}
arg &= (~O_NONBLOCK);
if( fcntl(soc, F_SETFL, arg) < 0) {
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
exit(0);
}
// I hope that is all
}

2008年12月22日 星期一

google patent search

還記得我以前在學習專利的時候
要花很多時間和USPTO緩慢的搜索引擎掙扎
花了很多時間也不一定可以找到好的關鍵字
今天 我豁然之間發現google也有搜尋專利的功能了
真是太方便了...
www.google.com/patents
Google makes life easier every day...

2008年11月22日 星期六

GNU debug

I've been reading tutorials about gdb, and find it as very handy tool while debugging...
It comes out that you can set breaking points, watching points, access and change variables inside memory. That makes debugging so much easier than ever before.
here are some really useful websites that would really help out, if you would like to learn more about gdb, including two tutorials and a simple user manual.
GDB - GNU debugger
gdb Tutorial
Peter's gdb Tutorial: Table Of Contents

2008年8月1日 星期五

法國家鄉馬鈴薯

切丁未去皮的馬鈴薯
配上切碎的百里香
灑上黑胡椒 白胡椒 海鹽 西班牙紅醬
在預熱250度C的烤箱內烤熟

味道超好

2008年7月30日 星期三

涼拌鮮蝦葡萄柚

和姑姑到士林的TOROS吃東西
吃到一個好吃的沙拉
回來 找到他的食譜

材料:

1.蝦子數條 2.葡萄柚一顆 3.檸檬半顆 4.新鮮檸檬百里香少許
調味料:

1. 鹽 2.糖 3.橄欖油 4.白胡椒


作法:

1.蝦子放入滾水煮熟,蝦殼剝掉備用

2.將葡萄柚從中剖半,但果肉不要傷到,這裡需要小心一些,取出完整果肉

3.將葡萄柚剝成瓣狀備用

4.取一碗,將檸檬汁及所有調味料混合均勻

5.將蝦仁、葡萄柚果肉及調味料拌勻後,擺盤,最後用百里香裝飾一下即可。

2008年7月19日 星期六

美式鬆餅

以前想要自己做一份美式早餐鬆餅
都必須要先賣一包很貴很貴的鬆餅粉
有了這份食譜之後 就可以用麵粉還有泡打粉自己做出鬆餅粉了

食材

1.
低筋麵粉...........160g
2.
泡打粉.............6g
3.
奶油...............15g
4.
砂糖...............50g
5.
鹽.................1g
6.
牛奶(或水).........175g
7.
雞蛋...............2個
8.
楓糖...............適量

份量:10片


作法

1.
先將奶油放置室溫放軟,用加熱的方式也可以。
將低筋麵粉跟泡打粉過篩混和後備用。

2.
把糖、鹽
奶油加入麵粉中,牛奶分兩次加入攪拌均勻。
再把雞蛋打散攪拌後,加入麵糊裡一起攪拌均勻。
3.
先將鍋子預熱後,舀定量的麵糊到鍋子上煎。
我是用不沾的平底鍋,所以我開小火不放油乾煎。
等表面充滿氣泡後,就可以翻面了,翻面之後再煎20秒即可。
4.
將煎好的pancake疊成層狀,淋上楓糖
果醬鮮奶油即可。