当前位置: 首页>>代码示例>>C#>>正文


C# UdpClient.Close方法代码示例

本文整理汇总了C#中UdpClient.Close方法的典型用法代码示例。如果您正苦于以下问题:C# UdpClient.Close方法的具体用法?C# UdpClient.Close怎么用?C# UdpClient.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UdpClient的用法示例。


在下文中一共展示了UdpClient.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

    public static void Main()
    {
        byte[] data = new byte[1024];
          	string input, stringData;
          	UdpClient server = new UdpClient("127.0.0.1", 9877);

        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 9876);

        string welcome = "Hello, are you there?";
        data = Encoding.ASCII.GetBytes(welcome);
        server.Send(data, data.Length);

        data = server.Receive(ref sender);

        Console.WriteLine("Message received from {0}:", sender.ToString());
        stringData = Encoding.ASCII.GetString(data, 0, data.Length);
        Console.WriteLine(stringData);

        while(true){
            input = Console.ReadLine();
            if (input == "exit")
                break;

                server.Send(Encoding.ASCII.GetBytes(input), input.Length);
             	data = server.Receive(ref sender);
        }
        Console.WriteLine("Stopping client");
        server.Close();
    }
开发者ID:Kaerit,项目名称:IsoMonksComm,代码行数:29,代码来源:client.cs

示例2: StartListener

    private static void StartListener()
    {
        //Initiate UDP server
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        UdpClient listener = new UdpClient(ListenPort);
        IPEndPoint groupEp = new IPEndPoint(IPAddress.Any,ListenPort);
        IPEndPoint responseEp;
        string receivedCommand;

        try
        {
            while (true)
            {
                //Wait for incoming command
                Console.WriteLine("Waiting for command");
                byte[] bytes = listener.Receive( ref groupEp);
                receivedCommand = Encoding.ASCII.GetString(bytes,0,bytes.Length);
                Console.WriteLine("Received command: " + receivedCommand + " from " + groupEp.Address);

                //Send matching response
                responseEp = new IPEndPoint(groupEp.Address, ListenPort);
                if (receivedCommand == "U" || receivedCommand ==  "u")
                {
                    using (StreamReader sr = new StreamReader ("/proc/uptime"))
                    {
                        String line = sr.ReadToEnd();
                        Console.WriteLine("Sending uptime: " + line);

                        byte[] sendbuf = Encoding.ASCII.GetBytes(line);
                        s.SendTo(sendbuf, responseEp);
                    }
                }
                else if(receivedCommand == "L" || receivedCommand ==  "l")
                {
                    using (StreamReader sr = new StreamReader ("/proc/loadavg"))
                    {
                        String line = sr.ReadToEnd();
                        Console.WriteLine("Sending load average: " + line);

                        byte[] sendbuf = Encoding.ASCII.GetBytes(line);
                        s.SendTo(sendbuf, responseEp);
                    }
                }
                else
                {
                    Console.WriteLine("Command " + receivedCommand + " not found\n");
                    byte[] sendbuf = Encoding.ASCII.GetBytes("Input not recognized, please try again!");
                    s.SendTo(sendbuf, responseEp);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            listener.Close();
        }
    }
开发者ID:BjornNorgaard,项目名称:I4IKN,代码行数:60,代码来源:ListenerCode.cs

示例3: Main

 public static int Main()
 {
     bool done = false;
     UdpClient listener = new UdpClient(listenPort);
     IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
     string received_data;
     byte[] receive_byte_array;
     try
     {
         while (!done)
         {
             Console.WriteLine("Waiting for broadcast");
             // this is the line of code that receives the broadcase message.
             // It calls the receive function from the object listener (class UdpClient)
             // It passes to listener the end point groupEP.
             // It puts the data from the broadcast message into the byte array
             // named received_byte_array.
             // I don't know why this uses the class UdpClient and IPEndPoint like this.
             // Contrast this with the talker code. It does not pass by reference.
             // Note that this is a synchronous or blocking call.
             receive_byte_array = listener.Receive(ref groupEP);
             Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
             received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
             Console.WriteLine("data follows \n{0}\n\n", received_data);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
     listener.Close();
     return 0;
 }
开发者ID:Lrss,项目名称:P3,代码行数:33,代码来源:Program.cs

示例4: StartListener

 private static void StartListener()
 {
     bool done = false;
     IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
     UdpClient listener = new UdpClient(groupEP);
     Log.Notice("Listener", "Waiting for broadcast");
     try
     {
         while (!done)
         {
             byte[] bytes = listener.Receive(ref groupEP);
             Log.Info("Listener", "Client " + groupEP.ToString() + " is trying to connect");
             listener.Connect(groupEP);
             Log.Succes("Listener", "Listener connected to client " + groupEP.ToString());
             done = true;
             //TODO - rest of district server connecting
         }
     }
     catch (Exception e)
     {
         FrameWork.Logger.Log.Error("Listener", e.ToString());
     }
     finally
     {
         listener.Close();
     }
 }
开发者ID:harleyknd1,项目名称:rAPB,代码行数:27,代码来源:Listener.cs

示例5: ThreadFuncReceive

 static void ThreadFuncReceive()
 {
     try
     {
         while (true)
         {
             //подключение к локальному хосту
             UdpClient uClient = new UdpClient(LocalPort);
             IPEndPoint ipEnd = null;
             //получание дейтаграммы
             byte[] responce = uClient.Receive(ref ipEnd);
             //преобразование в строку
             string strResult = Encoding.Unicode.GetString(responce);
             Console.ForegroundColor = ConsoleColor.Green;
             //вывод на экран
             Console.WriteLine(strResult);
             Console.ForegroundColor = ConsoleColor.Red;
             uClient.Close();
         }
     }
     catch (SocketException sockEx)
     {
         Console.WriteLine("Ошибка сокета: " + sockEx.Message);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Ошибка : " + ex.Message);
     }
 }
开发者ID:xs2ranjeet,项目名称:13ns9-1spr,代码行数:29,代码来源:Program.cs

示例6: Main

   public static void Main()
   {
      byte[] data = new byte[1024];
      string input, stringData;
      UdpClient udpClient = new UdpClient("127.0.0.1", 9999);

      IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

      string welcome = "Hello";
      data = Encoding.ASCII.GetBytes(welcome);
      udpClient.Send(data, data.Length);

      data = udpClient.Receive(ref sender);

      Console.WriteLine("Message received from {0}:", sender.ToString());
      stringData = Encoding.ASCII.GetString(data, 0, data.Length);
      Console.WriteLine(stringData);

      while(true)
      {
         input = Console.ReadLine();
         udpClient.Send(Encoding.ASCII.GetBytes(input), input.Length);
         data = udpClient.Receive(ref sender);
         stringData = Encoding.ASCII.GetString(data, 0, data.Length);
         Console.WriteLine(stringData);
      }
      udpClient.Close();
   }
开发者ID:EdiCarlos,项目名称:MyPractices,代码行数:28,代码来源:udpclient.cs

示例7: FuncRcvData

    private void FuncRcvData()
    {
        client = new UdpClient (port);
        client.Client.ReceiveTimeout = 300; // msec
        client.Client.Blocking = false;
        while (stopThr == false) {
            try {
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = client.Receive(ref anyIP);
                string text = Encoding.ASCII.GetString(data);
                lastRcvd = text;

                if (lastRcvd.Length > 0) {
                    Thread.Sleep(delay_msec);
                    client.Send(data, data.Length, anyIP); // echo
                }
            }
            catch (Exception err)
            {
                //              print(err.ToString());
            }

            // without this sleep, on adnroid, the app will not start (freeze at Unity splash)
            Thread.Sleep(20); // 200
        }
        client.Close ();
    }
开发者ID:yasokada,项目名称:unity-150829-udpEcho,代码行数:27,代码来源:UdpEchoServer.cs

示例8: procComm

    void procComm()
    {
        port = getPort();
        string ipadr = getIpadr ();

        client = new UdpClient ();

        // send
        string sendstr = IFmsg.text + System.Environment.NewLine;
        byte[] data = ASCIIEncoding.ASCII.GetBytes (sendstr);
        client.Send (data, data.Length, ipadr, port);

        // receive
        client.Client.ReceiveTimeout = 2000; // msec
        IPEndPoint remoteIP = new IPEndPoint(IPAddress.Any, 0);
        lastRcvd = "";
        try {
            data = client.Receive (ref remoteIP);
            if (data.Length > 0) {
                string text = Encoding.ASCII.GetString (data);
                lastRcvd = text;
            }
        } catch (Exception err) {
        }

        client.Close ();
    }
开发者ID:yasokada,项目名称:unity-150830-udpSender,代码行数:27,代码来源:SendButtonScript.cs

示例9: StartListener

    public void StartListener()
    {
        bool done = false;

        UdpClient listener = new UdpClient(listenPort);
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);

        try
        {
            while (!done)
            {
                Console.WriteLine("Waiting for broadcast");
                byte[] bytes = listener.Receive( ref groupEP);

                Console.WriteLine("Received broadcast from {0} :\n {1}\n",
                    groupEP.ToString(),
                    Encoding.ASCII.GetString(bytes,0,bytes.Length));
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            listener.Close();
        }
    }
开发者ID:eraumotorsports,项目名称:carLogger,代码行数:29,代码来源:BroadcastListener.cs

示例10: Send

 public void Send()
 {
     UdpClient client = new UdpClient();
     IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 15000);
     byte[] bytes = Encoding.ASCII.GetBytes("Foo");
     client.Send(bytes, bytes.Length, ip);
     client.Close();
 }
开发者ID:Supaidaman,项目名称:MasterServerController,代码行数:8,代码来源:Sender.cs

示例11: Broadcast

 private void Broadcast()
 {
     if (IsBroadcasting()) {
         UdpClient client = new UdpClient();
         IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, port);
         byte[] data = Encoding.ASCII.GetBytes(message);
         client.Send(data, data.Length, ip);
         client.Close();
     }
 }
开发者ID:RUGSoftEng,项目名称:Team-7,代码行数:10,代码来源:LocalBroadcaster.cs

示例12: Main

	public static void Main ()
	{
		var ip = IPAddress.Parse ("239.255.255.250");
		var bytes = new byte [] {60, 70, 75};
		UdpClient udp = new UdpClient ();
		udp.Connect (ip, 3802);
		udp.Send (bytes, 3);
		Console.WriteLine ("Sent");
		udp.Close ();
	}
开发者ID:alesliehughes,项目名称:olive,代码行数:10,代码来源:udp-direct-cli.cs

示例13: read_data

    public string read_data(int player, int data_type)
    {
        int offset = data_type*2 - 2 + player;

        UdpClient listener = new UdpClient(port_base+offset);
        listener.Client.ReceiveTimeout = 50;
        IPEndPoint address = new IPEndPoint(IPAddress.Any, port_base+offset);
        byte[] recb;

        try {
            recb = listener.Receive( ref address );
            listener.Close();
            return Encoding.ASCII.GetString(recb);
        } catch ( SocketException e ) {
            e.ToString();
        }

        listener.Close();
        return "silent";
    }
开发者ID:brgmnn,项目名称:van-dyke,代码行数:20,代码来源:KinectSkeletonClient.cs

示例14: Main

	public static void Main ()
	{
		var ip = IPAddress.Parse ("239.255.255.250");
		while (true) {
			UdpClient udp = new UdpClient (3802);
			udp.JoinMulticastGroup (ip, 1);
			IPEndPoint dummy = null;
			udp.Receive (ref dummy);
			Console.WriteLine ("Received");
			udp.DropMulticastGroup (ip);
			udp.Close ();
		}
	}
开发者ID:alesliehughes,项目名称:olive,代码行数:13,代码来源:udp-direct-svc.cs

示例15: ReceiveData

    private void ReceiveData()
    {
        client = new UdpClient(port);
        while (m_keepRunning)
        {
            try
            {

                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);

                byte[] data = client.Receive(ref anyIP);

 

                // Bytes mit der UTF8-Kodierung in das Textformat kodieren.

                string text = Encoding.UTF8.GetString(data);
				
				Debug.Log(text);
				
				string[] words = text.Split(' ');

 				
				Quaternion q = new Quaternion(float.Parse(words[12]),
				                              float.Parse(words[13]),
				                              float.Parse(words[14]),
				                              float.Parse(words[15]));
				Vector3 t = new Vector3(float.Parse(words[18]),
				                        float.Parse(words[19]),
				                        float.Parse(words[20]));

                m_newData = new Pose();
                UbiMeasurementUtils.coordsysemChange(t, ref m_newData.pos);
                UbiMeasurementUtils.coordsysemChange(q, ref m_newData.rot);

            }

            catch (Exception err)

            {

                Debug.Log(err.ToString());

            }

        }
		client.Close();

    }
开发者ID:Ozelotl,项目名称:Portfolio,代码行数:49,代码来源:NetworkPoseSink_LightWeight.cs


注:本文中的UdpClient.Close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。