日期:2014-05-18  浏览次数:20795 次

关于C# P2P技术的网络应用
C# code

private void button3_Click(object sender, EventArgs e)
        {
            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)
            {
                toolStripStatusLabel1.Text = "目标计算机拒绝连接请求!";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                IPAddress ip = new IPAddress(new byte[] { 192, 168, 1, 105 });
                tcplistener = new TcpListener(ip, 1234);
                tcplistener.Start();
                toolStripStatusLabel1.Text = "开始监听……";
                while (true)
                {
                    //等待客户机接入
                    tcpclient = tcplistener.AcceptTcpClient();
                    ns = tcpclient.GetStream();
                    //
                    byte[] buffer = new byte[1024];
                    int bytesRead = ns.Read(buffer, 0, 1024);
                    string msg = System.Text.Encoding.GetEncoding("gb2312").GetString(buffer);
                    this.textBox1.Text = msg;
                }
            }
            catch (System.Security.SecurityException)
            {
                StopListener();
                MessageBox.Show("防火墙安全错误!", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception)
            {
                StopListener();
                toolStripStatusLabel1.Text = "已停止监听!";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //
            StopListener();
            toolStripStatusLabel1.Text = "已停止监听!";
        }

        private void StopListener()
        {
            ns.Close();
            tcpclient.Close();
            tcplistener.Stop();
        }



总是在等待客户机接入那一步未响应 我在另一台机子上运行就报错说未实例化对象

------解决方案--------------------
namespace P2PTest

  {

   using System;

   using System.Net.Sockets;

   using System.Threading;

   public class Listener

   {

  private Thread th;

  private TcpListener tcpl;

  public bool listenerRun = true;

  //listenerRun为true,表示可以接受连接请求,false则为结束程序

  public Listener()//构造函数

  {

   th = new Thread(new ThreadStart(Listen));//新建一个用于监听的线程

   th.Start();//打开新线程

  }

  public void Stop()

  {

   tcpl.Stop();

   th.Abort();//终止线程

  }

  private void Listen()

  {

   try

   {

  tcpl = new TcpListener(5656);//在5656端口新建一个TcpListener对象

  tcpl.Start();

  Console.WriteLine("started listening..");

  while(listenerRun)//开始监听

  {

   Socket s = tcpl.AcceptSocket();

   string remote = s.RemoteEndPoint.ToString();

   Byte[] stream = new Byte[80];

   int i=s.Receive(stream);//接受连接请求的字节流

   string msg = "<" + remote + ">" + System.Text.Encoding.UTF8.GetString(stream);

   Console.WriteLine(msg);//在控制台显示字符串

  }