利用irc协议的优点:

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

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

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

 

缺点:安全性比较差

 

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

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

  1 using System;
  2 using TechLifeForum;
  3 using System.Windows.Forms;
  4 using System.Timers;
  5 using System.Net.Sockets;
  6 using System.IO;
  7 using System.Net;
  8 using System.Text;
  9 
 10 
 11 namespace ClientDemo
 12 {
 13     public partial class frmMain : Form
 14     {
 15         IrcClient client;
 16         public static string sendmessage;
 17         public frmMain()
 18         {
 19             InitializeComponent();
 20         }
 21 
 22         private void frmMain_Load(object sender, EventArgs e)
 23         {
 24 
 25 
 26         }
 27         private void btnConnect_Click(object sender, EventArgs e)
 28         {
 29             if (btnConnect.Text == "连接")
 30             {
 31                 checkSSL.Enabled = false;
 32                 DoConnect();
 33             }
 34             else
 35             {
 36                 DoDisconnect();
 37                 checkSSL.Enabled = true;
 38             }
 39         }
 40         private void btnSend_Click(object sender, EventArgs e)
 41         {
 42             try
 43             {
 44                 if (client.Connected && !String.IsNullOrEmpty(txtSend.Text.Trim()))
 45                 {
 46 
 47                     if (txtChannel.Text.StartsWith("#"))
 48                         client.SendMessage(txtChannel.Text.Trim(), txtSend.Text.Trim());
 49                     else
 50                         client.SendMessage("#" + txtChannel.Text.Trim(), txtSend.Text.Trim());
 51 
 52                     AddToChatWindow("Me: " + txtSend.Text.Trim());
 53                     txtSend.Clear();
 54                     txtSend.Focus();
 55                 }
 56             }
 57             catch
 58             { }
 59         }
 60         private void txtSend_KeyDown(object sender, KeyEventArgs e)
 61         {
 62             if (e.KeyCode == Keys.Enter)
 63                 btnSend.PerformClick();
 64         }
 65 
 66         private void DoConnect()
 67         {
 68             if (String.IsNullOrEmpty(txtServer.Text.Trim()))
 69             {
 70                 MessageBox.Show("Please specify a server");
 71                 return;
 72             }
 73             if (String.IsNullOrEmpty(txtChannel.Text.Trim()))
 74             {
 75                 MessageBox.Show("Please specify a channel");
 76                 return;
 77             }
 78             if (String.IsNullOrEmpty(txtNick.Text.Trim()))
 79             {
 80                 MessageBox.Show("Please specify a nick");
 81                 return;
 82             }
 83 
 84             int port;
 85             if (Int32.TryParse(txtPort.Text, out port))
 86                 if (checkSSL.Checked)
 87                 {
 88                     client = new IrcClient(txtServer.Text.Trim(), port, true);
 89                 }
 90                 else {
 91                     client = new IrcClient(txtServer.Text.Trim(), port, false);
 92                 }
 93             else
 94                 client = new IrcClient(txtServer.Text.Trim());
 95 
 96             AddEvents();
 97             client.Nick = txtNick.Text.Trim();
 98 
 99             btnConnect.Enabled = false;
100             txtChannel.Enabled = false;
101             txtPort.Enabled = false;
102             txtServer.Enabled = false;
103             txtNick.Enabled = false;
104             rtbOutput.Clear(); // in case they reconnect and have old stuff there
105 
106 
107             client.Connect();
108         }
109         private void DoDisconnect()
110         {
111 
112             btnConnect.Enabled = true;
113             txtChannel.Enabled = true;
114             txtPort.Enabled = true;
115             txtServer.Enabled = true;
116             txtNick.Enabled = true;
117 
118             lstUsers.Items.Clear();
119             txtSend.Enabled = false;
120             btnSend.Enabled = false;
121 
122             client.Disconnect();
123             client = null;
124 
125             btnConnect.Text = "连接";
126         }
127         private void AddEvents()
128         {
129             client.ChannelMessage += client_ChannelMessage;
130             client.ExceptionThrown += client_ExceptionThrown;
131             client.NoticeMessage += client_NoticeMessage;
132             client.OnConnect += client_OnConnect;
133             client.PrivateMessage += client_PrivateMessage;
134             client.ServerMessage += client_ServerMessage;
135             client.UpdateUsers += client_UpdateUsers;
136             client.UserJoined += client_UserJoined;
137             client.UserLeft += client_UserLeft;
138             client.UserNickChange += client_UserNickChange;
139         }
140         private void AddToChatWindow(string message)
141         {
142             rtbOutput.AppendText(message + "\n");
143             rtbOutput.ScrollToCaret();
144         }
145 
146         #region Event Listeners
147 
148         void client_OnConnect(object sender, EventArgs e)
149         {
150             txtSend.Enabled = true;
151             txtSend.Focus();
152             btnSend.Enabled = true;
153 
154             btnConnect.Text = "Disconnect";
155             btnConnect.Enabled = true;
156 
157             if (txtChannel.Text.StartsWith("#"))
158                 client.JoinChannel(txtChannel.Text.Trim());
159             else
160                 client.JoinChannel("#" + txtChannel.Text.Trim());
161 
162         }
163 
164         void client_UserNickChange(object sender, UserNickChangedEventArgs e)
165         {
166             lstUsers.Items[lstUsers.Items.IndexOf(e.Old)] = e.New;
167         }
168 
169         void client_UserLeft(object sender, UserLeftEventArgs e)
170         {
171             lstUsers.Items.Remove(e.User);
172         }
173 
174         void client_UserJoined(object sender, UserJoinedEventArgs e)
175         {
176             lstUsers.Items.Add(e.User);
177         }
178 
179         void client_UpdateUsers(object sender, UpdateUsersEventArgs e)
180         {
181             lstUsers.Items.Clear();
182             lstUsers.Items.AddRange(e.UserList);
183             
184         }
185 
186         void client_ServerMessage(object sender, StringEventArgs e)
187         {
188             Console.WriteLine(e.Result);
189         }
190 
191         void client_PrivateMessage(object sender, PrivateMessageEventArgs e)
192         {
193             AddToChatWindow("PM FROM " + e.From + ": " + e.Message);
194         }
195 
196         void client_NoticeMessage(object sender,NoticeMessageEventArgs e)
197         {
198             AddToChatWindow("NOTICE FROM " + e.From + ": " + e.Message);
199         }
200         
201         void client_ExceptionThrown(object sender, ExceptionEventArgs e)
202         {
203             MessageBox.Show(e.Exception.Message);
204         }
205 
206         void client_ChannelMessage(object sender, ChannelMessageEventArgs e)
207         {
208             AddToChatWindow(e.From + ": " + e.Message);
209         }
210 
211         #endregion
212         #region SSL
213         private void checkSSL_CheckedChanged(object sender, EventArgs e)
214         {
215             if (checkSSL.Checked)
216             {
217                 MessageBox.Show("SSL IRC Usually opperates over port 6697!", "WARNING!");
218             }
219         }
220         #endregion
221 
222         public static string Get(string url)
223         {
224             string result = "";
225             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
226             HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
227             Stream stream = resp.GetResponseStream();
228             try
229             {
230                 //获取内容
231                 using (StreamReader reader = new StreamReader(stream))
232                 {
233                     result = reader.ReadToEnd();
234                 }
235             }
236             finally
237             {
238                 stream.Close();
239             }
240             return result;
241         }
242 
243         void Tmr_Elapsed(object sender, ElapsedEventArgs e)
244         {
245             try
246             {
247                 int a = new Random().Next(0, 100);
248                 sendmessage = "当前温度" + a.ToString();
249                 //MessageBox.Show(sendmessage);
250                 txtSend.Text = sendmessage;
251                 btnSend_Click(sender, e);
252             }
253             catch
254             { }
255         }
256 
257         private void lstUsers_SelectedIndexChanged(object sender, EventArgs e)
258         {
259             
260         }
261 
262         private void LabelServer_Click(object sender, EventArgs e)
263         {
264             System.Timers.Timer maxTimer = new System.Timers.Timer();
265             maxTimer.Elapsed += new ElapsedEventHandler(Tmr_Elapsed);
266             // 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
267             maxTimer.Interval = 1000;
268             maxTimer.Enabled = true;
269             
270         }
271     }
272 }