English 中文(简体)
防止从睡觉到寄生。 收到的捐款
原标题:Prevent Thread From Sleeping When Calling Socket.Receive

I m working on a low latency financial application that receives tcp data over sockets.
This is how I m making a socket connection and receiving bytes:

public class IncomingData
{
  Socket _Socket;
  byte[] buffer = new byte[4096];

  public static void Connect(IPEndPoint endPoint)
  {
    _Socket = new Socket(
                  AddressFamily.InterNetwork,
                  SocketType.Stream, 
                  ProtocolType.Tcp);

    _Socket.Connect(endPoint);
  }

  public static void ReadSocket(int ReadQty)
  {
    _Socket.Receive(buffer, 0, ReadQty, SocketFlags.None); 
  }
}

我听说,当你打电话Receive()时,电话线索要睡觉,收到数据时,便醒。 我愿全力以赴(利用万国邮联的能力)。

是否有办法用一个简便的袖珍材料来做到这一点? 如果唯一的方式是罗列拉库,你能否树立榜样?

问题回答

如果我正确理解你的话,你希望不要被阻止?

查阅<代码> BeginX/EndX 表格类别方法。 这些方法同步进行(并不阻碍目前的胎面)。 他们接受反馈方法作为其参数之一,在作业完成时将采用这种方法(在这种情况下,将收到数据)。 这基本上与事件相同。

public class IncomingData
{
    Socket _Socket;
    byte[] buffer = new byte[4096];

    public static void Connect(IPEndPoint endPoint)
    {
        _Socket = new Socket(
                      AddressFamily.InterNetwork,
                      SocketType.Stream, 
                      ProtocolType.Tcp);        

        _Socket.Connect(endPoint);

    }

    public static void ReadSocket(int ReadQty)
    {
         // Wait for some data to be received. When data is received,
         // ReceiveCallback will be called.
         _Socket.BeginReceive(buffer, 0, ReadQty, SocketFlags.None, ReceiveCallback, null);
    }

    private static void ReceiveCallback(IAsyncResult asyncResult)
    {
        int bytesTransferred = _Socket.EndReceive(asyncResult);

        // ...
        // process the data
        // ...
    }
}

您可使用<代码>Socket.Poll,以确定袖珍是否拥有数据,并以其他方式保存:

// will spin here until poll returns true
while(!socket.Poll(0, SelectMode.SelectRead));
socket.Receive...

也许会通过核对networkComms.net,把数据发送到网络上。 http://bitbucket.org/MarcF/networkcomms.net/src/952f51c14671/ExamplesConsole/BasicSend.cs”rel=“nofollow”here,希望不会过于复杂! 你可能遇到的大多数问题已经得到解决,可能节省一些时间。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签