BeginReceiveMessageFrom() public method

public BeginReceiveMessageFrom ( byte buffer, int offset, int size, SocketFlags socketFlags, EndPoint &remoteEP, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
size int
socketFlags SocketFlags
remoteEP System.Net.EndPoint
callback AsyncCallback
state object
return IAsyncResult
コード例 #1
0
        public static Task <SocketReceiveMessageFromResult> ReceiveMessageFromAsync(
            this Socket socket,
            ArraySegment <byte> buffer,
            SocketFlags socketFlags,
            EndPoint remoteEndPoint)
        {
            return(Task <SocketReceiveMessageFromResult> .Factory.FromAsync(
                       (callback, state) => socket.BeginReceiveMessageFrom(
                           buffer.Array,
                           buffer.Offset,
                           buffer.Count,
                           socketFlags,
                           ref remoteEndPoint,
                           callback,
                           state),
                       asyncResult => {
                IPPacketInformation ipPacket;
                int bytesReceived = socket.EndReceiveMessageFrom(
                    asyncResult,
                    ref socketFlags,
                    ref remoteEndPoint,
                    out ipPacket);

                return new SocketReceiveMessageFromResult()
                {
                    PacketInformation = ipPacket,
                    ReceivedBytes = bytesReceived,
                    RemoteEndPoint = remoteEndPoint,
                    SocketFlags = socketFlags
                };
            },
                       null));
        }
コード例 #2
0
        public static Task <SocketReceiveMessageFromResult> ReceiveMessageFromAsync(
            this Socket socket,
            ArraySegment <byte> buffer,
            SocketFlags socketFlags,
            EndPoint remoteEndPoint)
        {
            var tcs = new StateTaskCompletionSource <SocketFlags, EndPoint, SocketReceiveMessageFromResult>(socket)
            {
                _field1 = socketFlags, _field2 = remoteEndPoint
            };

            socket.BeginReceiveMessageFrom(buffer.Array, buffer.Offset, buffer.Count, socketFlags, ref tcs._field2, iar =>
            {
                var innerTcs = (StateTaskCompletionSource <SocketFlags, EndPoint, SocketReceiveMessageFromResult>)iar.AsyncState;
                try
                {
                    IPPacketInformation ipPacketInformation;
                    int receivedBytes = ((Socket)innerTcs.Task.AsyncState).EndReceiveMessageFrom(iar, ref innerTcs._field1, ref innerTcs._field2, out ipPacketInformation);
                    innerTcs.TrySetResult(new SocketReceiveMessageFromResult
                    {
                        ReceivedBytes     = receivedBytes,
                        RemoteEndPoint    = innerTcs._field2,
                        SocketFlags       = innerTcs._field1,
                        PacketInformation = ipPacketInformation
                    });
                }
                catch (Exception e) { innerTcs.TrySetException(e); }
            }, tcs);
            return(tcs.Task);
        }
コード例 #3
0
        public SharedSocket(int port)
        {
            addressEntries = new List<AddressEntry>();

            this.port = port;

            buffer = new byte[65535];
            receiveEndpoint = new IPEndPoint(IPAddress.Any, 0);

            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            socket.Bind(new IPEndPoint(IPAddress.Any, port));
            socket.BeginReceiveMessageFrom(buffer, 0, buffer.Length, SocketFlags.None, ref receiveEndpoint, OnDataReceived, null);
        }
コード例 #4
0
 System.IAsyncResult Utils.Wrappers.Interfaces.ISocket.BeginReceiveMessageFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref System.Net.EndPoint remoteEP, System.AsyncCallback callback, object state)
 {
     return(InternalSocket.BeginReceiveMessageFrom(buffer, offset, size, socketFlags, ref remoteEP, callback, state));
 }
コード例 #5
0
ファイル: SendArping.cs プロジェクト: LudovicT/Grid-Mapper
        public void Arping( IPAddress ipToSearchFor )
        {
            string ownMAC;
            string ownIP;
            getLocalMacAndIP( out ownIP, out ownMAC );
            char[] aDelimiter = { '.' };
            string[] aIPReso = ipToSearchFor.ToString().Split( aDelimiter, 4 );
            string[] aIPAddr = ownIP.Split( aDelimiter, 4 );

            byte[] oPacket = new byte[] { /*0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                        Convert.ToByte("0x" + ownMAC.Substring(0,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(2,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(4,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(6,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(8,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(10,2), 16),
                        0x08, 0x06,*/ 0x00, 0x01,
                        0x08, 0x00, 0x06, 0x04, 0x00, 0x01,
                        Convert.ToByte("0x" + ownMAC.Substring(0,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(2,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(4,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(6,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(8,2), 16),
                        Convert.ToByte("0x" + ownMAC.Substring(10,2), 16),
                        Convert.ToByte(aIPAddr[0], 10),
                        Convert.ToByte(aIPAddr[1], 10),
                        Convert.ToByte(aIPAddr[2], 10),
                        Convert.ToByte(aIPAddr[3], 10),
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                        Convert.ToByte(aIPReso[0], 10),
                        Convert.ToByte(aIPReso[1], 10),
                        Convert.ToByte(aIPReso[2], 10),
                        Convert.ToByte(aIPReso[3], 10)};
            Socket arpSocket;
            arpSocket = new Socket( System.Net.Sockets.AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw );
            //arpSocket.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true );
            arpSocket.EnableBroadcast = true;
            EndPoint localEndPoint = new IPEndPoint( IPAddress.Any, 0 );
            EndPoint remoteEndPoint = new IPEndPoint( ipToSearchFor, 0 );
            arpSocket.Bind( localEndPoint );
            arpSocket.BeginSendTo( oPacket,0,oPacket.Length,SocketFlags.None, remoteEndPoint, null, this);
            byte[] buffer = new byte[100];
            arpSocket.BeginReceiveMessageFrom( buffer, 0, 100, SocketFlags.None, ref remoteEndPoint, null, this);
        }
コード例 #6
0
        public void StartListening()
        {
            lock (lockobj) {
                ResetStats();

                hasData = true;

                if (socket == null) {
                    receiveEndpoint = new IPEndPoint(IPAddress.Any, 0);

                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    socket.Bind(new IPEndPoint(IPAddress.Any, this.endpoint.Port));
                    socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(this.endpoint.Address));
                    socket.BeginReceiveMessageFrom(buf, 0, buf.Length, SocketFlags.None, ref receiveEndpoint, ReceiveCallback, socket);
                }
            }
        }