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


C# UdpClient.Send方法代码示例

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


在下文中一共展示了UdpClient.Send方法的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: StartBroadcast

    IEnumerator StartBroadcast()
    {
        // multicast send setup
        udp_client = new UdpClient ();
        udp_client.JoinMulticastGroup (group_address);
        IPEndPoint remote_end = new IPEndPoint (group_address, startup_port);

        // sends multicast
        while (true)
        {
            byte[] buffer = Encoding.ASCII.GetBytes ("GameServer");
            udp_client.Send (buffer, buffer.Length, remote_end);

            yield return new WaitForSeconds (1);
        }
    }
开发者ID:philipcass,项目名称:NetworkingDemo,代码行数:16,代码来源:DiscoverMonoBehaviour.cs

示例3: 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

示例4: exportData

    private void exportData(ref UdpClient client, ref IPEndPoint anyIP)
    {
        byte[] data;
        string text;

        text = "SOT"; // start of table
        text = text + System.Environment.NewLine;
        data = System.Text.Encoding.ASCII.GetBytes(text);
        client.Send(data, data.Length, anyIP);

        foreach (var ptr in timeGraphScript.dateTime_val_dic) {
            text = ptr.Key.ToString("yyyy/MM/dd HH:mm:ss");
            text = text + ",";
            text = text + ptr.Value.ToString();
            text = text + System.Environment.NewLine;

            data = System.Text.Encoding.ASCII.GetBytes(text);
            client.Send(data, data.Length, anyIP);
        }

        text = "EOT"; // end of table
        text = text + System.Environment.NewLine;
        data = System.Text.Encoding.ASCII.GetBytes(text);
        client.Send(data, data.Length, anyIP);
    }
开发者ID:yasokada,项目名称:unity-150908-udpTimeGraph,代码行数:25,代码来源:udpReceiverScript.cs

示例5: Main

    public static void Main()
    {
        byte[] data = new byte[1024];
          IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9876);
          UdpClient newsock = new UdpClient(ipep);

          Console.WriteLine("Waiting for a client...");

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

          data = newsock.Receive(ref sender);

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

          string msg = Encoding.ASCII.GetString(data, 0, data.Length);
          data = Encoding.ASCII.GetBytes(msg);
          newsock.Send(data, data.Length, sender);

          while(true){
         data = newsock.Receive(ref sender);

         Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
         newsock.Send(data, data.Length, sender);
          }
    }
开发者ID:Kaerit,项目名称:IsoMonksComm,代码行数:26,代码来源:server.cs

示例6: Main

    public static void Main()
    {
        string addr = "127.0.0.1";
        int port = 7980;
        short cmd = 1;
        short seq = 0;
        string msg = "Hello";
        byte[] payload = Encoding.ASCII.GetBytes(msg);
        byte[] packet = Packet.Create(cmd, seq, payload);

        UdpClient udp = new UdpClient();
        var ip = IPAddress.Parse(addr);
        IPEndPoint ep = new IPEndPoint(ip, port);
        udp.Connect(ep);

        // send
        udp.Send(packet, packet.Length);

        // receive
        bool done = false;
        while (!done) {
            if (udp.Available <= 0) {
                IPEndPoint ep2 = new IPEndPoint(0, 0);
                byte[] packet2 = udp.Receive(ref ep2);

                Console.WriteLine("packet size: {0}", packet2.Length);

                Dictionary<string, object> parsed = Packet.Parse(packet2);
                foreach (KeyValuePair<string, object> item in parsed) {
                    Console.WriteLine("Received:{0} = {1}", item.Key, item.Value);
                }
                done = true;
            }
        }
    }
开发者ID:voltrue2,项目名称:gracenode,代码行数:35,代码来源:Test.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: SendMes

    private static void SendMes(string datagram)
    {
        // Создаем UdpClient
        UdpClient sender = new UdpClient();

        // Создаем endPoint по информации об удаленном хосте
        IPEndPoint endPoint = new IPEndPoint(remoteIPAddress, remotePort);

        try
        {
            // Преобразуем данные в массив байтов
            byte[] bytes = Encoding.UTF8.GetBytes(datagram);

            // Отправляем данные
            sender.Send(bytes, bytes.Length, endPoint);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Возникло исключение: " + ex.ToString() + "\n  " + ex.Message);
        }
        finally
        {
            // Закрыть соединение
            sender.Close();
        }
    }
开发者ID:master404,项目名称:-ClientServer,代码行数:26,代码来源:Program.cs

示例9: WakeUp

 public static void WakeUp(byte[] mac, string ip = "255.255.255.255", int port = 9)
 {
     using (var client = new UdpClient {EnableBroadcast = true})
     {
         client.Send(CreateMagickPacket(mac), MagickPacketSize, ip, port);
     }
 }
开发者ID:MisterHoker,项目名称:NetworkLibrary,代码行数:7,代码来源:WakeOnLan.cs

示例10: 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

示例11: broadCastCloudRequests

 public void broadCastCloudRequests(bool continuous)
 {
     UdpClient udp = new UdpClient ();
     string message = CloudMessage.createRequestMessage (continuous ? 1 : 0);
     byte[] data = Encoding.UTF8.GetBytes (message);
     IPEndPoint remoteEndPoint = new IPEndPoint (IPAddress.Broadcast, TrackerProperties.Instance.listenPort + 1);
     udp.Send (data, data.Length, remoteEndPoint);
 }
开发者ID:mauriciosousa,项目名称:creepytracker,代码行数:8,代码来源:Tracker.cs

示例12: 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

示例13: broadcast

	void broadcast(string message) {
		// multicast send setup
		udp_client = new UdpClient ();
		udp_client.JoinMulticastGroup (group_address);
		remote_end = new IPEndPoint (group_address, port);

		byte[] buffer = Encoding.ASCII.GetBytes (message);
		udp_client.Send (buffer, buffer.Length, remote_end);
	}
开发者ID:bosung90,项目名称:MadScientist,代码行数:9,代码来源:CMMulticastSender.cs

示例14: Main

 static void Main(string[] args)
 {
     UdpClient client = new UdpClient("127.0.0.1", 0);
     client.Receive += (o, e) => {
         Console.WriteLine(Encoding.UTF8.GetString(
             e.Data, 0, e.Count));
     };
     client.Send("http://www.163.com", "127.0.0.1", 8089);
     Console.Read();
 }
开发者ID:priceLiu,项目名称:controlServer,代码行数:10,代码来源:Program.cs

示例15: 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


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