//TODO: restructure this method //this function is a mess.. needs a new structure private void startListening() { Socket mySocket; while (true) { try { mySocket = server.AcceptSocket(); } catch (Exception) { mySocket = null; } if ((mySocket != null) && (mySocket.Connected)) { mySocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); // speeds up tcp ... Byte[] bReceive = new Byte[1024]; string sBuffer; clientIP = ((IPEndPoint)mySocket.RemoteEndPoint).Address.ToString(); // DSW 12/12/2007 thats how (see above)... clientIP = clientIP.Split(':')[0]; //get rid of local port. what's a better way to do this? try { mySocket.Receive(bReceive, bReceive.Length, 0); sBuffer = Encoding.ASCII.GetString(bReceive); if (sBuffer.Length > 0) { if (sBuffer.Split(' ')[1] != "Get /favicon.ico") { Invoke(new onAddLog(addLog), "Req: " + sBuffer.Split(' ')[1], sBuffer); } else { sBuffer = ""; } } } catch (Exception ex) { Invoke(new onAddLog(addLog), "Error", "startListening: " + Environment.NewLine + ex.ToString()); sBuffer = ""; } //we must validate authentication on each request, not on each "GET /AVNC" if ((!sBuffer.StartsWith("GET / ")) && (!validate(sBuffer))) { HTMLWrapper.sendPAGE("Wrong password...", mySocket); Invoke(new onAddLog(addLog), "Error (WP)", String.Format("Wrong Password:{0}{1}", Environment.NewLine, sBuffer)); sBuffer = ""; } if ((sBuffer.StartsWith("GET /sendClick")) && (viewOnlyCB.Checked == false)) { doMouseClick(sBuffer); Thread.Sleep(250); //give UI a chance to update after mouse click generateSnapshot(); //mySocket.Send(Encoding.ASCII.GetBytes(imagesToSend)); HTMLWrapper.sendTEXT(imagesToSend, mySocket, 200); mySocket.Close(); imagesToSend = "IMGS"; } else if ((sBuffer.StartsWith("GET /sendDrag")) && (viewOnlyCB.Checked == false)) // added drag mouse event... { doMouseDrag(sBuffer); Thread.Sleep(250); //give UI a chance to update after mouse drag generateSnapshot(); //mySocket.Send(Encoding.ASCII.GetBytes(imagesToSend)); HTMLWrapper.sendTEXT(imagesToSend, mySocket, 200); mySocket.Close(); imagesToSend = "IMGS"; } else if ((sBuffer.StartsWith("GET /sendStroke")) && (viewOnlyCB.Checked == false)) { doStroke(sBuffer); Thread.Sleep(100); //give UI a chance to update after keystroke generateSnapshot(); //mySocket.Send(Encoding.ASCII.GetBytes(imagesToSend)); HTMLWrapper.sendTEXT(imagesToSend, mySocket, 200); mySocket.Close(); imagesToSend = "IMGS"; } else if (sBuffer.StartsWith("GET /whatsNew")) { generateSnapshot(); //mySocket.Send(Encoding.ASCII.GetBytes(imagesToSend)); HTMLWrapper.sendTEXT(imagesToSend, mySocket, 200); mySocket.Close(); imagesToSend = "IMGS"; } else if (sBuffer.StartsWith("GET / ")) { //mySocket.Send(Encoding.ASCII.GetBytes(loginPage())); HTMLWrapper.sendPAGE(loginPage(), mySocket); mySocket.Close(); } else if (sBuffer.StartsWith("GET /AVNC")) { pieces.RemoveRange(0, pieces.Count - 1); //remove all old images generateSnapshot(); HTMLWrapper.sendPAGE(mainPage(), mySocket); } else if (sBuffer.StartsWith("GET /image")) { try { HTMLWrapper.sendImage((Piece)pieces[extractImageNumber(sBuffer)], mySocket, imageCompression); } catch { //TODO: send error image HTMLWrapper.sendTEXT("Error, try again", mySocket, 500); } } else { HTMLWrapper.sendPAGE("Unknown request...", mySocket); } } } }
private void button1_Click(object sender, EventArgs e) { if (loginPasswordTB.Text == "") { MessageBox.Show("You must specify a 'Login Password'.", "A-VNC"); loginPasswordTB.Focus(); return; } radioBtnIPv6.Enabled = false; radioBtnIPv4.Enabled = false; HTMLWrapper.setTitle("A-VNC"); if (button1.Text == "Start") { setRegistryValues(); //<required initialization> pieces = new ArrayList(); logs = new ArrayList(); //</required initailization> logLV.Items.Clear(); // to keep the logLV's index concurrent with logs' index try { if (radioBtnIPv6.Checked) { server = new TcpListener(IPAddress.IPv6Any, Convert.ToInt32(listenPortTB.Value)); } if (radioBtnIPv4.Checked) { server = new TcpListener(IPAddress.Any, Convert.ToInt32(listenPortTB.Value)); } server.Start(); } catch (Exception ex) { addLog("Error", "ServerStop: " + ex.ToString()); } listeningThread = new Thread(new ThreadStart(startListening)); listeningThread.Start(); button1.Text = "Stop"; listenPortTB.Enabled = false; loginPasswordTB.Enabled = false; trayIconMenu.Items[0].Text = "Stop"; generateSnapshot(); //still don't know why it will not work sometimes if this is not here } else { try { server.Stop(); listeningThread.Abort(); button1.Text = "Start"; listenPortTB.Enabled = true; loginPasswordTB.Enabled = true; trayIconMenu.Items[0].Text = "Start"; } catch (Exception ex) { addLog("Error", String.Format("ServerStop: {0}", ex)); } radioBtnIPv6.Enabled = true; radioBtnIPv4.Enabled = true; } }