利用irc服务器做物联网数据转发 c# winform程序

利用irc协议的优点:

免费,不用购买服务器就可以实现稳定公网转发数据

开源,irc的客户端和开源库众多,省去了自己造*的时间

历史悠久,互联网诞生伊始就有irc

缺点:安全性比较差

简单协议接收可以用:https://kiwiirc.com/

下面是发送客户端的源码:

 using System;
using TechLifeForum;
using System.Windows.Forms;
using System.Timers;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Text; namespace ClientDemo
{
public partial class frmMain : Form
{
IrcClient client;
public static string sendmessage;
public frmMain()
{
InitializeComponent();
} private void frmMain_Load(object sender, EventArgs e)
{ }
private void btnConnect_Click(object sender, EventArgs e)
{
if (btnConnect.Text == "连接")
{
checkSSL.Enabled = false;
DoConnect();
}
else
{
DoDisconnect();
checkSSL.Enabled = true;
}
}
private void btnSend_Click(object sender, EventArgs e)
{
try
{
if (client.Connected && !String.IsNullOrEmpty(txtSend.Text.Trim()))
{ if (txtChannel.Text.StartsWith("#"))
client.SendMessage(txtChannel.Text.Trim(), txtSend.Text.Trim());
else
client.SendMessage("#" + txtChannel.Text.Trim(), txtSend.Text.Trim()); AddToChatWindow("Me: " + txtSend.Text.Trim());
txtSend.Clear();
txtSend.Focus();
}
}
catch
{ }
}
private void txtSend_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
btnSend.PerformClick();
} private void DoConnect()
{
if (String.IsNullOrEmpty(txtServer.Text.Trim()))
{
MessageBox.Show("Please specify a server");
return;
}
if (String.IsNullOrEmpty(txtChannel.Text.Trim()))
{
MessageBox.Show("Please specify a channel");
return;
}
if (String.IsNullOrEmpty(txtNick.Text.Trim()))
{
MessageBox.Show("Please specify a nick");
return;
} int port;
if (Int32.TryParse(txtPort.Text, out port))
if (checkSSL.Checked)
{
client = new IrcClient(txtServer.Text.Trim(), port, true);
}
else {
client = new IrcClient(txtServer.Text.Trim(), port, false);
}
else
client = new IrcClient(txtServer.Text.Trim()); AddEvents();
client.Nick = txtNick.Text.Trim(); btnConnect.Enabled = false;
txtChannel.Enabled = false;
txtPort.Enabled = false;
txtServer.Enabled = false;
txtNick.Enabled = false;
rtbOutput.Clear(); // in case they reconnect and have old stuff there client.Connect();
}
private void DoDisconnect()
{ btnConnect.Enabled = true;
txtChannel.Enabled = true;
txtPort.Enabled = true;
txtServer.Enabled = true;
txtNick.Enabled = true; lstUsers.Items.Clear();
txtSend.Enabled = false;
btnSend.Enabled = false; client.Disconnect();
client = null; btnConnect.Text = "连接";
}
private void AddEvents()
{
client.ChannelMessage += client_ChannelMessage;
client.ExceptionThrown += client_ExceptionThrown;
client.NoticeMessage += client_NoticeMessage;
client.OnConnect += client_OnConnect;
client.PrivateMessage += client_PrivateMessage;
client.ServerMessage += client_ServerMessage;
client.UpdateUsers += client_UpdateUsers;
client.UserJoined += client_UserJoined;
client.UserLeft += client_UserLeft;
client.UserNickChange += client_UserNickChange;
}
private void AddToChatWindow(string message)
{
rtbOutput.AppendText(message + "\n");
rtbOutput.ScrollToCaret();
} #region Event Listeners void client_OnConnect(object sender, EventArgs e)
{
txtSend.Enabled = true;
txtSend.Focus();
btnSend.Enabled = true; btnConnect.Text = "Disconnect";
btnConnect.Enabled = true; if (txtChannel.Text.StartsWith("#"))
client.JoinChannel(txtChannel.Text.Trim());
else
client.JoinChannel("#" + txtChannel.Text.Trim()); } void client_UserNickChange(object sender, UserNickChangedEventArgs e)
{
lstUsers.Items[lstUsers.Items.IndexOf(e.Old)] = e.New;
} void client_UserLeft(object sender, UserLeftEventArgs e)
{
lstUsers.Items.Remove(e.User);
} void client_UserJoined(object sender, UserJoinedEventArgs e)
{
lstUsers.Items.Add(e.User);
} void client_UpdateUsers(object sender, UpdateUsersEventArgs e)
{
lstUsers.Items.Clear();
lstUsers.Items.AddRange(e.UserList); } void client_ServerMessage(object sender, StringEventArgs e)
{
Console.WriteLine(e.Result);
} void client_PrivateMessage(object sender, PrivateMessageEventArgs e)
{
AddToChatWindow("PM FROM " + e.From + ": " + e.Message);
} void client_NoticeMessage(object sender,NoticeMessageEventArgs e)
{
AddToChatWindow("NOTICE FROM " + e.From + ": " + e.Message);
} void client_ExceptionThrown(object sender, ExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message);
} void client_ChannelMessage(object sender, ChannelMessageEventArgs e)
{
AddToChatWindow(e.From + ": " + e.Message);
} #endregion
#region SSL
private void checkSSL_CheckedChanged(object sender, EventArgs e)
{
if (checkSSL.Checked)
{
MessageBox.Show("SSL IRC Usually opperates over port 6697!", "WARNING!");
}
}
#endregion public static string Get(string url)
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
try
{
//获取内容
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
finally
{
stream.Close();
}
return result;
} void Tmr_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
int a = new Random().Next(, );
sendmessage = "当前温度" + a.ToString();
//MessageBox.Show(sendmessage);
txtSend.Text = sendmessage;
btnSend_Click(sender, e);
}
catch
{ }
} private void lstUsers_SelectedIndexChanged(object sender, EventArgs e)
{ } private void LabelServer_Click(object sender, EventArgs e)
{
System.Timers.Timer maxTimer = new System.Timers.Timer();
maxTimer.Elapsed += new ElapsedEventHandler(Tmr_Elapsed);
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
maxTimer.Interval = ;
maxTimer.Enabled = true; }
}
}
上一篇:【转】十步让你成为一名优秀的Web开发人员


下一篇:IDEA插件开发总结