Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .Net异步套接字多线程发送_C#_.net_Multithreading_Sockets_Networking - Fatal编程技术网

C# .Net异步套接字多线程发送

C# .Net异步套接字多线程发送,c#,.net,multithreading,sockets,networking,C#,.net,Multithreading,Sockets,Networking,我有一个套接字,我用它来发送大的缓冲区,如果我做类似的事情的话 // may be accsed form a a lot of threads void Send(byte[] buffer) { m_socket.BeginSend(buffer ... , SendCallback); } void SendCallback() { m_socket.EndSend() // if not done sending- send the reset m_socket.Be

我有一个套接字,我用它来发送大的缓冲区,如果我做类似的事情的话

// may be accsed form a a lot of threads
void Send(byte[] buffer)
{
  m_socket.BeginSend(buffer ... , SendCallback);
}

void SendCallback()
{
  m_socket.EndSend()

  // if not done sending- send the reset
  m_socket.BeginSend()
}
我的问题是:从多线程的角度来看,这是可行的,还是缓冲区会交错?

引用MSDN:

如果执行多个异步 在套接字上执行操作时,它们不会 必须按照中的顺序完成 这是他们开始的


但是,如果您从多个线程发送数据块,您对“顺序”的定义是什么?

这似乎是线程安全的

由于对“SendCallback”的委托是在一个新线程上执行的,因此我假定EndSend()可以根据当前线程上下文判断要结束的异步操作

有关异步编程模型,请参见MSDN“最佳实践”:

Simultaneously Executing Operations

If your class supports multiple concurrent invocations, enable the developer to track each invocation separately by defining the MethodNameAsync overload that takes an object-valued state parameter, or task ID, called userSuppliedState. This parameter should always be the last parameter in the MethodNameAsync method's signature.

您能更清楚地了解您使用的API吗?我没有看到与您的调用匹配的Socket.Send()重载。很抱歉,函数名错误,我已编辑。im使用:public IAsyncResult BeginSend(字节[]缓冲区,int偏移量,int大小,SocketFlags SocketFlags,异步回调,对象状态);如果我必须发送消息:“aaaa”和“bbbb”,我不会让接收者收到:“aaaabbbb”或“bbbbaaaa”,但如果套接字的发送缓冲区是2字节大,则不会收到“aabbaabb”。