2009年11月18日 星期三

TCP Windows Size 解答

說明TCP Windows Size 的用途與注意事項
1.TCP Windows Size 目的是 Sender 與 Receiver 最好能把Data 一次處理完,
以避免因為網路設備彼此速度不同,而造成Data Lost 的問題。

2. TCP Windows Size 要多大呢?
window size = bandwidth * delay
假設網路是 100 Mbits/s, round trip time (RTT)是 5 ms,所以 Windows Size 應為
( 100 * 10^6) *( 5 * 10^-3) = 500 * 10^3 bits (65 kilobytes)

3.最後 Client 與 Server 的buffer 要設一樣:
/* An example of client code that sets the TCP window size */

int window_size = 128 * 1024; /* 128 kilobytes */

sock = socket(AF_INET, SOCK_STREAM, 0);

/* These setsockopt()s must happen before the connect() */
setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
(char *) &window_size, sizeof(window_size));
setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
(char *) &window_size, sizeof(window_size));
connect(sock, (struct sockaddr *) &address, sizeof(address));


/* An example of server code that sets the TCP window size */
int window_size = 128 * 1024; /* 128 kilobytes */
sock = socket(AF_INET, SOCK_STREAM, 0);

/* These setsockopt()s must happen before the accept() */
setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
(char *) &window_size, sizeof(window_size));
setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
(char *) &window_size, sizeof(window_size));

listen(sock, 5);
accept(sock, NULL, NULL);
4.備註:
a.Nonstop 系統
TCP programming :setsockopt 參數(SO_SNDBUF/SO_RCVBUF)
Default 8192 bytes

5.參考:
a.A User's Guide to TCP Windows
b.Enabling High Performance Data Transfers
c.Windows 2000 TCP Performance Tuning Tips

6.
a.Max Transmission Unit (MTU) - TCP header(20 byte)- IP header (20 bytes) =Max Segment Size(MSS)
b.

2009年11月12日 星期四

Python ctypes 使用方法(2)

介紹一些使用方法:
define C library 的 header 檔
from ctypes import *

WS_MAX_USER_LEN=10
WS_MAX_PIN_LEN =4
WS_MAX_LMK_ID =1
WS_MAX_IP_ADDR_LEN =10
WS_MAX_LABEL=5
WS_MAX_DESC=10
WS_MAX_LOG_ENTRY_LEN=100
WS_MAX_ZMK_KEY_ID_LEN=10

WS_VOID=c_void_p
WS_BYTE=c_ubyte
WS_CHAR=WS_BYTE
WS_BOOL=WS_BYTE
WS_USHORT=c_ushort
WS_ULONG=c_ulong
WS_LONG=c_long

WS_RV=WS_ULONG
WS_UNIT_ID=WS_ULONG
WS_DEVICE_ID=WS_ULONG
WS_CLUSTER_ID=WS_ULONG
WS_FLAGS=WS_ULONG
WS_UNIT_TYPE=WS_ULONG
# WebSentry  Specific Return Codes #
WSR_OK= 0x00000000
WSR_CANCEL = 0x00000001
WSR_HOST_MEMORY= 0x00000002
WSR_UNIT_ID_INVALID =0x00000003
WSR_GENERAL_ERROR=0x00000005

class WS_ITEM(Structure):
_fields_=[('pData',POINTER(WS_BYTE)),('len', WS_ULONG)]

class WS_VERSION(Structure):
_fields_=[('major', WS_BYTE), ('minor', WS_BYTE)]

from wapich  import *   #import python wrap data
from ctypes import *

libc=cdll.LoadLibrary('wapicd.dll')
WS_Initialize=libc.WS_Initialize  #define  function
WS_Initialize.restype=WS_RV    # set return type  = WS_RV, if none, always return int

rv=WSR_OK
rv=WS_Initialize()

if rv != WSR_OK:
print rv
else:
print 'WS_Initialize is Ok'

WS_GetInfo=libc.WS_GetInfo
WS_GetInfo.restype=WS_RV

myInfo=WS_INFO()
myInfo_p=pointer(myInfo)
myInfo_p.contents=myInfo   # assign  myInfo_ptr pointer to myInfo

rv=WSR_OK
rv=WS_GetInfo(myInfo_p)
if rv !=WSR_OK:
print rv

print myInfo.manufacturerID[:]
print myInfo.description[:]
print myInfo.version.major
print myInfo.version.minor

2009年11月6日 星期五

Python ctypes 使用方法(1)

因為要使用C libray ,所以要使用ctypes 去 wrap C function
from ctypes import *
from ctypes.util import find_library

print '---Begin Show----'

print find_library('c')
print windll.kernel32
print cdll.msvcrt
print find_library('wapicd')
libc=cdll.LoadLibrary("msvcr90.dll")

print "---------- char --------"
x=c_char_p("Hello World")
print x
print x.value
print sizeof(x)

print '---------- int ----------'
i=c_int(42)
print i
print i.value

p = create_string_buffer("Hello", 10)
print sizeof(p), repr(p.raw)

print "---------- Bo class---------------"
class Bo(object):
def __init__(self, number):
self._as_parameter_=number

bo=Bo(33)
printf = libc.printf
printf("%d bottles of beer \n", bo)

strchr=libc.strchr
print strchr('abcdef', ord('c'))
strchr.restype=c_char_p # set retyrn type is char_pointer
print strchr('abcdef', ord('d'))


print '----Passing by reference---------'
i=c_int()
f=c_float()
s=create_string_buffer('\000'*32)
print i.value, f.value, repr(s.value)

libc.sscanf("1 3.14 hello", "%d %f %s", byref(i), byref(f), s) #Passing by ref
print i.value, f.value, repr(s.value)

print '---- Pointer ---------'
ptr_i=pointer(i)
ptr_f=pointer(f)
ptr_s=pointer(s)
libc.sscanf("1 3.14 hello", "%d %f %s", ptr_i,ptr_f,ptr_s)
print i.value, f.value, repr(s.value)
print 'point of i:', ptr_i.contents
print 'point of f:', ptr_f.contents
print 'point of s:', ptr_s.contents

print '------------ Test Structure ---------'
class POINT(Structure):
_fields_=[('x', c_int), ('y', c_int)]

p=POINT(10, 20)
print p.x, p.y

def testptr(a):
b=POINT()
b=a
b.x=a.x+100
b.y=a.y+100
return b


testptr.restype=POINT()
g=testptr(p)
print g.x, g.y



測試結果:

---Begin Show----
msvcr90.dll


C:\WINDOWS\system32\wapicd.dll
---------- char --------
c_char_p('Hello World')
Hello World
4
---------- int ----------
c_long(42)
42
10 'Hello\x00\x00\x00\x00\x00'
---------- Bo class---------------
12838358
def
----Passing by reference byref()---------
0 0.0 ''
1 3.1400001049 'hello'
---- Pointer pointer()---------
1 3.1400001049 'hello'
point of i: c_long(1)
point of f: c_float(3.1400001049041748)
point of s:
------------ Test Structure ---------
10 20
110 120