回覆列表
  • 1 # 海綿寶寶的彩虹

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Net;

    using System.Net.Sockets;

    private void button1_Click(object sender, EventArgs e)

    {

    /*構建UDP伺服器*/

    /*得到本機IP,設定UDP埠號*/

    IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 10000);

    Socket command_sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

    /*繫結網路地址*/

    command_sock.Bind(ipep);

    Console.WriteLine("這是個UDP伺服器,名字是 {0}", Dns.GetHostName());

    while (true)

    {

    /*接收資訊*/

    command_sock.ReceiveTimeout = 10000; //設定接收超時時間單位毫秒

    try

    {

    int recv = command_sock.ReceiveFrom(data, ref Remote);

    /*得到目標的IP 埠*/

    Console.WriteLine("Message received from {0}: ", Remote.ToString());

    /*得到收到的資訊*/

    Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

    }

    catch

    {

    Console.WriteLine("連線超時~~ ");

    continue;

    }

    if(data[0] == 1)

    {

    /*開啟新視窗,前提你已經建立了新的form*/

    form2 new_form= new form2 ();

    new_form.ShowDialog();

    }

    else if(data[0] == 2)

    {

    /*關閉現在的視窗*/

    this.Close();

    }

    else if(data[0] == 3)

    {

    /*隱藏現在視窗*/

    this.Hide();

    }

    }

    }

  • 中秋節和大豐收的關聯?
  • “荷馬史詩”的名詞解釋?