C # UDP Send and Receive Request tool class introduction

Source: Internet
Author: User
Initialization

listeningport = Int. Parse (configurationmanager.appsettings["Listeningport"]); sendingport = Int. Parse (configurationmanager.appsettings["Sendingport"]); SENDINGIP = configurationmanager.appsettings["Sendingip"];

Listening:

public static void Listen () {task.run () + = {var done = false;                var listener = new UdpClient (listeningport);                var groupep = new IPEndPoint (Ipaddress.any, Listeningport);                String Received_data;                Byte[] Receive_byte_array; try {_log.                    Error ("########### #Service started###########"); while (true) {Receive_byte_array = listener.                        Receive (ref GROUPEP);                        Console.WriteLine ("Received a broadcast from {0}", groupep.tostring ()); Received_data = Encoding.UTF8.GetString (receive_byte_array, 0, Receive_byte_array.                        Length);                    Parsecommand (Received_data); }} catch (Exception e) {_log.                    Error (e); Console.WriteLine (E.tostring ());               } _log.            Error ("########### #Service stopped###########");        }); }

Send:

public static void SendCommand (string xmlcmd)        {            try            {                var sending_socket = new Socket ( AddressFamily.InterNetwork, Sockettype.dgram, protocoltype.udp);                var sending_end_point = new IPEndPoint (Ipaddress.parse (SENDINGIP), sendingport);                var send_buffer = Encoding.UTF8.GetBytes (xmlcmd);                Sending_socket. SendTo (Send_buffer, sending_end_point);                _log. Info ("[COMMAND SENT]:" + xmlcmd);            }            catch (Exception ex)            {                _log. Error (ex);            }        }


Above is the C # UDP send and Receive Request tool class introduction content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.