private void Tcpclientmgr(TcpClient tcpc, int clientid, string clientname) { //adds 1 to the program clientcount once this has been established so sync is maintained. clientcount++; int myid = clientid; //tcrun is used on a per thread basis to determine if this individual thread should still run Boolean tcrun = true; int cmdreadcnt = inccmds.Count(); NewClient thisclient = new NewClient(myid, tcpc.Client.RemoteEndPoint.ToString()); //add our id and remote endpoint to the clientlist clientlist.Add(thisclient); //add our id and name to the clientlist - this is the list we will use later for our friendly name //overwrites endpoint client identifier with our naming client that can be modified later if (clientname != "") { thisclient = new NewClient(myid, clientname); } //adds the client to the list clientnames.Add(thisclient); byte[] nbuff = Encoding.ASCII.GetBytes("Client Connected:" + myid + vbcrlf + "To Send Shared Server Messages Use Chat:<Message>" + vbcrlf); //tcpc.Client.Send(nbuff); tcpc.ReceiveTimeout = 10; tcpc.SendTimeout = 10; //servrun is main boolean to determine if whole program should execute //tcrun is the boolean to determine if each individual threaded tcpclient should run or not //sends data to tcpclient on each cycle of the while loop to determine if it is still open, if not it closes so as not to hog threads. try { byte[] tempbuff = new byte[1]; //peek must be used so that we dont tie up the main receive buffer if (tcpc.Client.Receive(tempbuff, SocketFlags.Peek) < 1) { tcpc.Close(); tcpc.Client.Close(); tcrun = false; Thread.CurrentThread.Abort(); } } catch (Exception excnct) { //WriteLog("TCP Server Error: " + excnct.ToString()); } try { //this.Invoke(new Action(() => this.Text = string.Format("Hutchison Technologies - Fiit Manager {0}", pollcount) + " - " + cmdreadcnt + "-" + inccmds.Count())); //open network stream from tcp client NetworkStream ns = tcpc.GetStream(); // must set timeouts so that thread does not hang on read ns.ReadTimeout = 10; ns.WriteTimeout = 10; byte[] buffer = new byte[1024]; String unencbuff = null; //makes sure data is available in the network stream before trying to read if (ns.DataAvailable == true) { //makes sure reading is possible before trying to read if (ns.CanRead == true) { // reads buffer value to byte ns.Read(buffer, 0, 1024); //steps through each byte in the byte array to make sure no dead data is held in memory if byte = 0 then it is not commited foreach (byte b in buffer) { if (b != 0) { unencbuff += Convert.ToChar(b); } } // writecmd(unencbuff); try { StringBuilder sb = new StringBuilder(); sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>"); sb.AppendLine("<root>"); string respbody = "<html><body><h1> Hello, Richard!</h1></body></html> "; string response = "HTTP/1.1 200 OK\r\nDate: " + DateTime.Now.ToString("ddd MMM yyy HH:mm:SS") + "\r\nServer: Graeme Hind / 1.0 (Win64)\r\nLast - Modified: " + DateTime.Now.ToString("ddd MMM yyy HH:mm:SS") + "\r\nContent - Length: " + respbody.Length + "\r\nContent - Type: text / xml\r\nConnection: Closed"; //tries to split the incoming commands, this stops us having to user .compare and creates a more specific environment where we can specify command[1] = a result String[] command = new string[2]; command = unencbuff.Split(Convert.ToChar(":")); string[] responselines; string[] separators = { "\r\n" }; responselines = unencbuff.Split(separators, StringSplitOptions.None); string searchline = "GET /requests/status.xml"; string actcmd = ""; string input = ""; try { string cururl = "http://127.0.0.1:8080" + responselines[0].Replace("GET ", "").Replace("HTTP/1.1", ""); Uri nuri = new Uri(cururl); actcmd = HttpUtility.ParseQueryString(nuri.Query).Get("command"); input = HttpUtility.ParseQueryString(nuri.Query).Get("input"); } catch (Exception ex) { } try { if (actcmd.ToLower() == "in_play") { input = HttpUtility.UrlDecode(input); WMP.URL = HttpUtility.HtmlDecode(input); Thread.Sleep(200); if (WMP.playState.ToString() == "wmppsPlaying") { skpTime.Maximum = Convert.ToInt32(WMP.currentMedia.duration); skpTime.Minimum = 0; skpTime.Enabled = true; btnSkip.Enabled = true; this.Invoke(new Action(() => this.Text = thistitle + " - Playing:" + WMP.currentMedia.name)); Thread ntrd = new Thread(() => MusicFade("play")); ntrd.IsBackground = true; ntrd.Start(); } } else if (actcmd.ToLower() == "pl_stop") { //Thread ntrd = new Thread(() => MusicFade("stop")); //ntrd.IsBackground = true; //ntrd.Start(); WMP.Invoke(new Action(() => WMP.Ctlcontrols.stop())); } else if (actcmd.ToLower() == "pl_empty") { // Thread ntrd = new Thread(() => MusicFade("stop")); //ntrd.IsBackground = true; //ntrd.Start(); WMP.Invoke(new Action(() => WMP.Ctlcontrols.stop())); WMP.Invoke(new Action(() => WMP.URL = "")); } else if (actcmd.ToLower() == "pause") { //Thread ntrd = new Thread(()=> MusicFade("pause")); //ntrd.IsBackground = true; //ntrd.Start(); WMP.Invoke(new Action(() => WMP.Ctlcontrols.pause())); } else if (actcmd.ToLower() == "play") { Thread ntrd = new Thread(() => MusicFade("play")); ntrd.IsBackground = true; ntrd.Start(); } else if (actcmd.ToLower() == "loop") { if (input == "1") { WMP.settings.setMode("loop", true); } else if (input == "0") { WMP.settings.setMode("loop", false); } } }catch (Exception ex) { } try { sb.AppendLine("<length>" + Convert.ToInt32(WMP.currentMedia.duration) + "</length>"); }catch (Exception ex) { sb.AppendLine("<length>0</length>"); } sb.AppendLine("<time>" + Convert.ToInt32(WMP.Ctlcontrols.currentPosition) + "</time>"); if (WMP.playState.ToString() == "wmppsUndefined") { sb.AppendLine("<state>stopped</state>"); } else if (WMP.playState.ToString() == "wmppsPlaying") { sb.AppendLine("<state>playing</state>"); } else if (WMP.playState.ToString() == "wmppsStopped") { sb.AppendLine("<state>stopped</state>"); } else { sb.AppendLine("<state>stopped</state>"); } try { sb.AppendLine("<track>" + WMP.currentMedia.name + "</track>"); } catch (Exception ex) { } sb.AppendLine("</root>"); byte[] nbuffinc = Encoding.ASCII.GetBytes(response + "\r\n\r\n" + sb.ToString()); ns.Write(nbuffinc, 0, nbuffinc.Length); } catch (Exception ex) { // MessageBox.Show(ex.ToString()); } } } try { // for loop to cycle through all messages sitting in our list of messages which we earlier removed dead bytes from and commited /* for (int i = cmdreadcnt; i < inccmds.Count; i++) * { * byte[] nbuffinc = Encoding.ASCII.GetBytes(inccmds[i]); * * //keeps the regional variable in sync * cmdreadcnt++; * * //write messages from the previous messages buffer to the connected client * if (ns.CanWrite == true) * { * ns.Write(nbuffinc, 0, nbuffinc.Length); * * } * } * // ns.Write() */ } catch (Exception ex) { WriteLog("TCP Client Manager: " + ex.ToString()); } } catch (Exception mainex) { WriteLog("TCP Client Manager:" + mainex.ToString()); } //sleep the thread for 1ms so it does not hog resources Thread.Sleep(1); //if exited from the while loop close the tcp client and abort the thread tcpc.Close(); Thread.CurrentThread.Abort(); }