private void TcpListening() { System.Net.Sockets.TcpListener listener = new TcpListener(IPAddress.Any, 1001); try { listener.Start(); } catch (SocketException e) { MessageBox.Show(e.Message); Application.Exit(); } byte[] buffer = new byte[2048]; MemoryStream stream; while (true) { stream = new MemoryStream(); Socket client = listener.AcceptSocket(); try { int receivedLength; do { receivedLength = client.Receive(buffer); stream.Write(buffer, 0, receivedLength); } while (receivedLength > 0); } catch (SocketException) { } finally { stream.Position = 0; string json = (new StreamReader(stream, Encoding.UTF8)).ReadToEnd(); client.Close(); JavaScriptSerializer serializer = new JavaScriptSerializer(); NotifyMessage msg = null; try { msg = serializer.Deserialize <NotifyMessage>(json); } catch (Exception e) { } stream.Close(); if (msg != null && msg.VerifyCode == "NOTIFY1.0.0.0") { myNotify.ShowBalloonTip(msg.Timeout, msg.TipTitle, msg.TipText, ToolTipIcon.Info); AddNotify(msg); } } } }
static void Main(string[] args) { TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Loopback, 1001)); NotifyMessage msg = new NotifyMessage { VerifyCode = "NOTIFY1.0.0.0", Timeout = 5000, TipTitle = "Tip Tile", TipText = "Test notify:)<>>KK!@#!$$汉字" }; JavaScriptSerializer serializer = new JavaScriptSerializer(); string json = serializer.Serialize(msg); byte[] buffer = Encoding.UTF8.GetBytes(json); NetworkStream ns = client.GetStream(); ns.Write(buffer, 0, buffer.Length); ns.Close(); client.Close(); }
private void AddNotify(NotifyMessage msg) { NotifySoundPlayer.Play(); NotifyList.Add(new LoggedNotifyMessage(msg)); this.Invoke(new Action(UpdateNotifyListWindow)); }
public LoggedNotifyMessage(NotifyMessage nMsg) : base(nMsg.VerifyCode, nMsg.Timeout, nMsg.TipTitle, nMsg.TipText) { TimeStamp = DateTime.Now; }