Socket.BeginSendTo(Byte[], Int

电脑杂谈  发布时间:2019-05-22 21:16:37  来源:网络整理

socket am2和socket am2_stream socket sendto_socket fm1和socket fm2

以异步方式将数据发送到特定远程主机stream socket sendto。Sends data asynchronously to a specific remote host.

public:
 IAsyncResult ^ BeginSendTo(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socket_flags, System::Net::EndPoint ^ remote_end, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginSendTo (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socket_flags, System.Net.EndPoint remote_end, AsyncCallback callback, object state);
member this.BeginSendTo : byte[] * int * int * System.Net.Sockets.SocketFlags * System.Net.EndPoint * AsyncCallback * obj -> IAsyncResult
Public Function BeginSendTo (buffer As Byte(), offset As Integer, size As Integer, socket_flags As SocketFlags, remote_end As EndPoint, callback As AsyncCallback, state As Object) As IAsyncResult

buffer

Byte 类型的数组,它包含要发送的数据。An array of type Byte that contains the data to send.

offset

buffer 中的从其开始发送数据的、从零开始编排的位置。The zero-based position in buffer at which to begin sending data.

要发送的字节数。The number of bytes to send.

socketFlags

socket am2和socket am2_stream socket sendto_socket fm1和socket fm2

SocketFlagsSocketFlagsSocketFlagsSocketFlags

SocketFlags 值的按位组合。A bitwise combination of the SocketFlags values.

remoteEP

EndPointEndPointEndPointEndPoint

表示远程设备的 EndPoint。An EndPoint that represents the remote device.

callback

AsyncCallbackAsyncCallbackAsyncCallbackAsyncCallback

AsyncCallback 委托。The AsyncCallback delegate.

state

一个对象,它包含此请求的状态信息。An object that contains state information for this request.

IAsyncResultIAsyncResultIAsyncResultIAsyncResult

stream socket sendto_socket am2和socket am2_socket fm1和socket fm2

引用异步发送的 IAsyncResult。An IAsyncResult that references the asynchronous send.

ArgumentNullExceptionArgumentNullExceptionArgumentNullExceptionArgumentNullException

buffer 为 null。buffer is null.

- 或 --or- remoteEP 为 nullstream socket sendto。remoteEP is null.

SocketExceptionSocketExceptionSocketExceptionSocketException

尝试访问套接字时出错。An error occurred when attempting to access the socket.

ArgumentOutOfRangeExceptionArgumentOutOfRangeExceptionArgumentOutOfRangeExceptionArgumentOutOfRangeException

offset 小于 0。offset is less than 0.

- 或 --or- offset 大于 buffer 的长度。offset is greater than the length of buffer.

- 或 --or- size 小于 0。size is less than 0.

- 或 --or- size 大于 buffer 的长度减去 offset 参数的值。size is greater than the length of buffer minus the value of the offset parameter.

socket am2和socket am2_stream socket sendto_socket fm1和socket fm2

ObjectDisposedExceptionObjectDisposedExceptionObjectDisposedExceptionObjectDisposedException

Socket 已关闭。The Socket has been closed.

SecurityExceptionSecurityExceptionSecurityExceptionSecurityException

调用堆栈中的较高调用方无权执行所请求的操作。A caller higher in the call stack does not have permission for the requested operation.

(我试运行了这段代码,如果通过数据绑定为datagridview添加数据,那么下面的代码将不会生效,因为它只能为非绑定方式添加的行排序,如果要以绑定方式添加数据,请参看我的示例程序)。所以send必须是异步发送.每次pop连接buffer中的数据,异步发送数据,成功后继续pop发送余下的数据,平时待发数据都push到连接buffer中,一但连接buffer数据超过一定大小,即可认定该连接已断线或者恶意不接受数据,直接kick(当然你也可以在send超时直接kick,但client可能只是偶尔延迟几秒,这样做势必导致玩家常常掉线.).。vb-get-webpage-source-demon.zip - 这是一个vb利用xmlhttp方式远程获取网页源码的示例,演示了post和get两种方法进行获取,可模拟提交表单数据进行远程登录,代码短小精悍,实用性很强。

IPHostEntry^ lipa = Dns::Resolve( "host.contoso.com" );
IPEndPoint^ lep = gcnew IPEndPoint( lipa->AddressList[ 0 ], 11000 );
Socket^ s = gcnew Socket( lep->Address->AddressFamily,
   SocketType::Stream,
   ProtocolType::Tcp );
try
{
   while ( true )
   {
      allDone->Reset();
      array<Byte>^ buff = Encoding::ASCII->GetBytes( "This is a test" );
      Console::WriteLine( "Sending Message Now.." );
      s->BeginSendTo( buff, 0, buff->Length, SocketFlags::None, lep,
         gcnew AsyncCallback( &Async_Send_Receive::Connect_Callback ), s );
      allDone->WaitOne();
   }
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e );
}
IPHostEntry lipa = Dns.Resolve("host.contoso.com");
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);
      Socket s = new Socket(lep.Address.AddressFamily,
      	                           SocketType.Stream,
                                        ProtocolType.Tcp);
      try{
         
                while(true){
                allDone.Reset();
                byte[] buff = Encoding.ASCII.GetBytes("This is a test");
                
                Console.WriteLine("Sending Message Now..");
                s.BeginSendTo(buff, 0, buff.Length, 0, lep, new AsyncCallback(Async_Send_Receive.SendTo_Callback), s);
                allDone.WaitOne();
           }
      }
      catch (Exception e){
           Console.WriteLine(e.ToString());
      }
   Dim lipa As IPHostEntry = Dns.Resolve("host.contoso.com")
   Dim lep As New IPEndPoint(lipa.AddressList(0), 11000)
   
   Dim s As New Socket(lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
   Try
      
      While True
         allDone.Reset()
         
         Dim buff As Byte() = Encoding.ASCII.GetBytes("This is a test")
         
         Console.WriteLine("Sending Message Now..")
         s.BeginSendTo(buff, 0, buff.Length, 0, lep, New AsyncCallback(AddressOf Async_Send_Receive.SendTo_Callback), s)
         
         allDone.WaitOne()
      End While
   Catch e As Exception
      Console.WriteLine(e.ToString())
   End Try
End Sub 'SendTo


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-100613-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...