C # UDP transmission and reception

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UDPTest
{
    /// <summary>
    /// 接受
    /// </summary>
    public class LabelReceiveService
    {
        /// <summary>
        /// 用于UDP发送的网络服务类
        /// </summary>
        static UdpClient udpcRecv = null; 

        Static the IPEndPoint localIpep = null ; 

        ///  <Summary> 
        /// switch: UDP packets in a listening phase is true, otherwise to false
         ///  </ Summary> 
        static  BOOL IsUdpcRecvStart = to false ;
         ///  <Summary> 
        / // thread: continue to monitor UDP packets
         ///  </ the Summary> 
        static the thread thrRecv; 

        public  static  void StartReceive () 
        { 
            IF (! IsUdpcRecvStart) // without listening, starts listening 
            { 
                localIpep = new newThe IPEndPoint (IPAddress.Parse ( " 127.0.0.1 " ), 8899 ); // local IP and port number 
                udpcRecv = new new the UdpClient (localIpep); 
                thrRecv = new new the Thread (ReceiveMessage); 
                thrRecv.Start (); 
                IsUdpcRecvStart = to true ; 
                Console.WriteLine ( " the UDP listener has been started successfully " ); 
            } 
        } 

        public  static  void StopReceive () 
        { 
            IF (IsUdpcRecvStart) 
            {
                thrRecv.Abort (); // must first close this thread, otherwise abnormal 
                udpcRecv.Close (); 
                IsUdpcRecvStart = false ; 
                Console.WriteLine ( " UDP listener has successfully closed " ); 
            } 
        } 

        ///  <the Summary> 
        / // received data
         ///  </ Summary> 
        ///  <param name = "obj"> </ param> 
        Private  static  void ReceiveMessage ( Object obj) 
        { 
            the while (IsUdpcRecvStart) 
            { 
                the try 
                { 
                    byte[] bytRecv = udpcRecv.Receive(ref localIpep);
                    string message = Encoding.UTF8.GetString(bytRecv, 0, bytRecv.Length);
                    Console.WriteLine(string.Format("{0}[{1}]", localIpep, message));

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }
        }

    }
}
    public static void Main(string[] args)
        {
            LabelReceiveService.StartReceive();
            Console.ReadKey();
        }

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UDPSendTest
{
    class Program
    {
        /// <summary>
        /// 用于UDP发送的网络服务类
        /// </summary>
        private static UdpClient udpcSend = null;

        static IPEndPoint localIpep = null;

        public static  void the Main ( String [] args) 
        { 
            localIpep = new new the IPEndPoint (IPAddress.Parse ( " 127.0.0.1 " ), 8888 ); // local IP and port number 
            udpcSend = new new the UdpClient (localIpep); 

 
            // branded transmission 
            String MSG = null ;
             the while (! (MSG = Console.ReadLine ()) = null ) 
            { 
                the Thread thrSend = new new the Thread (the SendMessage); 
                thrSend.Start (MSG); 
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="obj"></param>
        private static void SendMessage(object obj)
        {
            try
            {
                string message = (string)obj;
                byte[] sendbytes = Encoding.Unicode.GetBytes(message);
                IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8899); //Sending the IP address and port number 
                udpcSend.Send (sendbytes, sendbytes.Length, remoteIpep);
                 // udpcSend.Close (); 
            }
             the catch {} 
        } 
         
    } 
}

 

Guess you like

Origin www.cnblogs.com/chxl800/p/12072751.html