根據接收數據判斷窗體的啟動關閉隱藏?
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();
}
}
}