最近生活无聊,用C#做了个类似木马的程序,不过没有木马那么强大,缺少隐藏性等...,但功能实现了,好了废话少说。。进入正题。看看下面程序
private void CZ_Load(object sender, EventArgs e)//窗体加载事件
{
Form.CheckForIllegalCrossThreadCalls = false;//窗体控件排除线程影响
ss = new Send(ip);//自定义发送类创立对象
try
{
messThread = new Thread(new ThreadStart(havemess));//消息线程
messThread.IsBackground = true;//后台执行
FileThread = new Thread(new ThreadStart(ReceiveFile));//文件线程
FileThread.IsBackground = true;//后台执行
messThread.Start();//线程开始
FileThread.Start();//线程开始
}
catch (Exception t)
{
MessageBox.Show(t.Message);
}
}
public void havemess()//接收消息
{
int len = 0;
tl = new TcpListener(9999);//监听9999这个端口
while (true)
{
tl.Start();
s = tl.AcceptSocket();
byte[] b = new byte[4098];
len = s.Receive(b);
if (key == 1)
{
tl.Stop();
break;
}
string text = System.Text.Encoding.Unicode.GetString(b);
//this.listbox1.Text = "";
//this.listbox1.Text = text;
this.richTextBox1.Clear();
this.richTextBox1.AppendText(text);
s.Close();
}
closing = true;
}
public void ReceiveFile()//接收文件
{
int len = 0;
t2 = new TcpListener(9996);//监听9996这个端口
while (true)
{
t2.Start();
string path; //要传入文件的路径
s2 = t2.AcceptSocket();
byte[] b = new byte[4098];
len = s2.Receive(b);
if (key == 1)
{
t2.Stop();
break;
}
string text = System.Text.Encoding.Unicode.GetString(b);
//getfilemessage(text);
//if (kind == "f%")
//{
string []text2 = text.Split('&');
filetype = text2[1];
SaveFileDialog di = new SaveFileDialog();
di.Filter = "." + filetype + "|*." + filetype;
if (di.ShowDialog() == DialogResult.OK)
{
savefilename = di.FileName;
}
if (len != 0)
{
FileStream fs = new FileStream(savefilename, FileMode.OpenOrCreate, FileAccess.Write); //注意这个的属性和send端有所不同
BinaryWriter binarywrite = new BinaryWriter(fs);
int count;
int sum = 0;
while ((count = s2.Receive(b, 4098, SocketFlags.None)) != 0) //这个是接受文件流
{
binarywrite.Write(b, 0, count); //将接收的流用写成文件
sum += count;
this.richTextBox1.AppendText("接收了" + sum);
}
binarywrite.Close();
fs.Close();
s2.Close();
t2.Stop();
this.richTextBox1.Clear();
this.richTextBox1.AppendText("接收完成");
}
len = 0;
}
closing2 = true;
}
这是在一个子窗体里面的程序。havemess() 和ReceiveFile()这两个函数里面都有while(true)无限循环,要怎样才能关闭这个窗口,使得重新打开这个窗口时,不会异常提示“端口只能使用一次”。这个问题困扰了我1天。彻夜难眠....
我往这个窗体添加了一个关闭按钮,以下是关闭按钮的事件
private void BtnClose_Click(object sender, EventArgs e)//关闭子窗口
{
key = 1;//关闭标志
ss.SendMessage(Mains.localip, "closing", 0);//发送消息给主窗口,消息线程会接收
ss.SendMessage(Mains.localip, "closing", 1);//发送消息给主窗口,文件线程会接收
if (closing == true && closing2 == true)
{
this.Close();
}
}
当按下这个按钮时,key=1, ss.SendMessage(Mains.localip, "closing", 0);所发送的消息被havemess() 里面的
s = tl.AcceptSocket();接受,然后接收,程序继续执行
byte[] b = new byte[4098];
len = s.Receive(b);
if (key == 1)//现在判断到key=1了
{
tl.Stop();//终止了监听
break;//退出了这个循环
}
跳到函数末 closing = true;
对于
ss.SendMessage(Mains.localip, "closing", 1);被ReceiveFile()接收,程序执行
s2 = t2.AcceptSocket();
byte[] b = new byte[4098];
len = s2.Receive(b);
if (key == 1)
{
t2.Stop();//停止监听
break;//退出
}
跳到函数末 closing2 = true;
private void BtnClose_Click(object sender, EventArgs e)//关闭子窗口
{
key = 1;
ss.SendMessage(Mains.localip, "closing", 0);//发送消息给主窗口,消息线程会接收
ss.SendMessage(Mains.localip, "closing", 1);//发送消息给主窗口,文件线程会接收
if (closing == true && closing2 == true)//这时closing 和closing2都为true方可关闭这个窗口
{
this.Close();
}
}
这样就可以随时正常关闭开启这个子窗口了。
本人第一次写的,经验不足,希望可以帮到it界的朋友们...