当前位置: 首页>>代码示例>>C#>>正文


C# SocketAsyncEventArgs类代码示例

本文整理汇总了C#中SocketAsyncEventArgs的典型用法代码示例。如果您正苦于以下问题:C# SocketAsyncEventArgs类的具体用法?C# SocketAsyncEventArgs怎么用?C# SocketAsyncEventArgs使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


SocketAsyncEventArgs类属于命名空间,在下文中一共展示了SocketAsyncEventArgs类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SocketAsyncEventCompleted

 static void SocketAsyncEventCompleted(object sender, SocketAsyncEventArgs e)
 {
     e.Completed -= SocketAsyncEventCompleted;
     var token = (ConnectToken)e.UserToken;
     e.UserToken = null;
     token.Callback(sender as Socket, token.State, e);
 }
开发者ID:ChuckFork,项目名称:SuperSocket.ClientEngine,代码行数:7,代码来源:ConnectAsyncExtension.cs

示例2: OnGetSocket

        protected override void OnGetSocket(SocketAsyncEventArgs e)
        {
            try
            {
#if SILVERLIGHT
                var sslStream = new SslStream(new NetworkStream(Client));
                sslStream.BeginAuthenticateAsClient(HostName, OnAuthenticated, sslStream);
#else
                var securityOption = Security;

                if (securityOption == null)
                {
                    throw new Exception("securityOption was not configured");
                }

#if NETSTANDARD

                AuthenticateAsClientAsync(new SslStream(new NetworkStream(Client), false, ValidateRemoteCertificate), Security);             
 
#else

                var sslStream = new SslStream(new NetworkStream(Client), false, ValidateRemoteCertificate);
                sslStream.BeginAuthenticateAsClient(HostName, securityOption.Certificates, securityOption.EnabledSslProtocols, false, OnAuthenticated, sslStream);
                
#endif
#endif

            }
            catch (Exception exc)
            {
                if (!IsIgnorableException(exc))
                    OnError(exc);
            }
        }
开发者ID:ChuckFork,项目名称:SuperSocket.ClientEngine,代码行数:34,代码来源:SslStreamTcpSession.cs

示例3: Success

        public void Success()
        {
            ManualResetEvent completed = new ManualResetEvent(false);

            if (Socket.OSSupportsIPv4)
            {
                using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
                {
                    int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
                    receiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);

                    Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
                    sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));

                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    args.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    args.SetBuffer(new byte[1024], 0, 1024);
                    args.Completed += OnCompleted;
                    args.UserToken = completed;

                    Assert.True(receiver.ReceiveMessageFromAsync(args));

                    Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timeout while waiting for connection");

                    Assert.Equal(1024, args.BytesTransferred);
                    Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
                    Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);

                    sender.Dispose();
                }
            }
        }
开发者ID:rainersigwald,项目名称:corefx,代码行数:33,代码来源:ReceiveMessageFromAsync.cs

示例4: DisconnectAsync_Success

        public void DisconnectAsync_Success()
        {
            AutoResetEvent completed = new AutoResetEvent(false);

            IPEndPoint loopback = new IPEndPoint(IPAddress.Loopback, 0);
            using (var server1 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
            using (var server2 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
            {
                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.Completed += OnCompleted;
                args.UserToken = completed;
                args.RemoteEndPoint = server1.EndPoint;
                args.DisconnectReuseSocket = true;

                using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    Assert.True(client.ConnectAsync(args));
                    completed.WaitOne();
                    Assert.Equal(SocketError.Success, args.SocketError);

                    Assert.True(client.DisconnectAsync(args));
                    completed.WaitOne();
                    Assert.Equal(SocketError.Success, args.SocketError);

                    args.RemoteEndPoint = server2.EndPoint;

                    Assert.True(client.ConnectAsync(args));
                    completed.WaitOne();
                    Assert.Equal(SocketError.Success, args.SocketError);
                }
            }
        }
开发者ID:Corillian,项目名称:corefx,代码行数:32,代码来源:DisconnectTest.cs

示例5: Bot

            public Bot(BotManager manager,BotDescriptor BotDesc, int BotId)
            {
                FBotId = BotId;
                FManager = manager;
                FBotDesc = BotDesc;
                FBufferManager = new BufferManager();
                FBufferManager.Initialize(100, 100);

                FPacketSpliter = new PacketSpliter();

                FPacketSpliter.OnPacket += OnPacket;

                FConnArg = new SocketAsyncEventArgs();
                FConnArg.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
                FConnArg.UserToken = this;
                FConnArg.RemoteEndPoint = FBotDesc.Host;

                FDisconnArg = new SocketAsyncEventArgs();
                FDisconnArg.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
                FDisconnArg.UserToken = this;
                FDisconnArg.DisconnectReuseSocket = true;

                FSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                FTimer = new Timer(OnTimer);
            }
开发者ID:lythm,项目名称:orb3d,代码行数:26,代码来源:Bot.cs

示例6: StartReceiving

	private void StartReceiving(object sender, SocketAsyncEventArgs args)
	{
		// если входящее подключение принято
		switch (args.SocketError)
		{
		case SocketError.Success:
			Log.Trace("{0} подключен", args.AcceptSocket.RemoteEndPoint);
			
			var recieveArgs = _clientPool.TakeObject();
		 	recieveArgs.AcceptSocket = args.AcceptSocket;
			//args.AcceptSocket.BeginReceive
			//if (!args.AcceptSocket.ReceiveAsync(recieveArgs.AcceptSocket))
				ProcessReceive(recieveArgs);
			break;
		case SocketError.OperationAborted:
			break;
		default:
			Log.Error("Подключение не принято: {0}", args.SocketError);
			break;
		}
		
		// освобождаем принятый сокет и начинаем новый прием
		args.AcceptSocket = null;
		StartAccepting();
	}
开发者ID:hebbaixue99,项目名称:XJDemoServer,代码行数:25,代码来源:DemoMain.cs

示例7: Success

        public void Success()
        {
            AutoResetEvent completed = new AutoResetEvent(false);

            if (Socket.OSSupportsIPv4)
            {
                int port, port1;
                using (SocketTestServer.SocketTestServerFactory(IPAddress.Loopback, out port))
                using (SocketTestServer.SocketTestServerFactory(IPAddress.Loopback, out port1))
                {
                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    args.Completed += OnCompleted;
                    args.UserToken = completed;
                    args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port);
                    args.DisconnectReuseSocket = true;

                    Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                    Assert.True(client.ConnectAsync(args));
                    Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timed out while waiting for connection");
                    Assert.Equal<SocketError>(SocketError.Success, args.SocketError);

                    client.Disconnect(true);

                    args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port1);

                    Assert.True(client.ConnectAsync(args));
                    Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timed out while waiting for connection");
                    Assert.Equal<SocketError>(SocketError.Success, args.SocketError);

                    client.Dispose();
                }
            }
        }
开发者ID:shrutigarg,项目名称:corefx,代码行数:34,代码来源:Disconnect.cs

示例8: AsyncEventArgsCompleted

 protected void AsyncEventArgsCompleted(object sender, SocketAsyncEventArgs e)
 {
     if (e.LastOperation == SocketAsyncOperation.Send)
         ProcessSend(e);
     else
         ProcessReceive(e);
 }
开发者ID:wiyonoaten,项目名称:pusher-websocket-dotnet,代码行数:7,代码来源:ProxyConnectorBase.cs

示例9: GetNonDefaultIPPacketInformation

        private IPPacketInformation GetNonDefaultIPPacketInformation()
        {
            const int ReceiveTimeout = 5000;

            using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                int port = receiver.BindToAnonymousPort(IPAddress.Loopback);

                var waitHandle = new ManualResetEvent(false);

                SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs {
                    RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port),
                    UserToken = waitHandle
                };

                receiveArgs.SetBuffer(new byte[1], 0, 1);
                receiveArgs.Completed += (_, args) => ((ManualResetEvent)args.UserToken).Set();

                Assert.True(receiver.ReceiveMessageFromAsync(receiveArgs));

                // Send a few packets, in case they aren't delivered reliably.
                for (int i = 0; i < Configuration.UDPRedundancy; i++)
                {
                    sender.SendTo(new byte[1], new IPEndPoint(IPAddress.Loopback, port));
                }

                Assert.True(waitHandle.WaitOne(ReceiveTimeout));

                return receiveArgs.ReceiveMessageFromPacketInfo;
            }
        }
开发者ID:SGuyGe,项目名称:corefx,代码行数:32,代码来源:IPPacketInformationTest.cs

示例10: NewBuffer

            private SocketAsyncEventArgs NewBuffer()
            {
                SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                e.SetBuffer(new byte[1024], 0, 1024);

                return e;
            }
开发者ID:lythm,项目名称:orb3d,代码行数:7,代码来源:BufferManager.cs

示例11: Socket_ConnectAsyncDnsEndPoint_Success

        public void Socket_ConnectAsyncDnsEndPoint_Success()
        {
            Assert.True(Capability.IPv4Support());

            int port;
            SocketTestServer server = SocketTestServer.SocketTestServerFactory(IPAddress.Loopback, out port);

            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            args.RemoteEndPoint = new DnsEndPoint("localhost", port);
            args.Completed += OnConnectAsyncCompleted;

            ManualResetEvent complete = new ManualResetEvent(false);
            args.UserToken = complete;

            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Assert.True(sock.ConnectAsync(args));

            complete.WaitOne();

            Assert.Equal(SocketError.Success, args.SocketError);
            Assert.Null(args.ConnectByNameError);

            complete.Dispose();
            sock.Dispose();
            server.Dispose();
        }
开发者ID:nbilling,项目名称:corefx,代码行数:26,代码来源:DnsEndPointTest.cs

示例12: GetNonDefaultIPPacketInformation

        private IPPacketInformation GetNonDefaultIPPacketInformation()
        {
            const int ReceiveTimeout = 5000;

            using (var receiver = new Socket(SocketType.Dgram, ProtocolType.Udp))
            using (var sender = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                int port = receiver.BindToAnonymousPort(IPAddress.Loopback);

                var waitHandle = new ManualResetEvent(false);

                SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs {
                    RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port),
                    UserToken = waitHandle
                };

                receiveArgs.SetBuffer(new byte[1], 0, 1);
                receiveArgs.Completed += (_, args) => ((ManualResetEvent)args.UserToken).Set();

                Assert.True(receiver.ReceiveMessageFromAsync(receiveArgs));

                sender.SendTo(new byte[1], new IPEndPoint(IPAddress.Loopback, port));

                Assert.True(waitHandle.WaitOne(ReceiveTimeout));

                return receiveArgs.ReceiveMessageFromPacketInfo;
            }
        }
开发者ID:vbouret,项目名称:corefx,代码行数:28,代码来源:IPPacketInformationTest.cs

示例13: BeginRequestTime

		/// <summary>
		/// Connects to the server and begins listening for time updates.
		/// </summary>
		public void BeginRequestTime()
		{
			byte[] buffer = new byte[48];
			buffer[0] = 0x1B;
			for (var i = 1; i < buffer.Length; ++i)
			{
				buffer[i] = 0;
			}

			var endPoint = new DnsEndPoint(_ServerAddress, 123);

			_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
			var socketArgs = new SocketAsyncEventArgs() { RemoteEndPoint = endPoint };
			socketArgs.Completed += (o, e) =>
			{
				if (e.SocketError == SocketError.Success)
				{
					var sArgs = new SocketAsyncEventArgs()
					{
						RemoteEndPoint = endPoint
					};

					sArgs.Completed += new EventHandler<SocketAsyncEventArgs>(sArgs_Completed);
					sArgs.SetBuffer(buffer, 0, buffer.Length);
					sArgs.UserToken = buffer;
					_Socket.SendAsync(sArgs);
				}
			};
			_Socket.ConnectAsync(socketArgs);
		}
开发者ID:Yortw,项目名称:Yort.Ntp,代码行数:33,代码来源:NtpClient.cs

示例14: makeSocketAsyncEventArgs

 //生成自定义AsyncEventArg
 private SocketAsyncEventArgs makeSocketAsyncEventArgs()
 {
     SocketAsyncEventArgs re = new SocketAsyncEventArgs();
     re.UserToken = new AsyncUserToken(m_userTokenBufferLength);
     re.SetBuffer(((AsyncUserToken)re.UserToken).buffer, 0, m_userTokenBufferLength);
     re.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
     return re;
 }
开发者ID:261521019,项目名称:CSharp-server,代码行数:9,代码来源:BaseSocket.cs

示例15: SendData

 public void SendData(string line)
 {
     byte[] data = System.Text.Encoding.UTF8.GetBytes(line + "\n");
     SocketAsyncEventArgs socketAsyncData = new SocketAsyncEventArgs();
     socketAsyncData.SetBuffer(data, 0, data.Length);
     bool res = _clientSocket.SendAsync(socketAsyncData);
     Debug.Log("Send line:" + line + " res:" + res);
 }
开发者ID:Genesic,项目名称:WarGamingProject,代码行数:8,代码来源:Client.cs


注:本文中的SocketAsyncEventArgs类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。