UDP Server for C #

Source: Internet
Author: User

Latest optimized version

/*http://www.cnblogs.com/zengqinglei/archive/2013/04/27/3046119.html*/using System;using System.Text; #region Namespaces using system.net;using system.net.sockets;using system.threading;using system.media; #endregionnamespace Socketserverconsole{class Program {#region console main function///<summary>///console main function////            lt;/summary>//<param name= "args" ></param> static void Main (string[] args) {        Udpserver (New IPEndPoint (0, 9167));        } #endregion #region UDP connection///<summary>//UDP connection///</summary>            <param name= "ServerIP" ></param> public static void Udpserver (IPEndPoint serverip) {            SoundPlayer sp = new SoundPlayer ();            bool Thread_flag = true;            Console.WriteLine ("UDP server starts listening" + Serverip.port + "port"); Socket udpserver = new socket (addressfamily.internetwork, Sockettype.dgram, PROTOCOLTYPE.UDP);            Udpserver.bind (ServerIP);            IPEndPoint Ipep = new IPEndPoint (ipaddress.any, 0);            EndPoint Remote = (EndPoint) Ipep; New Thread (() = {while (Thread_flag) {byte[] data = new                    BYTE[1024];                    int length = 0;                    try {length = Udpserver.receivefrom (data, ref Remote);//Accept data from the server } catch (Exception ex) {Console.WriteLine (string. Format ("An exception occurred: {0}", ex.)                        Message));                    Break                    } string datetime = DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss");                    String message = Encoding.UTF8.GetString (data, 0, length); String ipport = (Remote as IPEndPoint). Address.tostring () + ":" + (Remote as IPEndPoint).                    Port.tostring (); Console.WriteLine (String.Format ("{0} received a message from {1}: {2}", DateTime, Ipport, message)); Sp. Soundlocation = Thread.getdomain ().                    BaseDirectory + message; try {sp.                    Playlooping (); } catch (Exception ex) {Console.WriteLine (string. Format ("Error: {0}, Path={1}", ex. Message, sp.                    soundlocation));            }} udpserver.close (); }).            Start (); Console.WriteLine ("\ n \ nyou Press [F4] key to exit.            ");            Consolekey key; while (true) {key = Console.readkey (true).                Key;                    if (key = = consolekey.f4) {Console.WriteLine ("End waiting for UDP data.");                    Thread_flag = false;                Break }}} #endregion}}

  

UDP Server for C #

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.