MSP Tech Passion

23/02/2009

Today I had a presentation about Functional Programming in >NET with C# and F# at Microsoft Vietnam office, according to the MSP Tech Passion conference series. All things is OK, and I think the audience really get exciting with my presentation. I’m really glad about it.

So I post the slide and demo source code here. If you wanna discuss more about this topic, feel free to contact me via email or by commenting in this post.


What can you do with MS SmallBasic

11/01/2009

I spent my rarely free time before the Lunar New Year holiday by playing with SmallBasic – one of more interesting tools introduced by Microsoft. Just another easy tool for newbies, but I wonder if “what can I do with it?”. It’s just has less than 15 keywords, and at the 1st sight, I can’t imagine anything I can do with it…

Read the rest of this entry »


Ensure reading data from socket in .NET

18/10/2008
private void ReadSockData(Socket sock, int len, byte[] dest)
{
  int iByteRead = 0;
  while (iByteRead < len)
  {
    iByteRead += sock.Receive(dest, iByteRead, len - iByteRead,
SocketFlags.None);
  }
}

Microsoft’s new ‘M’ programming language

14/10/2008

In a software-centric world where we already have many, many languages to program in, from scripting to bytecode compiled languages, to frameworks on top of languages and embedded languages, now Redmond wants to bring ANOTHER language to the table, titled ‘M’ (for Microsoft?).

Read the rest of this entry »


Another trick with System.Net.Sockets.NetworkStream class

04/10/2008

Để hiểu dc cái trick này, người viết đã phải mất vài ngày… Hix…

Bình thường thì một ứng dụng mạng .NET sẽ có thread mạng được bọc trong vòng lặp như sau:

NetworkStream s = m_TcpClnt.GetStream();
while (s.DataAvailable)
{
//read mess
s.Read(aLen, 0, MSG_LEN);
// ……………
}

Ở đây ta dùng thuộc tính DataAvailable của lớp NetworkStream để xác định khi nào trong stream còn dữ liệu. Tuy nhiên vấn đề là nếu ta vừa đọc lên 1 lượng dữ liệu khá lớn (tầm 2KB) trong vòng while thì ngay sau đó, mặc dù phía server ko truyền gì nữa, nhưng DataAvaiible vẫn có giá trị TRUE, và vòng lặp tiếp tục, ta sẽ đọc được toàn là rác…

Để hạn chế điều này, ta có thể viết như sau:

while(true)

{

NetworkStream s = m_TcpClnt.GetStream();
if(s.Read(aLen, 0, MSG_LEN) > 0)
{
// ……………
}

}

Hàm Read() sẽ block đến khi nào có dữ liệu mới thoát, trả về số byte mà nó nhận được. Mặc dù không triệt tiêu 100% (nhất là khi truyền qua môi trường WiFi, GPRS v.v…) nhưng có lẽ đây cũng là cách hiệu quả.