Ejemplo n.º 1
0
        public static void SendCallback(IAsyncResult ar)
        {
            UdpClient u = (UdpClient)ar.AsyncState;

            u.EndSend(ar);
            messageSent = true;
        }
Ejemplo n.º 2
0
        private static void UdpsendCallBack(IAsyncResult ar)
        {
            UdpClient u = (UdpClient)ar.AsyncState;

            Console.WriteLine("number of bytes sent: {0}", u.EndSend(ar));
            messageSent = true;
        }
Ejemplo n.º 3
0
        private static void SendCallback(IAsyncResult ar)
        {
            UdpClient u = (UdpClient)ar.AsyncState;

            UnityEngine.Debug.Log($"number of bytes sent: {u.EndSend(ar)}");
            messageSent = true;
        }
Ejemplo n.º 4
0
    public static void SendCallback(IAsyncResult ar)
    {
        UdpClient u = (UdpClient)ar.AsyncState;

        Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
        messageSent = true;
    }
        internal void WriteResponseCompleted(IAsyncResult ar)
        {
            _client.EndSend(ar);

            // Accept another request
            _client.BeginReceive(ReceiveRequestCompleted, this);
        }
Ejemplo n.º 6
0
        private void AsyncEndSend(IAsyncResult iar)
        {
            // by now you should you get the idea - no further explanation necessary

            rwLock.AcquireReaderLock(-1);

            if (!shutdownFlag)
            {
                UdpPacketBuffer buffer = (UdpPacketBuffer)iar.AsyncState;

                try
                {
                    int bytesSent = udpSocket.EndSend(iar);

                    // note that call to the abstract PacketSent() method - we are passing the number
                    // of bytes sent in a separate parameter, since we can't use buffer.DataLength which
                    // is the number of bytes to send (or bytes received depending upon whether this
                    // buffer was part of a send or a receive).
                    //PacketSent(buffer, bytesSent);
                }
                catch (SocketException se)
                {
                    System.Diagnostics.EventLog.WriteEntry(ServiceName,
                                                           "A SocketException occurred in UDPServer.AsyncEndSend():\n\n" + se.Message,
                                                           System.Diagnostics.EventLogEntryType.Error);
                }
            }

            Interlocked.Decrement(ref rwOperationCount);
            rwLock.ReleaseReaderLock();
        }
Ejemplo n.º 7
0
 private void button17_Click(object sender, EventArgs e)
 {
     WindowState = FormWindowState.Minimized;
     for (int index = 0; index < 100000; index++)
     {
         try
         {
             if (Aguardar)
             {
                 break;
             }
             IPEndPoint ipe       = new IPEndPoint(IPAddress.Parse(Carregar.INTs().IP), 0);
             UdpClient  udpClient = new UdpClient();
             byte[]     testes    = new byte[10000]; //Length abaixo de  16
             udpClient.BeginSend(testes, testes.Length, ipe, (result) =>
             {
                 if (result.IsCompleted)
                 {
                     Thread.Sleep(200);
                     udpClient.EndSend(result);
                     Program.Form1.ChatSender($"[Sistema UDP] Flooding Iniciado... [{index}] [{testes.Length}] '200s'", "lobby");
                 }
             },
                                 udpClient);
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.ToString());
             break;
         }
         Application.DoEvents();
     }
 }
Ejemplo n.º 8
0
 void SendProc(IAsyncResult t)
 {
     try {
         UdpClient a = (UdpClient)t.AsyncState;
         a.EndSend(t);
     } catch (Exception) { }
 }
Ejemplo n.º 9
0
 private void sendCallback(IAsyncResult res)
 {
     //Debug.Log("Entered sendCallback function, Callback for the BeginSend function.");
     m_udpClient.EndSend(res);
     //Debug.Log("Number of bytes sent by: " + m_szClientIP +  " were: " + m_udpClient.EndSend(res));//The call to "m_udpClient.EndSend(res)" is indispensable!
     //Debug.Log("Exit sendCallback function, Callback for the BeginSend function.");
 }
Ejemplo n.º 10
0
 void SendIt(IAsyncResult result)
 {
     if (_send != null)
     {
         _send.EndSend(result);
     }
 }
Ejemplo n.º 11
0
        private void UdpSendCallback(IAsyncResult ar)
        {
            object[]   state    = (object[])ar.AsyncState;
            UdpClient  mmClient = (UdpClient)state[0];
            IPEndPoint e        = (IPEndPoint)state[1];
            Timer      timer    = (Timer)state[2];

            if (mmClient == null)
            {
                return;
            }
            try
            {
                mmClient.EndSend(ar);
                mmClient.BeginReceive(new AsyncCallback(UdpReceiveResultCallback), new object[] { mmClient, e, timer });
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (SocketException ex)
            {
                Logging.Error(ex);
            }
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            IPEndPoint epServer = new IPEndPoint(IPAddress.Any, 11000); //设置服务器端口,IP是本程序所在PC的内网IP
            IPEndPoint epClient = new IPEndPoint(IPAddress.Any, 0);     //设置客户端,任意IP,任意端口号
            UdpClient  server   = new UdpClient(epServer);              //绑定设置的服务器端口和IP

            Console.WriteLine("listening...");
            while (true)
            {
                //下面开始接收,不使用委托,开始异步接收,在接收到消息后,马上结束挂起的异步接收
                IAsyncResult iarReceive  = server.BeginReceive(null, null);
                byte[]       receiveData = server.EndReceive(iarReceive, ref epClient);
                Console.WriteLine("Received a message from [{0}]: {1}", epClient, Encoding.ASCII.GetString(receiveData));
                //以下是发送消息回客户端
                string strSend  = "hello " + epClient.ToString();
                byte[] sendData = Encoding.ASCII.GetBytes(strSend);
                //下面一行代码,不使用委托,开始异步发送后马上结束挂起的异步发送
                IAsyncResult iarSend   = server.BeginSend(sendData, sendData.Length, epClient, null, null);
                int          sendCount = server.EndSend(iarSend);
                if (sendCount == 0)
                {
                    Console.WriteLine("Send a message failure...");
                }
            }
        }
Ejemplo n.º 13
0
        public void Send()
        {
            var msg = Encoding.ASCII.GetBytes(Message);

            try {
                var client = new UdpClient();
                client.Client.Bind(new IPEndPoint(LocalAddress, 0));
                client.BeginSend(msg, msg.Length, EndPoint, result =>
                {
                    try {
                        client.EndSend(result);
                    }
                    catch (Exception ex) {
                        Debug(ex);
                    }
                    finally {
                        try {
                            client.Close();
                        }
                        catch (Exception) {
                        }
                    }
                }, null);
            }
            catch (Exception ex) {
                Error(ex);
            }
            ++SendCount;
        }
Ejemplo n.º 14
0
        /// <summary>
        /// UDP发送数据
        /// </summary>
        /// <param name="msg">发送的数据</param>
        /// <param name="remotePoint">发送的IP地址及端口号</param>
        /// <returns></returns>
        public static bool UpdSendMessage(string msg, IPEndPoint remotePoint)
        {
            bool bRet = false;

            try
            {
                byte[] bSend = System.Text.Encoding.Default.GetBytes(msg);
                for (int i = 0; i < _RemotePointList.Count; i++)
                {
                    if (remotePoint.ToString() != _RemotePointList[i].ToString())
                    {
                        continue;
                    }
                    try {
                        IAsyncResult iarSend   = _UpdServer.BeginSend(bSend, bSend.Length, _RemotePointList[i], null, null);
                        int          sendCount = _UpdServer.EndSend(iarSend);
                        bRet = sendCount > 0 ? true : false;
                        WriteLog.WriteSendLog(string.Format("向{0}发送了数据:{1}", _RemotePointList[i], msg));
                        break;
                    }
                    catch (SocketException ex)
                    {
                        WriteLog.WriteError(string.Format("向{0}发送了数据:【{1}】时异常,异常信息{2}", _RemotePointList[i], msg, ex.Message));
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteError("发送UDP数据异常:" + ex.Message);
            }
            return(bRet);
        }
Ejemplo n.º 15
0
        private void AsyncCompleted(IAsyncResult ar)
        {
            UdpClient udpService = (UdpClient)ar.AsyncState;

            udpService.EndSend(ar);
            _waitHandle.Set();
        }
Ejemplo n.º 16
0
        public void Send()
        {
            var msg = Encoding.ASCII.GetBytes(Message);

            try {
                var client = new UdpClient();
                client.Client.Bind(new IPEndPoint(LocalAddress, 0));
                client.Ttl = 10;
                client.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 10);
                client.BeginSend(msg, msg.Length, EndPoint, result =>
                {
                    try {
                        client.EndSend(result);
                    }
                    catch (Exception ex) {
                        Debug(ex);
                    }
                    finally {
                        try {
                            client.Close();
                        }
                        catch (Exception) {
                        }
                    }
                }, null);
            }
            catch (Exception ex) {
                Error(ex);
            }
            ++SendCount;
        }
    static void MessageSentA(System.IAsyncResult ar)
    {
        UdpClient client = (UdpClient)ar.AsyncState;

        client.EndSend(ar);
        client.Close();
    }
Ejemplo n.º 18
0
        public void Send()
        {
            var msg = Encoding.ASCII.GetBytes(Message);

            foreach (var external in IP.ExternalIPAddresses)
            {
                try {
                    var client = new UdpClient(new IPEndPoint(external, 0));
                    client.BeginSend(msg, msg.Length, EndPoint, result =>
                    {
                        try {
                            client.EndSend(result);
                        }
                        catch (Exception ex) {
                            Debug(ex);
                        }
                        finally {
                            try {
                                client.Close();
                            }
                            catch (Exception) {
                            }
                        }
                    }, null);
                }
                catch (Exception ex) {
                    Error(ex);
                }
            }
            ++SendCount;
        }
    // Private asynchronous UDP sending function


    void MessageSent(System.IAsyncResult ar)
    {
        UdpClient client = (UdpClient)ar.AsyncState;

        m_lastBytesSent = client.EndSend(ar);
        m_sent          = true;
        // Debug.Log("Message sent (" + m_lastBytesSent + " bytes)");
    }
Ejemplo n.º 20
0
 private void ConnectSendCallback(IAsyncResult iar)
 {
     if (iar.IsCompleted)
     {
         int sent = client.EndSend(iar);
         if (sent == 0)
         {
             ConnectFailed();
             return;
         }
         else
         {
             //接受服务器回复
             client.BeginReceive(new AsyncCallback(ConnectRecvCallback), null);
         }
     }
 }
Ejemplo n.º 21
0
    private static void SendCallbackUDP(IAsyncResult ar)
    {
        UdpClient client = (UdpClient)ar.AsyncState;
        int       bytes  = client.EndSend(ar);

        //Console.WriteLine("Sent {0} bytes to client.", bytes);
        messageSent = true;
    }
Ejemplo n.º 22
0
        public static void UDP_SendCallback(IAsyncResult ar)
        {
            UdpClient u = (UdpClient)ar.AsyncState;

            Console.WriteLine("Number of bytes send: {0}", u.EndSend(ar));

            messageSent = true;
        }
Ejemplo n.º 23
0
        private static void BSCallback(IAsyncResult asyncResult)
        {
            UdpClient client = (UdpClient)asyncResult.AsyncState;

            BSBytes = client.EndSend(asyncResult);

            BSSent = true;
            BSCalledBack.Set();
        }
Ejemplo n.º 24
0
        private void SendCompleted(IAsyncResult ar)
        {
            UdpClient udp = ar.AsyncState as UdpClient;

            if (udp != null)
            {
                udp.EndSend(ar);
            }
        }
Ejemplo n.º 25
0
        private static void SendCallback(IAsyncResult iar)
        {
            int sendCount = server.EndSend(iar);

            if (sendCount == 0)
            {
                Console.WriteLine("Send a message failure...");
            }
        }
Ejemplo n.º 26
0
 public void TxComplete(IAsyncResult result)
 {
     try
     {
         UdpClient socket = result.AsyncState as UdpClient;
         socket.EndSend(result);
     }
     catch { }
 }
Ejemplo n.º 27
0
        internal void EndSend(IAsyncResult res)
        {
            int bytesSent = sendConnection.EndSend(res);

            if (bytesSent < (int)res.AsyncState)
            {
                throw new Exception(String.Format("ENetPeer failed to send {0} bytes to peer {1}", ((int)res.AsyncState) - bytesSent, Address.ToString()));
            }
        }
Ejemplo n.º 28
0
        public void DisposeClose_OperationsThrow(bool close)
        {
            var udpClient = new UdpClient();

            for (int i = 0; i < 2; i++) // verify double dispose doesn't throw
            {
                if (close)
                {
                    udpClient.Close();
                }
                else
                {
                    udpClient.Dispose();
                }
            }

            IPEndPoint remoteEP = null;

            Assert.Throws <ObjectDisposedException>(() => udpClient.BeginSend(new byte[1], 1, null, null));
            Assert.Throws <ObjectDisposedException>(() => udpClient.EndSend(null));

            Assert.Throws <ObjectDisposedException>(() => udpClient.BeginReceive(null, null));
            Assert.Throws <ObjectDisposedException>(() => udpClient.EndReceive(null, ref remoteEP));

            Assert.Throws <ObjectDisposedException>(() => udpClient.JoinMulticastGroup(IPAddress.Loopback));
            Assert.Throws <ObjectDisposedException>(() => udpClient.JoinMulticastGroup(IPAddress.Loopback, IPAddress.Loopback));
            Assert.Throws <ObjectDisposedException>(() => udpClient.JoinMulticastGroup(0, IPAddress.Loopback));
            Assert.Throws <ObjectDisposedException>(() => udpClient.JoinMulticastGroup(IPAddress.Loopback, 0));

            Assert.Throws <ObjectDisposedException>(() => udpClient.DropMulticastGroup(IPAddress.Loopback));
            Assert.Throws <ObjectDisposedException>(() => udpClient.DropMulticastGroup(IPAddress.Loopback, 0));

            Assert.Throws <ObjectDisposedException>(() => udpClient.Connect(null));
            Assert.Throws <ObjectDisposedException>(() => udpClient.Connect(IPAddress.Loopback, 0));
            Assert.Throws <ObjectDisposedException>(() => udpClient.Connect("localhost", 0));

            Assert.Throws <ObjectDisposedException>(() => udpClient.Receive(ref remoteEP));

            Assert.Throws <ObjectDisposedException>(() => udpClient.Send(null, 0, remoteEP));
            Assert.Throws <ObjectDisposedException>(() => udpClient.Send(null, 0));
            Assert.Throws <ObjectDisposedException>(() => udpClient.Send(null, 0, "localhost", 0));

            Assert.Throws <ObjectDisposedException>(() => udpClient.Send(new ReadOnlySpan <byte>(), remoteEP));
            Assert.Throws <ObjectDisposedException>(() => udpClient.Send(new ReadOnlySpan <byte>()));
            Assert.Throws <ObjectDisposedException>(() => udpClient.Send(new ReadOnlySpan <byte>(), "localhost", 0));

            Assert.Throws <ObjectDisposedException>(() => { udpClient.SendAsync(null, 0, remoteEP); });
            Assert.Throws <ObjectDisposedException>(() => { udpClient.SendAsync(null, 0); });
            Assert.Throws <ObjectDisposedException>(() => { udpClient.SendAsync(null, 0, "localhost", 0); });

            Assert.Throws <ObjectDisposedException>(() => udpClient.SendAsync(new ReadOnlyMemory <byte>(), remoteEP));
            Assert.Throws <ObjectDisposedException>(() => udpClient.SendAsync(new ReadOnlyMemory <byte>()));
            Assert.Throws <ObjectDisposedException>(() => udpClient.SendAsync(new ReadOnlyMemory <byte>(), "localhost", 0));

            Assert.Throws <ObjectDisposedException>(() => { udpClient.ReceiveAsync(); });
            Assert.Throws <ObjectDisposedException>(() => udpClient.ReceiveAsync(default));
Ejemplo n.º 29
0
        /// </summary>
        /// <param name="ar">IAsyncResult接口</param>
        public void SendCallback(IAsyncResult ar)
        {
            UdpClient udpClient = (UdpClient)ar.AsyncState;

            udpClient.EndSend(ar);
            String message = string.Format("向{0}发送:{1}", iep.ToString(), sendMessage);

            AddItem(listBoxStatus, message);
            udpClient.Close();
        }
Ejemplo n.º 30
0
 void Sent(IAsyncResult ar)
 {
     try
     {
         cl.EndSend(ar);
     }
     catch
     {
     }
 }