服务端:
1 namespace WebSocketServerEx 2 { 3 public class OpCode 4 { 5 public const sbyte Plain = -2; // defined by SuperWebSocket, to support hybi-00 6 public const string PlainTag = "-2"; 7 8 public const sbyte Handshake = -1; // defined by SuperWebSocket 9 public const string HandshakeTag = "-1"; 10 11 public const sbyte Continuation = 0; 12 public const string ContinuationTag = "0"; 13 14 public const sbyte Text = 1; 15 public const string TextTag = "1"; 16 17 public const sbyte Binary = 2; 18 public const string BinaryTag = "2"; 19 20 public const sbyte Close = 8; 21 public const string CloseTag = "8"; 22 23 public const sbyte Ping = 9; 24 public const string PingTag = "9"; 25 26 public const sbyte Pong = 10; 27 public const string PongTag = "10"; 28 } 29 30 public partial class Form1 : Form 31 { 32 private Thread thMain = null; 33 private Thread thFlashServer = null; 34 private Thread thClear = null; 35 private Dictionary<string, TcpClient> listMainTcp = new Dictionary<string, TcpClient>(); 36 private List<TcpClient> listFlashTcp = new List<TcpClient>(); 37 private Dictionary<string, Thread> listths = new Dictionary<string, Thread>(); 38 39 private ManualResetEvent tcpClientConnected = new ManualResetEvent(false); 40 private ManualResetEvent tcpClientConnectedflash = new ManualResetEvent(false); 41 42 public bool IsOnline(TcpClient c) 43 { 44 return !((c.Client.Poll(1000, SelectMode.SelectRead) && (c.Client.Available == 0)) || !c.Client.Connected); 45 } 46 public Form1() 47 { 48 InitializeComponent(); 49 } 50 51 private void Form1_Load(object sender, EventArgs e) 52 { 53 txtIP.Text = GetLocalIP(); 54 txtPort.Text = "8888"; 55 } 56 57 private void WebSocketMain() 58 { 59 //实例化服务器本机的端点 60 IPEndPoint local = new IPEndPoint(IPAddress.Parse(txtIP.Text), Convert.ToInt32(txtPort.Text)); 61 //定义服务器监听对象 62 TcpListener listener = new TcpListener(local); 63 //开始监听 64 listener.Start(); 65 66 thMain = new Thread(() => 67 { 68 while (true) 69 { 70 try 71 { 72 tcpClientConnected.Reset(); 73 listener.BeginAcceptTcpClient(clientConnect, listener); 74 tcpClientConnected.WaitOne(); 75 } 76 catch (Exception ex) 77 { 78 WriteLog("Error", "WebSocketMain " + ex.Message); 79 } 80 } 81 }); 82 83 thMain.Start(); 84 } 85 86 private void clientConnect(IAsyncResult ar) 87 { 88 try 89 { 90 TcpListener listener = (TcpListener)ar.AsyncState; 91 //接受客户的连接,得到连接的Socket 92 TcpClient client = listener.EndAcceptTcpClient(ar); 93 client.ReceiveBufferSize = 524288; 94 client.SendBufferSize = 524288; 95 Console.WriteLine("t " + client.Available); 96 int maxi = 0; 97 while (IsOnline(client) && maxi <= 1000) 98 { 99 maxi++; 100 Console.WriteLine("max " + maxi); 101 if (client.Available > 0) 102 { 103 BinaryReader reader = new BinaryReader(client.GetStream()); 104 BinaryWriter writer = new BinaryWriter(client.GetStream()); 105 106 Console.WriteLine("33"); 107 108 byte[] buffer = new byte[client.Available]; 109 reader.Read(buffer, 0, client.Available); 110 String result = Encoding.UTF8.GetString(buffer); 111 if (result.IndexOf("Sec-WebSocket-Key") >= 0) 112 { 113 writer.Write(PackHandShakeData(GetSecKeyAccetp(result))); 114 writer.Flush(); 115 116 lock (listMainTcp) 117 { 118 if (!listMainTcp.ContainsValue(client)) 119 { 120 listMainTcp.Add(client.Client.RemoteEndPoint.ToString(), client); 121 } 122 } 123 124 cmbClient.Invoke(new Action(() => 125 { 126 cmbClient.Items.Add(client.Client.RemoteEndPoint.ToString()); 127 })); 128 129 WriteLog("Info", "websocket 握手成功!" + client.Client.RemoteEndPoint); 130 } 131 132 Thread th = new Thread(() => 133 { 134 while (true) 135 { 136 try 137 { 138 if (client.Available > 0) 139 { 140 byte[] buffers = new byte[client.Available]; 141 reader.Read(buffers, 0, client.Available); 142 result = AnalyticData(buffers, buffers.Length); 143 WriteLog("Info", "来自客户端[" + client.Client.RemoteEndPoint + "]消息" + result); 144 145 if (result == "u0003�") 146 { 147 lock (listMainTcp) 148 { 149 listMainTcp.Remove(client.Client.RemoteEndPoint.ToString()); 150 } 151 lock (listths) 152 { 153 listths.Remove(client.Client.RemoteEndPoint.ToString()); 154 } 155 client.Close(); 156 break; 157 } 158 } 159 } 160 catch (ThreadAbortException e) 161 { 162 163 } 164 catch (Exception ex) 165 { 166 WriteLog("Error", "th " + ex.Message); 167 } 168 } 169 }); 170 th.Name = client.Client.RemoteEndPoint.ToString(); 171 listths.Add(th.Name, th); 172 th.Start(); 173 174 break; 175 } 176 } 177 } 178 catch 179 { 180 } 181 finally 182 { 183 tcpClientConnected.Set(); 184 } 185 } 186 187 private void FlashSocketServer() 188 { 189 //实例化服务器本机的端点 190 IPEndPoint local = new IPEndPoint(IPAddress.Parse(txtIP.Text), 843); 191 //定义服务器监听对象 192 TcpListener listener = new TcpListener(local); 193 //开始监听 194 listener.Start(); 195 196 TcpClient client = null; 197 BinaryReader reader = null; 198 BinaryWriter writer = null; 199 thFlashServer = new Thread(() => 200 { 201 while (true) 202 { 203 try 204 { 205 client = listener.AcceptTcpClient(); 206 Console.WriteLine("1 t" + client.Available); 207 int maxi = 0; 208 while (IsOnline(client) && maxi <= 1000) 209 { 210 maxi++; 211 Console.WriteLine("max2 " + maxi); 212 if (client.Available > 0) 213 { 214 lock (listFlashTcp) 215 { 216 if (!listFlashTcp.Contains(client)) 217 { 218 listFlashTcp.Add(client); 219 } 220 } 221 222 reader = new BinaryReader(client.GetStream()); 223 writer = new BinaryWriter(client.GetStream()); 224 225 226 byte[] buffer = new byte[client.Available]; 227 reader.Read(buffer, 0, client.Available); 228 String result = Encoding.UTF8.GetString(buffer); 229 if (result.IndexOf("<policy-file-request/>") >= 0) 230 { 231 byte[] datas = System.Text.Encoding.UTF8.GetBytes("<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>