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


C# Socket.BeginReceiveFrom方法代码示例

本文整理汇总了C#中Socket.BeginReceiveFrom方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginReceiveFrom方法的具体用法?C# Socket.BeginReceiveFrom怎么用?C# Socket.BeginReceiveFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Socket的用法示例。


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

示例1: Main

    static void Main()
    {
        var sk = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            EndPoint my = new IPEndPoint(IPAddress.Loopback,1233);
            sk.BeginReceiveFrom(new byte[1500], 0, 1500, SocketFlags.None, ref my, new AsyncCallback(ReceiveCallback), null);
            sk.Close();
    }
开发者ID:tigerhu67,项目名称:monobuildtools,代码行数:8,代码来源:lucas.cs

示例2: UDPServer

	public UDPServer()
	{
		try
		{
			// Iniciando array de clientes conectados
			this.listaClientes = new ArrayList();
			entrantPackagesCounter = 0;
			sendingPackagesCounter = 0;			
			// Inicializando el delegado para actualizar estado
			//this.updateStatusDelegate = new UpdateStatusDelegate(this.UpdateStatus);
		
			// Inicializando el socket
			serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
			
			// Inicializar IP y escuhar puerto 30000
			IPEndPoint server = new IPEndPoint(IPAddress.Any, 30001);
			
			// Asociar socket con el IP dado y el puerto
			serverSocket.Bind(server);
			
			// Inicializar IPEndpoint de los clientes
			IPEndPoint clients = new IPEndPoint(IPAddress.Any, 0);
			
			// Inicializar Endpoint de clientes
			EndPoint epSender = (EndPoint)clients;
			
			// Empezar a escuhar datos entrantes
			serverSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epSender, new AsyncCallback(ReceiveData), epSender);

		}
		catch (Exception ex)
		{
			//Debug.Log("Error al cargar servidor: " + ex.Message+ " ---UDP ");
		}
	}
开发者ID:PJEstrada,项目名称:proyecto1-redes,代码行数:35,代码来源:UDPServer.cs

示例3: UDPClient

	public UDPClient(string serverIP) {
		//Paquete sendData = new Paquete ();
		//sendData.id = 0;
		//sendData.identificadorPaquete = Paquete.Identificador.conectar;
		entrantPackagesCounter = 0;
		sendingPackagesCounter = 0;
		//Creamos conexion
		this.clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
		//Inicializamos IP del servidor
		try{
			IPAddress ipServer = IPAddress.Parse (serverIP);
			IPEndPoint server = new IPEndPoint (ipServer, 30001);
			
			epServer = (EndPoint)server;
			//Debug.Log("Enviando data de inicio de conexion: ");
			//string  data = sendData.GetDataStream();
			//byte[] dataBytes = GetBytes (data);
			//Enviar solicitud de conexion al servidor
			//clientSocket.BeginSendTo (dataBytes,0,dataBytes.Length,SocketFlags.None,epServer,new System.AsyncCallback(this.SendData),null);
			// Inicializando el dataStream
			this.dataStream = new byte[1024];
			// Empezamos a escuhar respuestas del servidor
			clientSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epServer, new AsyncCallback(this.ReceiveData), null);

		}
		catch(FormatException e){
		
			throw e;
		}
		catch(SocketException e){
			
			throw e;
		}
	
	}
开发者ID:PJEstrada,项目名称:proyecto1-redes,代码行数:35,代码来源:UDPClient.cs

示例4: StartReceive

 public void StartReceive()
 {
     if (receiveSocket != null)
         StopReceive();
     receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     bindEndPoint = new IPEndPoint(IPAddress.Any, 3001);
     recBuffer = new byte[maxBuffer];
     receiveSocket.Bind(bindEndPoint);
     receiveSocket.BeginReceiveFrom(recBuffer, 0, recBuffer.Length, SocketFlags.None, ref bindEndPoint, new AsyncCallback(MessageReceivedCallback), (object)this);
 }
开发者ID:andrejpanic,项目名称:win-mobile-code,代码行数:10,代码来源:sockrcv.cs

示例5: Initialize

    public void Initialize(String _remoteIp, int _remotePort, int _localPort)
    {
        Debug.Log ("UAC CTOR");
        localPort = _localPort;

        remoteIp = _remoteIp;
        remotePort = _remotePort;
        //Setup the socket and message buffer
        udpSock = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        udpSock.Bind (new IPEndPoint (IPAddress.Any, localPort));
        buffer = new byte[1024];

        //Start listening for a new message.
        EndPoint newClientEP = new IPEndPoint (IPAddress.Any, 0);
        udpSock.BeginReceiveFrom (buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
    }
开发者ID:siddht1,项目名称:GameJam2013-AtTheCore,代码行数:16,代码来源:UdpAsyncConnection.cs

示例6: StartReceive

    public void StartReceive()
    {
        try
        {
            if (receiveSocket != null)
                StopReceive();
            receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            bindEndPoint = new IPEndPoint(IPAddress.Any, 3002);

            recBuffer = new byte[maxBuffer];
            receiveSocket.Bind(bindEndPoint);
            receiveSocket.BeginReceiveFrom(recBuffer, 0, recBuffer.Length, SocketFlags.None, ref bindEndPoint, new AsyncCallback(MessageReceivedCallback), (object)this);

        }
        catch (SocketException ex)
        {
            System.Diagnostics.Debug.WriteLine(String.Format("StartReceive SocketException: {0} ", ex.Message));
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(String.Format("StartReceive Exception: {0} ", ex.Message));
        }
    }
开发者ID:hjgode,项目名称:VMusage,代码行数:23,代码来源:sockrcv.cs

示例7: BeginReceiveFrom_NotSupported

        public void BeginReceiveFrom_NotSupported()
        {
            using (Socket sock = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                EndPoint ep = new IPEndPoint(IPAddress.Any, 0);
                sock.Bind(ep);

                byte[] buf = new byte[1];

                Assert.Throws<PlatformNotSupportedException>(() => sock.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref ep, null, null));
            }
        }
开发者ID:eerhardt,项目名称:corefx,代码行数:12,代码来源:DualModeSocketTest.cs

示例8: BeginReceiveFrom_Helper

        private void BeginReceiveFrom_Helper(IPAddress listenOn, IPAddress connectTo, bool expectedToTimeout = false)
        {
            using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                serverSocket.ReceiveTimeout = 500;
                int port = serverSocket.BindToAnonymousPort(listenOn);

                EndPoint receivedFrom = new IPEndPoint(connectTo, port);
                IAsyncResult async = serverSocket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);

                // Behavior difference from Desktop: receivedFrom will _not_ change during the synchronous phase.

                // IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                // Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                // Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

                SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);
                bool success = async.AsyncWaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout);
                if (!success)
                {
                    throw new TimeoutException();
                }

                receivedFrom = new IPEndPoint(connectTo, port);
                int received = serverSocket.EndReceiveFrom(async, ref receivedFrom);

                Assert.Equal(1, received);
                Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));

                IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
            }
        }
开发者ID:eerhardt,项目名称:corefx,代码行数:34,代码来源:DualModeSocketTest.cs

示例9: Socket_BeginReceiveFromDnsEndPoint_Throws

        // "The parameter remoteEP must not be of type DnsEndPoint."
        public void Socket_BeginReceiveFromDnsEndPoint_Throws()
        {
            using (Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                int port = socket.BindToAnonymousPort(IPAddress.IPv6Loopback);
                EndPoint receivedFrom = new DnsEndPoint("localhost", port, AddressFamily.InterNetworkV6);

                Assert.Throws<ArgumentException>(() =>
                {
                    socket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);
                });
            }
        }
开发者ID:eerhardt,项目名称:corefx,代码行数:14,代码来源:DualModeSocketTest.cs

示例10: Socket_BeginReceiveFromV4IPEndPointFromV4Client_Throws

        [Fact] // Base case
        // "The supplied EndPoint of AddressFamily InterNetwork is not valid for this Socket, use InterNetworkV6 instead."
        public void Socket_BeginReceiveFromV4IPEndPointFromV4Client_Throws()
        {
            Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
            socket.DualMode = false;

            EndPoint receivedFrom = new IPEndPoint(IPAddress.Loopback, UnusedPort);
            Assert.Throws<ArgumentException>(() =>
            {
                socket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);
            });
        }
开发者ID:eerhardt,项目名称:corefx,代码行数:13,代码来源:DualModeSocketTest.cs

示例11: SocketUdpServer

            public SocketUdpServer(IPAddress address, bool dualMode, out int port)
            {
                _server = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                if (dualMode)
                {
                    _server.DualMode = dualMode;
                }

                port = _server.BindToAnonymousPort(address);

                IPAddress remoteAddress = address.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any;
                EndPoint remote = new IPEndPoint(remoteAddress, 0);
                _server.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref remote, Received, null);
            }
开发者ID:rainersigwald,项目名称:corefx,代码行数:14,代码来源:DualModeSocketTest.cs

示例12: BeginReceiveFrom_Helper

        private void BeginReceiveFrom_Helper(IPAddress listenOn, IPAddress connectTo)
        {
            using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                serverSocket.ReceiveTimeout = 500;
                int port = serverSocket.BindToAnonymousPort(listenOn);

                EndPoint receivedFrom = new IPEndPoint(connectTo, port);
                IAsyncResult async = serverSocket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);

                IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

                SocketUdpClient client = new SocketUdpClient(serverSocket, connectTo, port);
                bool success = async.AsyncWaitHandle.WaitOne(500);
                if (!success)
                {
                    throw new TimeoutException();
                }

                receivedFrom = new IPEndPoint(connectTo, port);
                int received = serverSocket.EndReceiveFrom(async, ref receivedFrom);

                Assert.Equal(1, received);
                Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));

                remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
            }
        }
开发者ID:nnyamhon,项目名称:corefx,代码行数:32,代码来源:DualModeSocketTest.cs

示例13: Listen

 public void Listen(int Port)
 {
     socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     try
     {
         socket.Bind(new IPEndPoint(IPAddress.Parse("0"), Port));
         socket.BeginReceiveFrom(ReceiveBuffer, 0, ReceiveBuffer.Length, SocketFlags.None, ref RemoteEP, new AsyncCallback(ReceiveFrom_Callback), socket);
     }
     catch
     {
         if (OnBindFail != null)
         {
             OnBindFail();
         }
     }
 }
开发者ID:hhrhzy,项目名称:mywork,代码行数:16,代码来源:Network.cs


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