freebug's profilefreebug's spaceBlogLists Tools Help
November 16

一年多后,再次体验Live Writer

第一次体验Live Writer大约是一年多前吧,记忆已经不是很清晰了。不过当时的感觉是writer还是比较粗糙,偶尔会不明原因的死掉。界面也比较一般,现在感觉好多了,界面非常的好看,在上面打字的感觉也很爽,让人感觉很流畅。

今天和小鸡一起去电脑城买了一块旧的512的ddr1 400的内存,现在ddr1的内存真的是很少了。04年在学校里配的机器,当时是256的内存,现在加了一块内存后用起来也比以前爽多了,虽然比当前多数机器的配置差远了,但是用起来还是感觉非常的爽,嘿嘿~~~

June 25

setsockopt()用法

1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket:
BOOL bReuseaddr=TRUE;
setsockopt(s,SOL_SOCKET ,SO_REUSEADDR,(const char*)&bReuseaddr,sizeof(BOOL));


2. 如果要已经处于连接状态的soket在调用closesocket后强制关闭,不经历
TIME_WAIT的过程:
BOOL bDontLinger = FALSE;
setsockopt(s,SOL_SOCKET,SO_DONTLINGER,(const char*)&bDontLinger,sizeof(BOOL));


3.在send(),recv()过程中有时由于网络状况等原因,发收不能预期进行,而设置收发时限:
int nNetTimeout=1000;//1秒
//发送时限
setsockopt(socket,SOL_S0CKET,SO_SNDTIMEO,(char *)&nNetTimeout,sizeof(int));
//接收时限
setsockopt(socket,SOL_S0CKET,SO_RCVTIMEO,(char *)&nNetTimeout,sizeof(int));


4.在send()的时候,返回的是实际发送出去的字节(同步)或发送到socket缓冲区的字节
(异步);系统默认的状态发送和接收一次为8688字节(约为8.5K);在实际的过程中发送数据
和接收数据量比较大,可以设置socket缓冲区,而避免了send(),recv()不断的循环收发:
// 接收缓冲区
int nRecvBuf=32*1024;//设置为32K
setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char*)&nRecvBuf,sizeof(int));
//发送缓冲区
int nSendBuf=32*1024;//设置为32K
setsockopt(s,SOL_SOCKET,SO_SNDBUF,(const char*)&nSendBuf,sizeof(int));


5. 如果在发送数据的时,希望不经历由系统缓冲区到socket缓冲区的拷贝而影响
程序的性能:
int nZero=0;
setsockopt(socket,SOL_S0CKET,SO_SNDBUF,(char *)&nZero,sizeof(nZero));


6.同上在recv()完成上述功能(默认情况是将socket缓冲区的内容拷贝到系统缓冲区):
int nZero=0;
setsockopt(socket,SOL_S0CKET,SO_RCVBUF,(char *)&nZero,sizeof(int));


7.一般在发送UDP数据报的时候,希望该socket发送的数据具有广播特性:
BOOL bBroadcast=TRUE;
setsockopt(s,SOL_SOCKET,SO_BROADCAST,(const char*)&bBroadcast,sizeof(BOOL));


8.在client连接服务器过程中,如果处于非阻塞模式下的socket在connect()的过程中可
以设置connect()延时,直到accpet()被呼叫(本函数设置只有在非阻塞的过程中有显著的
作用,在阻塞的函数调用中作用不大)
BOOL bConditionalAccept=TRUE;
setsockopt(s,SOL_SOCKET,SO_CONDITIONAL_ACCEPT,(const char*)&bConditionalAccept,sizeof(BOOL));


9.如果在发送数据的过程中(send()没有完成,还有数据没发送)而调用了closesocket(),以前我们
一般采取的措施是"从容关闭"shutdown(s,SD_BOTH),但是数据是肯定丢失了,如何设置让程序满足具体
应用的要求(即让没发完的数据发送出去后在关闭socket)?
struct linger {
u_short l_onoff;
u_short l_linger;
};
linger m_sLinger;
m_sLinger.l_onoff=1;//(在closesocket()调用,但是还有数据没发送完毕的时候容许逗留)
// 如果m_sLinger.l_onoff=0;则功能和2.)作用相同;
m_sLinger.l_linger=5;//(容许逗留的时间为5秒)
setsockopt(s,SOL_SOCKET,SO_LINGER,(const char*)&m_sLinger,sizeof(linger));

June 13

Tips on thread's last error code (valid in wintel platform)

 

引用

Tips on thread's last error code (valid in wintel platform)
We can use API GetLastError() to get the current thread's last error code in programme. But how can we instantly view the last error code when debugging?
 
1. If you use CDB, or Windbg, command "gle" will get you what you want. Also if you know well the structure of TEB, you can easily figure out the last error code was stored at address fs:34 (fs is used as TEB selector), so command "dd fs:34" will return what you want.
 
2. In Visual studio, it turn to be easy. Just typing either "@err,hr" or "err,hr" in Watch window will show what you want.
 ERR is a pseudoregister, and hr is a format specifier with ERR.
 
About pseudoregister:

A pseudoregister is not an actual hardware register, but is displayed as though it were a hardware register. With a pseudoregister, you can see and use certain values (error codes, thread information block...) in the debugger.

Complete list of pseudoregisters

Pseudoregister

Description

@ERR

Last error value; the same value returned by the GetLastError() API function

@TIB

Thread information block for the current thread; necessary because the debugger doesn't handle the "FS:0" format

@CLK

Undocumented clock register; usable only in the Watch window

@EAX, @EBX, @ECX, @EDX, @ESI, @EDI, @EIP, @ESP, @EBP, @EFL

Intel CPU registers

@CS, @DS, @ES, @SS, @FS, @GS

Intel CPU segment registers

@ST0, @ST1, @ST2, @ST3, @ST4, @ST5, @ST6, @ST7

Intel CPU floating-point registers

 

Two scenarios of pseudo-registers usage:

1. @ERR

Pseudoregisters can be used in conditional expressions. To try this out, put following lines after the fopen:

if (fp)  {       fclose(fp);   }

Put a breakpoint on the if (fp) line. Go to Edit->Breakpoints (or press Alt-F9). Select the breakpoint you just inserted and press the "Condition" button. Here, you can enter the @ERR==2 condition. Now start the debugger. The debugger will break on this breakpoint if fopen() failed because it couldn't find the file. If the file does exist, the debugger won't break, even if it encountered another error (say error 4: could not open the file). Try this out by running the code (not stepping) after creating, and deleting the "a_file_that_does_not_exist.txt" file on c:\.

Just for the very curious (and otherwise totally irrelevant to this article) : what does @ERR do? How does it get the error number? As it turns out, @ERR does exactly the same thing as GetLastError() does. These functions have a whopping 3 lines of assembly code:

mov eax,fs:[00000018h]   mov eax,dword ptr [eax+34h]   ret

So @ERR grabs the DWORD at offset 0x34 in the thread environment block pointed to by fs:[18h].

2 @TIB

Another important pseudoregister is @TIB. This is the thread information block for the current thread and is extremely helpful in multi-threaded debugging. If you place a breakpoint in a function that is called by multiple threads, the debugger will break execution every time no matter which thread passes the breakpoint. Even if you're stepping through your code, the debugger can jump to the breakpoint if another thread called the function. To solve this, you'll need to do the following. If execution breaks in the thread you want, add @TIB in the watch window. You will see some value like "0x7ffa6000" or "2147115008" in regular display. Go to the breakpoint menu (Alt-F9) and select the breakpoint. You can now add the @TIB==0x7ffa6000 condition filter. Doing this, the debugger will only break execution for this thread. All other threads using the same function will not result in a break.

This doesn't work in Windows 98 though. For Windows 98, you'll need to look at the Intel CPU FS register, which is unique for each thread. You can use the expression @FS==value

 

April 28

城市里面,长不出参天大树

    今天早上上班的时候无意看了一下路边的树木,发现这些树都长不高也长不大。在两三米高处,主干都被切掉,使其生出旁边的小枝来。一路看下去,看不到大树,回忆一下上次看到大树还是外出旅游的时候在一个古寺中看到的,那里的树都有几十米高,据说有的长了几千年,站在树下让人感觉天地之间尚有许多灵气。

北方的春天,很美,也很短

    第一次领略北方(具体说是天津)的春天,这里的春天很短暂,大概是从四月上旬中开始的吧,这时候树叶子开始慢慢的长出来了,到处都可以看到绿色,一个生机勃勃的世界,让人赏心悦目,太美妙了,唯一不好的就是春天里每天都有风,而且风还不小。然后到了四月下旬中的时候,春天就基本结束了,这时候树木的叶子已经长的比较大了,不过还是嫩绿的颜色,但是这个时候夏天也来了,天气开始热了,一到五一,就正式进入夏天了,这个时候风也没了,就是热。春天非常之短暂,也非常之美丽,到处一片生气,让人感觉很舒服,就想四处走走,欣赏四处的美景,如果能去乡村踏青是最好不过了。

April 15

What am I doing now?

    好久没看gmail了,今天突然看了一下,发现了nobita的一封对Twitter的邀请函,于是去Twitter看了下,发现其topic是"What are u doing now?",觉得这个想法不错,记录下自己每一天都做了些什么,这可以让我们每天有一个机会来反省自己是否虚度了年华,如果每天真的能记录下来自己所做的事情,到以后老了也可以不用写回忆录了,呵呵.

    于是乎我现在决定以后经常写一下what am I doing now了,但是我不想到Twitter去写,我觉得Microsoft的Live space也是这么一个地方.主要是随着我工作的日子一天天增加,我对Microsoft的崇拜和敬仰也是日益增加,还是喜欢用Microsoft的东西.

    那么到底What am I dong now呢?在公司做的事情由于属于公司的商业秘密,就不便于公开了,私下做的倒是可以说一说.其实最近很无聊,什么也没干,倒是昨天在qq上遇到以前的同学王帅力,他现在研究密码学方面的东西,发给我一个程序,是一个用RSA来加密文件的,让我看看改改,看了下我还是很有兴趣的,觉得可以做做,以后如果真的做出了有点使用价值的东东,再拿出来共享吧.

April 14

今天吃到了热干面

没想到在天津也能吃上热干面,真是谢谢LP了,呵呵,这么远从武汉给我带来.是方便面那种包装的,LP大人帮我煮好了,吃起来味道很正宗,恩,又怀念起在武汉四年的大学生活了,读书的日子真的是过的很惬意啊!

 

freebug cn

Occupation
Interests
写程序的