SixiBroadcast/UDPClient/Frm_Client.cs

154 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
using UDPClient.Properties;
namespace UDPClient
{
public partial class Frm_Client : Form
{
Icon icoOn = Resources.IconOn;
Icon icoOff = Resources.IconOff;
bool isOn;
bool isTwinkle;
static UdpClient client;
static IPEndPoint serverendpoint;
static System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
public Frm_Client()
{
InitializeComponent();
}
private void Frm_Client_Load(object sender, EventArgs e)
{
//开机自启动添加
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk.SetValue("SixiBroadcast", path);
rk.Close();
//初始化数据
client = new UdpClient(new IPEndPoint(IPAddress.Any, 25333));
serverendpoint = new IPEndPoint(IPAddress.Loopback, 25433);
//启动客户端监听
Thread t = new Thread(new ThreadStart(ClientThread));
t.IsBackground = true;
t.Start();
//启动Web监听
Thread w = new Thread(new ThreadStart(WebListen));
w.IsBackground = true;
w.Start();
//初始化Timer
tmr.Interval = 390;
tmr.Tick += tmr_Tick;
tmr.Enabled = true;
notifyIcon.ShowBalloonTip(1000, "四喜消息提示", "欢迎使用四喜消息提示系统!", ToolTipIcon.Info);
}
void tmr_Tick(object sender, EventArgs e)
{
if (!isTwinkle) return;
if (isOn)
{
isOn = false;
notifyIcon.Icon = icoOff;
}
else
{
isOn = true;
notifyIcon.Icon = icoOn;
}
}
public static IPAddress GetHostIP()
{
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); //取得本机IP
foreach (var item in ipEntry.AddressList)
{
if (!item.IsIPv6LinkLocal)
{
return item;
}
}
return null;
}
private void WebListen()
{
try
{
HttpListener listerner = new HttpListener();
listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
listerner.Prefixes.Add("http://127.0.0.1:2533/web/");
listerner.Start();
Console.WriteLine("网页服务器开始监听.......");
while (true)
{
//等待请求连接
//没有请求则GetContext处于阻塞状态
HttpListenerContext ctx = listerner.GetContext();
ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
string username = ctx.Request.QueryString["username"];
if (username != null && username.Length > 0)
{
IPAddress addr = GetHostIP();
string str = username + " " + (addr == null ? "null" : addr.ToString()) + " 25333";
Console.WriteLine(str);
byte[] buf = Encoding.UTF8.GetBytes(str);
client.Send(buf, buf.Length, serverendpoint);
notifyIcon.ShowBalloonTip(1000, "四喜消息提示", "用户登录成功!", ToolTipIcon.Info);
}
ctx.Response.Close();
}
}
catch (Exception e)
{
Console.WriteLine("网页服务器出现错误: " + e.Message);
}
}
private void ClientThread()
{
try
{
Console.WriteLine("客户端开始监听...");
while (true)
{
byte[] buf = client.Receive(ref serverendpoint);
string msg = Encoding.UTF8.GetString(buf);
isTwinkle = true;
notifyIcon.ShowBalloonTip(1000, "四喜消息提示", msg, ToolTipIcon.Info);
}
}
catch (Exception e)
{
Console.WriteLine("UDP客户端出现错误: " + e.Message);
client.Close();
}
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
isTwinkle = false;
notifyIcon.Icon = icoOn;
}
private void 退ToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
}
}