置顶随笔

[置顶]Visual AssistX 破解 补丁

摘要: Visual.Assist.X.v10.4.1649.0.WinALL.Cracked-BRD.rarVisual.Assist.X.10.4.1646V10.3.1555下载 ,适合VC6,在VS2003.net以及VS2005.net中 本地下载visual_assist_1555_破解.7zVisual.Assist.X.v10.4.1624.0Visual.Assist.X.v10.4.1619.0阅读全文

posted @ 2008-11-25 10:02 LT 阅读(1322) 评论(0) 编辑

[置顶]推荐几款vs2005的插件

摘要: vs2005的插件阅读全文

posted @ 2008-11-14 14:18 LT 阅读(586) 评论(0) 编辑

[置顶]每个[NET]开发人员现在应该下载的十种必备工具

摘要: http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/TenMHToolEDevShouDN.mspx?mfr=true阅读全文

posted @ 2007-08-27 12:57 LT 阅读(172) 评论(0) 编辑

[置顶][原创]Ajax UpLoadFile 多个大文件上传控件及应用范例

摘要: [版本更新 uploadfile 1.13] 添加IE7支持阅读全文

posted @ 2007-05-06 16:05 LT 阅读(10711) 评论(49) 编辑

2012年2月9日

一个好用的C#抓图类

The Utilities class. Call Utilities.CaptureWindow(Control.Handle) to capture a specific control:

public static class Utilities
{
    public static Image CaptureScreen()
    {
        return CaptureWindow(User32.GetDesktopWindow());
    }

    public static Image CaptureWindow(IntPtr handle)
    {

        IntPtr hdcSrc = User32.GetWindowDC(handle);

        RECT windowRect = new RECT();
        User32.GetWindowRect(handle, ref windowRect);

        int width = windowRect.right - windowRect.left;
        int height = windowRect.bottom - windowRect.top;

        IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
        IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

        IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);
        Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, ApiConstants.SRCCOPY);
        Gdi32.SelectObject(hdcDest, hOld);
        Gdi32.DeleteDC(hdcDest);
        User32.ReleaseDC(handle, hdcSrc);

        Image image = Image.FromHbitmap(hBitmap);
        Gdi32.DeleteObject(hBitmap);

        return image;
    }
}

The Gdi32 class:

public class Gdi32
{
    [DllImport("gdi32.dll")]
    public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop);
    [DllImport("gdi32.dll")]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
    [DllImport("gdi32.dll")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
    [DllImport("gdi32.dll")]
    public static extern bool DeleteDC(IntPtr hDC);
    [DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr hObject);
    [DllImport("gdi32.dll")]
    public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}

The User32 class:

public static class User32
{
    [DllImport("user32.dll")]
    public static extern IntPtr GetDesktopWindow();
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
    [DllImport("user32.dll")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
}

The constants used:

    public const int SRCCOPY = 13369376;

The structs used:

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}

A friendly Control extension method:

public static class ControlExtensions
{
    public static Image DrawToImage(this Control control)
    {
        return Utilities.CaptureWindow(control.Handle);
    }
}

This is a code snippet from my CC.Utilities project and I specifically wrote it to take screenshots from the WebBrowser control.

posted @ 2012-02-09 18:23 LT 阅读(4) 评论(0) 编辑

2012年2月5日

【转】WinSocket编程——原始套接字

http://blog.csdn.net/dylgsy/article/details/1957138

posted @ 2012-02-05 22:17 LT 阅读(4) 评论(0) 编辑

【转】SHarpPcap(winpcap基于c#封转的库)网络嗅探程序核心

1. 获得网络设备

由于一个系统的网络设备可能不止一个,因而使用了一个列表类来保存所有的设备,这里使用了一个静态方法进行操作

/* Retrieve the device list */

PcapDeviceList devices = SharpPcap.GetAllDevices();

获取列表后,就能对设备进行操作了,其实设备分为2个子类,一类是NetworkDevice,这个是算是真实的网络设备吧,还有一类是PcapOfflineDevice,这个类是通过读取抓包文件生成的虚拟设备.

如果是NetworkDevice,那么还有些其他的网络信息,如ip地址,子网掩码等.

2. 抓包过程

在选定了一个PcapDevice后,就能使用他的方法进行抓包了.首先要打开设备.

 //Open the device for capturing

 //true -- means promiscuous mode

 //1000 -- means a read wait of 1000ms

 device.PcapOpen(true, 1000);

方法提供2个参数,第一个为抓包模式,指明是否抓其他Ip地址的包,类似Hub的功能;第二个是指超时时间,毫秒级.

下面就能正式抓包了,一共提供了3种方法:

 device.PcapStartCapture();

 异步方式,调用之后立即返回,具体抓下来的包,由PcapOnPacketArrival事件处理.需要停止的时候调用device.PcapStopCapture()进行关闭.

 device.PcapCapture(int packetCount);

半同步方式,调用后,直到抓到packetCount数量的包才返回., 具体抓下来的包,由PcapOnPacketArrival事件处理.

注意:如果传入SharpPcap.INFINITE将不退出,永远都在接收,且程序就停在这个语句了.

packet=device.PcapGetNextPacket()

同步方式,调用后直接等待收到的下一个包,并获得该包.

注意:如果超时,就可能还没有获得包体就退出该过程.这时packet=null,所以使用该方法每次都要对包进行检测.

最后一定要记得,关闭设备.

device.Close();

3. 包体分析.

在捕捉到包后,就需要根据实际的包进行转换了.

 if(packet is TCPPacket)

 {

TCPPacket tcp = (TCPPacket)packet;

}

因为这个需要转换的包类型很多,具体都在Tamir.IPLib.Packets里面.按照说明,一直尝试转换是不对的了,需要与过滤机制配合使用,只对自己有用的包分析

4. 过滤机制

包过滤是抓包程序的必备机制,要想对某次捕捉进行过滤,就必须在设备打开后,开始抓包前设置设备的过滤参数.

 //tcpdump filter to capture only TCP/IP packets           

 string filter = "ip and tcp";

 //Associate the filter with this capture

 device.PcapSetFilter( filter );

注意的是,filter是一个文本,遵循了tcpdump syntax.

5. 其他

A. SharpPcap还能保存捕获的包,而且使用起来也很简单.

保存: 在抓包前设置Dump的文件

//Open or create a capture output file

device.PcapDumpOpen( capFile);

抓到包后,把需要的包保存起来

 //dump the packet to the file

    device.PcapDump( packet );

使用:把包文件当作一个脱机设备

 //Get an offline file pcap device

device = SharpPcap.GetPcapOfflineDevice( capFile );

然后这个设备也可以捕捉包,使用起来和真实的一样(当然,不会有超时了就是)

B.对设备直接发包

相对与捕捉包,也可以发送包.提供了2种方法

 //Send the packet out the network device,直接发送包

     device.PcapSendPacket( bytes );

  //使用设备的发送队列

device.PcapSendQueue(squeue, true );

两种显然上面的容易,下面的高效

C.网络流量统计—没看,我暂时也不想用.

6.总结

主要来说PcapDevice类是整个操作的核心,把它用好了就成.

posted @ 2012-02-05 22:16 LT 阅读(9) 评论(0) 编辑

2012年2月3日

[心得] socket 多路复用误区

最近设计了个UDP服务器,开始只使用一个socket进行收发,结果数据一多,一个socket异步收发(基于IO完成端口)出现N多问题。

所以只能分开两个socket搞了,找了下文章。

首先udp和tcp不一样,tcp同一个端口只能一个listen,UDP则可以同时监听(当然只能其中一个获得数据,按规则匹配)

1、UDP多路复用的是按最小规范集转发数据的。

例如A bing any , b bing 127.0.01,这时如果收到一个地址为127.0.0.1的包,那么b的绑定记录显然更详细,B得到了数据,A收不到。

如果非127.0.01的本机地址按AB的绑定分析后则是a获得数据。

2、如果A\B都绑定any(规则一样) 则是最后绑定的收到数据,条件相同就后入先得。

 

我一开始的时候也是犯2了,a用来监听,死活听不到,后来把A放到b后bind 就没事了。。。。囧。

这文章是给初涉socket提个醒了。

posted @ 2012-02-03 18:52 LT 阅读(3) 评论(0) 编辑

2012年2月2日

UDP 异步 收发 服务端例子 【转载】

采用异步模式设计的UDP服务器,源码如下:

1 using System;
2  using System.Net;
3  using System.Net.Sockets;
4 using System.ServiceProcess;
5 using System.Threading;
6
7 namespace TestUdpServer
8 {
9 // this class encapsulates a single packet that
10 // is either sent or received by a UDP socket
11 public class UDPPacketBuffer
12 {
13 // size of the buffer
14 public const int BUFFER_SIZE = 4096;
15
16 // the buffer itself
17 public byte[] Data;
18
19 // length of data to transmit
20 public int DataLength;
21
22 // the (IP)Endpoint of the remote host
23 public EndPoint RemoteEndPoint;
24
25 public UDPPacketBuffer()
26 {
27 this.Data = new byte[BUFFER_SIZE];
28
29 // this will be filled in by the call to udpSocket.BeginReceiveFrom
30 RemoteEndPoint = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
31 }
32
33 public UDPPacketBuffer(byte[] data, EndPoint remoteEndPoint)
34 {
35 this.Data = data;
36 this.DataLength = data.Length;
37 this.RemoteEndPoint = remoteEndPoint;
38 }
39 }
40
41 public abstract class UDPServer
42 {
43 // the port to listen on
44 private int udpPort;
45
46 // the UDP socket
47 private Socket udpSocket;
48
49 // the ReaderWriterLock is used solely for the purposes of shutdown (Stop()).
50 // since there are potentially many "reader" threads in the internal .NET IOCP
51 // thread pool, this is a cheaper synchronization primitive than using
52 // a Mutex object. This allows many UDP socket "reads" concurrently - when
53 // Stop() is called, it attempts to obtain a writer lock which will then
54 // wait until all outstanding operations are completed before shutting down.
55 // this avoids the problem of closing the socket with outstanding operations
56 // and trying to catch the inevitable ObjectDisposedException.
57 private ReaderWriterLock rwLock = new ReaderWriterLock();
58
59 // number of outstanding operations. This is a reference count
60 // which we use to ensure that the threads exit cleanly. Note that
61 // we need this because the threads will potentially still need to process
62 // data even after the socket is closed.
63 private int rwOperationCount = 0;
64
65 // the all important shutdownFlag. This is synchronized through the ReaderWriterLock.
66 private bool shutdownFlag = true;
67
68 // these abstract methods must be implemented in a derived class to actually do
69 // something with the packets that are sent and received.
70 protected abstract void PacketReceived(UDPPacketBuffer buffer);
71 protected abstract void PacketSent(UDPPacketBuffer buffer, int bytesSent);
72
73 // ServiceName
74 String ServiceName = "test";
75
76 public UDPServer(int port)
77 {
78 this.udpPort = port;
79 }
80
81 public void Start()
82 {
83 if (shutdownFlag)
84 {
85 // create and bind the socket
86 IPEndPoint ipep = new IPEndPoint(IPAddress.Any, udpPort);
87 udpSocket = new Socket(
88 AddressFamily.InterNetwork,
89 SocketType.Dgram,
90 ProtocolType.Udp);
91 udpSocket.Bind(ipep);
92 // we're not shutting down, we're starting up
93 shutdownFlag = false;
94
95 // kick off an async receive. The Start() method will return, the
96 // actual receives will occur asynchronously and will be caught in
97 // AsyncEndRecieve().
98 // I experimented with posting multiple AsyncBeginReceives() here in an attempt
99 // to "queue up" reads, however I found that it negatively impacted performance.
100 AsyncBeginReceive();
101 }
102 }
103
104 protected void Stop()
105 {
106 if (!shutdownFlag)
107 {
108 // wait indefinitely for a writer lock. Once this is called, the .NET runtime
109 // will deny any more reader locks, in effect blocking all other send/receive
110 // threads. Once we have the lock, we set shutdownFlag to inform the other
111 // threads that the socket is closed.
112 rwLock.AcquireWriterLock(-1);
113 shutdownFlag = true;
114 udpSocket.Close();
115 rwLock.ReleaseWriterLock();
116
117 // wait for any pending operations to complete on other
118 // threads before exiting.
119 while (rwOperationCount > 0)
120 Thread.Sleep(1);
121 }
122 }
123
124 public bool IsRunning
125 {
126 // self-explanitory
127 get { return !shutdownFlag; }
128 }
129
130 private void AsyncBeginReceive()
131 {
132 // this method actually kicks off the async read on the socket.
133 // we aquire a reader lock here to ensure that no other thread
134 // is trying to set shutdownFlag and close the socket.
135 rwLock.AcquireReaderLock(-1);
136
137 if (!shutdownFlag)
138 {
139 // increment the count of pending operations
140 Interlocked.Increment(ref rwOperationCount);
141
142 // allocate a packet buffer
143 UDPPacketBuffer buf = new UDPPacketBuffer();
144
145 try
146 {
147 // kick off an async read
148 udpSocket.BeginReceiveFrom(
149 buf.Data,
150 0,
151 UDPPacketBuffer.BUFFER_SIZE,
152 SocketFlags.None,
153 ref buf.RemoteEndPoint,
154 new AsyncCallback(AsyncEndReceive),
155 buf);
156 }
157 catch (SocketException se)
158 {
159 // something bad happened
160 System.Diagnostics.EventLog.WriteEntry(ServiceName,
161 "A SocketException occurred in UDPServer.AsyncBeginReceive():\n\n" + se.Message,
162 System.Diagnostics.EventLogEntryType.Error);
163
164 // an error occurred, therefore the operation is void. Decrement the reference count.
165 Interlocked.Decrement(ref rwOperationCount);
166 }
167 }
168
169 // we're done with the socket for now, release the reader lock.
170 rwLock.ReleaseReaderLock();
171 }
172
173 private void AsyncEndReceive(IAsyncResult iar)
174 {
175 // Asynchronous receive operations will complete here through the call
176 // to AsyncBeginReceive
177
178 // aquire a reader lock
179 rwLock.AcquireReaderLock(-1);
180
181 if (!shutdownFlag)
182 {
183 // start another receive - this keeps the server going!
184 AsyncBeginReceive();
185
186 // get the buffer that was created in AsyncBeginReceive
187 // this is the received data
188 UDPPacketBuffer buffer = (UDPPacketBuffer)iar.AsyncState;
189
190 try
191 {
192 // get the length of data actually read from the socket, store it with the
193 // buffer
194 buffer.DataLength = udpSocket.EndReceiveFrom(iar, ref buffer.RemoteEndPoint);
195
196 // this operation is now complete, decrement the reference count
197 Interlocked.Decrement(ref rwOperationCount);
198
199 // we're done with the socket, release the reader lock
200 rwLock.ReleaseReaderLock();
201
202 // call the abstract method PacketReceived(), passing the buffer that
203 // has just been filled from the socket read.
204 PacketReceived(buffer);
205 }
206 catch (SocketException se)
207 {
208 // something bad happened
209 System.Diagnostics.EventLog.WriteEntry(ServiceName,
210 "A SocketException occurred in UDPServer.AsyncEndReceive():\n\n" + se.Message,
211 System.Diagnostics.EventLogEntryType.Error);
212
213 // an error occurred, therefore the operation is void. Decrement the reference count.
214 Interlocked.Decrement(ref rwOperationCount);
215
216 // we're done with the socket for now, release the reader lock.
217 rwLock.ReleaseReaderLock();
218 }
219 }
220 else
221 {
222 // nothing bad happened, but we are done with the operation
223 // decrement the reference count and release the reader lock
224 Interlocked.Decrement(ref rwOperationCount);
225 rwLock.ReleaseReaderLock();
226 }
227 }
228
229 public void AsyncBeginSend(UDPPacketBuffer buf)
230 {
231 // by now you should you get the idea - no further explanation necessary
232
233 rwLock.AcquireReaderLock(-1);
234
235 if (!shutdownFlag)
236 {
237 try
238 {
239 Interlocked.Increment(ref rwOperationCount);
240 udpSocket.BeginSendTo(
241 buf.Data,
242 0,
243 buf.DataLength,
244 SocketFlags.None,
245 buf.RemoteEndPoint,
246 new AsyncCallback(AsyncEndSend),
247 buf);
248 }
249 catch (SocketException se)
250 {
251 System.Diagnostics.EventLog.WriteEntry(ServiceName,
252 "A SocketException occurred in UDPServer.AsyncBeginSend():\n\n" + se.Message,
253 System.Diagnostics.EventLogEntryType.Error);
254 }
255 }
256
257 rwLock.ReleaseReaderLock();
258 }
259
260 private void AsyncEndSend(IAsyncResult iar)
261 {
262 // by now you should you get the idea - no further explanation necessary
263
264 rwLock.AcquireReaderLock(-1);
265
266 if (!shutdownFlag)
267 {
268 UDPPacketBuffer buffer = (UDPPacketBuffer)iar.AsyncState;
269
270 try
271 {
272 int bytesSent = udpSocket.EndSendTo(iar);
273
274 // note that call to the abstract PacketSent() method - we are passing the number
275 // of bytes sent in a separate parameter, since we can't use buffer.DataLength which
276 // is the number of bytes to send (or bytes received depending upon whether this
277 // buffer was part of a send or a receive).
278 PacketSent(buffer, bytesSent);
279 }
280 catch (SocketException se)
281 {
282 System.Diagnostics.EventLog.WriteEntry(ServiceName,
283 "A SocketException occurred in UDPServer.AsyncEndSend():\n\n" + se.Message,
284 System.Diagnostics.EventLogEntryType.Error);
285 }
286 }
287
288 Interlocked.Decrement(ref rwOperationCount);
289 rwLock.ReleaseReaderLock();
290 }
291
292 }
293 }

实际使用时需继承抽象类UDPServer,并实现异步处理数据的相关方案,示例如下:

 

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5
6 namespace TestUdpServer
7 {
8 public class TUdpServer : UDPServer
9 {
10
11 public TUdpServer(int port)
12 : base(port)
13 {
14 Console.WriteLine("Server Start...@" + port.ToString());
15 }
16
17 protected override void PacketReceived(UDPPacketBuffer buffer)
18 {
19 AsyncBeginSend(buffer);
20 }
21
22 protected override void PacketSent(UDPPacketBuffer buffer, int bytesSent)
23 {
24
25 }
26
27 }
28 }
29

 

posted @ 2012-02-02 00:14 LT 阅读(13) 评论(0) 编辑

2012年2月1日

UDPClient 的奇特现象,实在搞不懂

我有个项目,设计了一个UDP服务端。

是通过UDPCLIENT 的一个实例(同一端口)提供收发服务。

 

当项目上线测试后,奇怪的现象发生。

如果重启过服务器的话,服务器可以运行一段较长时间无异常。

但一段时间之后,奇怪的事情来了。

 

1、例如之前有A\B\C等N个客户端已经连接到服务端,服务端一直持续为它们 提供服务,然后在正常运行一段时间后,ABC的数据都会变得不正常,服务端无法再正常解析数据提供服务了。客户数据是可以接收到,但已经不正确了。

2、然后更搞笑的是,如果有新入的连接(不同的IP),服务端却又能正常提供为该客户服务,但服务一段时间后,就会表现和ABC一样。

然后重复上面的两个问题 ,不断累积,反正有新客户(不同的IP)就正常服务,但不定什么时候就不正常了。

 

我的接收是通过异步BeginReceive投递的,而发送则是以阻塞方式Send

其中EndReceive也进行了线程同步, UDP的接收缓冲区我也设置得足够大了,TCP连接限制和端口可用数我也修改过,但问题依然无法解决。

 

让这个问题搞了几天了,希望大家能提供些思路和解决办法 ,真的古怪

posted @ 2012-02-01 20:33 LT 阅读(109) 评论(2) 编辑

2012年1月27日

[转载]TCP打开端口数和打开连接数限制系列文章

摘要: [转载]TCP打开端口数和打开连接数限制系列文章阅读全文

posted @ 2012-01-27 17:22 LT 阅读(9) 评论(0) 编辑

2012年1月16日

UDP错误10054:远程主机强迫关闭的解决方法---转载

摘要: UDP错误10054:远程主机强迫关闭了一个现有的连接 原文地址:http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic1887.aspx 在 公司一项目的UDP消息服务开发中时不时的会遇到这样一个问题:在UDP通信过程中,如果客户端中途断开,服务器会收到一个 SocketException,错误ID为10054,描述是“远程主机强迫关闭了一个现有的连接”,紧接着的事就可怕了,UDP服务终止监听,所有客 户端都受到了影响。也就是说一个客户端引起的异常导致了整个系统的崩溃。这个问题可...阅读全文

posted @ 2012-01-16 23:26 LT 阅读(5) 评论(0) 编辑

2011年11月6日

应用程式上屏蔽flash控件的右键菜单并获得左键消息_c#

摘要: 使用FLASH做界面美化是一种较为常见的方式,FLASH动画制做简单,美观大方,而且将FLASH嵌入到应用程式界面上也是很容易的,只要放置 一个Shockwave Flash控件即可。但这样做出来的界面有两个问题,一是在FLASH上点击右键会有FLASH的菜单,二是在FLASH上点击的鼠标消息从应用程式里是 获取不到的,FLASH控件并不会向用应程式发送WM_LBUTTONDOWN等鼠标消息。解决的办法应该有很多,我使用了自定义窗口过程的方法,经过试验效果不错。以下都基于Visual C 6.0和MFC环境:首先需要为Flash控件生成变量,这一步借助ClassWizard能够轻松完成,...阅读全文

posted @ 2011-11-06 17:40 LT 阅读(15) 评论(0) 编辑

2011年9月21日

AJAX分段下载/读取HTML内容(有效节省带宽加快运行速度)

摘要: AJAX分段下载/读取HTML内容(有效节省带宽加快运行速度) 本来以为自己已经算是对AJAX技术了如指掌了,因为从3年前就一直用XMLHttpRequest对象做一些无刷新页面的处理工作,但是,直道今天在工作中遇到了一个分段读取内容的问题,我才了解到,自己原来还需要补充。 基本的问题是这样的: 我需要设计一个框架,需要承载500万以上用户访问(公司毕竟是中国用户最多的互联网企业)。我们用了大量的静态化技术,为了应对可能出现的高负载,我们 还是用了一些公司内自主研发的高性能静态WEB服务器。由于页面是静态生成的,我们遇到一个问题,就是如何在某一时刻更新了某个静态文件后,在前台进行 AJA...阅读全文

posted @ 2011-09-21 20:45 LT 阅读(26) 评论(0) 编辑

导航

<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

公告

昵称:LT
园龄:5年9个月
粉丝:2
关注:1

搜索

 
 

常用链接

最新随笔

随笔分类(324)

随笔档案(257)

相册

友情链接

最新评论

阅读排行榜

评论排行榜

推荐排行榜

直角体Web动力