From 8c86ae24616ec3670a5e6033d85b6bb9fa0a8f6b Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Sat, 1 Mar 2014 11:46:24 +0800 Subject: [PATCH] Socket research --- Wox/MainWindow.xaml.cs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index feefb81ea..d24b07f1e 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Net; +using System.Net.Sockets; using System.Threading; using System.Windows; using System.Windows.Controls; @@ -57,8 +59,41 @@ namespace Wox { SetTheme(CommonStorage.Instance.UserSetting.Theme = "Default"); } + + new Thread(Listen).Start(); } + private void Listen() + { + + try + { + + TcpListener tcpl = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"),1234));//在5656端口新建一个TcpListener对象 + tcpl.Start(); + Debug.WriteLine("started listening.."); + while (true) + { + Socket s = tcpl.AcceptSocket(); + string remote = s.RemoteEndPoint.ToString(); + Byte[] stream = new Byte[80]; + int i = s.Receive(stream); + string msg = "<" + remote + ">" + System.Text.Encoding.UTF8.GetString(stream); + Debug.WriteLine(msg); + } + } + catch (System.Security.SecurityException) + { + Debug.WriteLine("firewall says no no to application - application cries.."); + } + + catch (Exception) + { + Debug.WriteLine("stoped listening.."); + } + } + + public void SetHotkey(string hotkeyStr, EventHandler action) { var hotkey = new HotkeyModel(hotkeyStr);