using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Threading;
namespace p2pChat
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool listenerRun = true;
private void Send()
{
try
{
string msg = " < "+textBox3.Text+ "> "+textBox2.Text;
TcpClient tcpc = new TcpClient(textBox1.Text, 5656);
NetworkStream tcpStream = tcpc.GetStream();
StreamWriter reqStreamW = new StreamWriter(tcpStream);
reqStreamW.Write (msg);
reqStreamW.Flush();
tcpStream.Close();
tcpc.Close();
richTextBox1.AppendText(msg);
textBox2.Clear();
}
catch(Exception)
{
label3.Text = "目标计算机拒绝连接请求! ";
}
}
private void Listen(){
try
{
TcpListener tcp1 = new TcpListener(5656);
tcp1.Start();
label3.Text = "正在监听... ";
while (listenerRun)
{
Socket s = tcp1.AcceptSocket();
Byte[] stream = new byte[80];
int i = s.Receive(stream);
string message = System.Text.Encoding.UTF8.GetString(stream);
richTextBox1.AppendText(message);
}
}
catch(System.Security.SecurityException)
{
MessageBox.Show( "防火墙安全错误 ", "错误 ",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
catch(Exception)
{
label3.Text = "停止监听... ";
}
}
private void Stop()
{
tcpl.Stop();//提示这二行错误
th.Abort();
}
[STAThread]
private void button1_Click(object sender, System.EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
Thread th = new Thread(new ThreadStart(Listen));
th.Start();
}
private void button2_Click(object sender, System.EventArgs e)
{
button1.Enabled = true;
button2.Enabled = false;
listenerRun = false;
Stop();
}
private void button3_Click(object sender, System.EventArgs e)
{
Send();
}
}
}
说:
错误 当前上下文中不存在名称“tcpl” D:\11\C#\p2pChat\p2pChat\Form1.cs 77 13 p2pChat
错误 前上下文中不存在名称“th” D:\11\C#\p2pChat\p2pChat\Form1.cs 78 13 p2pChat