c#点对点聊天程序示例

2007-11-22 16:07

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
   private System.Windows.Forms.Label label3;
   private System.Windows.Forms.Label label4;
   private System.Windows.Forms.Label label5;
   private System.Windows.Forms.Label label6;
   private System.Windows.Forms.Label label7;
   private System.Windows.Forms.Label label8;
   private System.Windows.Forms.TextBox textBox1;
   private System.Windows.Forms.TextBox textBox2;
   private System.Windows.Forms.TextBox textBox3;
   private System.Windows.Forms.ListBox listBox1;
   private System.Windows.Forms.ListBox listBox2;
   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.Button button2;
   private System.Windows.Forms.Button button3;
   private System.Windows.Forms.Button button4;
   private System.Windows.Forms.TextBox textBox4;
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.StatusBar statusBar1;
   private System.Windows.Forms.StatusBarPanel statusBarPanel1;
   private System.Windows.Forms.StatusBarPanel statusBarPanel2;
   private System.ComponentModel.Container components = null;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>

   //创建线程用来侦听端口号
   private Thread th;
   //用来侦听端口号
   private TcpListener tlListen1;
   //设置标志位,判断侦听状态
   private bool listenerRun = true;
   //创建传送接收的基本 数据流实例
   private NetworkStream tcpStream;
   //创建向远程机器传送信息实例
   private StreamWriter reqStreamW;
   //创建远程连接的实例
   private TcpClient tcpc;
   //创建接收远程主机传来的数据的实例
   private Socket skSocket;


   public Form1()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
   }

   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    try
    {
     listenerRun = false;
     th.Abort();
     th = null;
     tlListen1.Stop();
     skSocket.Close();
    }
    catch(Exception)
    {
    }
    if(disposing)
    {
     if(components!=null)
     {
      components.Dispose();
     }
    }
    base.Dispose(disposing);
   
   }

   #region Windows 窗体设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    this.label3 = new System.Windows.Forms.Label();
    this.label4 = new System.Windows.Forms.Label();
    this.label5 = new System.Windows.Forms.Label();
    this.label6 = new System.Windows.Forms.Label();
    this.label7 = new System.Windows.Forms.Label();
    this.label8 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.listBox2 = new System.Windows.Forms.ListBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.button4 = new System.Windows.Forms.Button();
    this.textBox4 = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.statusBar1 = new System.Windows.Forms.StatusBar();
    this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
    this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
    ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
    this.SuspendLayout();
    //
    // label3
    //
    this.label3.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(255)));
    this.label3.Location = new System.Drawing.Point(24, 120);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(72, 23);
    this.label3.TabIndex = 2;
    this.label3.Text = "发送信息:";
    //
    // label4
    //
    this.label4.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(255)));
    this.label4.Location = new System.Drawing.Point(24, 312);
    this.label4.Name = "label4";
    this.label4.Size = new System.Drawing.Size(72, 23);
    this.label4.TabIndex = 3;
    this.label4.Text = "接收信息";
    //
    // label5
    //
    this.label5.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(255)));
    this.label5.Location = new System.Drawing.Point(24, 184);
    this.label5.Name = "label5";
    this.label5.Size = new System.Drawing.Size(72, 23);
    this.label5.TabIndex = 4;
    this.label5.Text = "发送的信息";
    //
    // label6
    //
    this.label6.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(255)));
    this.label6.Location = new System.Drawing.Point(248, 88);
    this.label6.Name = "label6";
    this.label6.Size = new System.Drawing.Size(56, 23);
    this.label6.TabIndex = 5;
    this.label6.Text = "本地端口";
    //
    // label7
    //
    this.label7.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(255)));
    this.label7.ForeColor = System.Drawing.Color.Black;
    this.label7.Location = new System.Drawing.Point(24, 32);
    this.label7.Name = "label7";
    this.label7.Size = new System.Drawing.Size(72, 23);
    this.label7.TabIndex = 0;
    this.label7.Text = "远程IP地址";
    //
    // label8
    //
    this.label8.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(255)));
    this.label8.Location = new System.Drawing.Point(24, 88);
    this.label8.Name = "label8";
    this.label8.Size = new System.Drawing.Size(72, 23);
    this.label8.TabIndex = 1;
    this.label8.Text = "远程端口号";
    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(128, 32);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(264, 21);
    this.textBox1.TabIndex = 6;
    this.textBox1.Text = "textBox1";
    //
    // textBox2
    //
    this.textBox2.Location = new System.Drawing.Point(128, 88);
    this.textBox2.Name = "textBox2";
    this.textBox2.TabIndex = 7;
    this.textBox2.Text = "textBox2";
    //
    // textBox3
    //
    this.textBox3.Location = new System.Drawing.Point(312, 88);
    this.textBox3.Name = "textBox3";
    this.textBox3.Size = new System.Drawing.Size(80, 21);
    this.textBox3.TabIndex = 8;
    this.textBox3.Text = "textBox3";
    //
    // listBox1
    //
    this.listBox1.ItemHeight = 12;
    this.listBox1.Location = new System.Drawing.Point(128, 160);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(264, 112);
    this.listBox1.TabIndex = 9;
    //
    // listBox2
    //
    this.listBox2.ItemHeight = 12;
    this.listBox2.Location = new System.Drawing.Point(128, 296);
    this.listBox2.Name = "listBox2";
    this.listBox2.Size = new System.Drawing.Size(264, 112);
    this.listBox2.TabIndex = 10;
    //
    // button1
    //
    this.button1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(128)));
    this.button1.ForeColor = System.Drawing.Color.Red;
    this.button1.Location = new System.Drawing.Point(408, 40);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(120, 40);
    this.button1.TabIndex = 11;
    this.button1.Text = "连接远程主机";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // button2
    //
    this.button2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(128)));
    this.button2.ForeColor = System.Drawing.Color.Red;
    this.button2.Location = new System.Drawing.Point(408, 144);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(120, 40);
    this.button2.TabIndex = 12;
    this.button2.Text = "侦听端口";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    //
    // button3
    //
    this.button3.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(128)));
    this.button3.ForeColor = System.Drawing.Color.Red;
    this.button3.Location = new System.Drawing.Point(408, 240);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(120, 40);
    this.button3.TabIndex = 13;
    this.button3.Text = "发送信息";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    //
    // button4
    //
    this.button4.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(128)));
    this.button4.ForeColor = System.Drawing.Color.Red;
    this.button4.Location = new System.Drawing.Point(408, 352);
    this.button4.Name = "button4";
    this.button4.Size = new System.Drawing.Size(120, 40);
    this.button4.TabIndex = 14;
    this.button4.Text = "断开连接";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    //
    // textBox4
    //
    this.textBox4.Location = new System.Drawing.Point(128, 120);
    this.textBox4.Name = "textBox4";
    this.textBox4.Size = new System.Drawing.Size(264, 21);
    this.textBox4.TabIndex = 15;
    this.textBox4.Text = "textBox4";
    //
    // label1
    //
    this.label1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
    this.label1.Location = new System.Drawing.Point(0, 0);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(544, 23);
    this.label1.TabIndex = 16;
    this.label1.Text = "                     版权所有                    网络05玄魂制作";
    //
    // statusBar1
    //
    this.statusBar1.Location = new System.Drawing.Point(0, 429);
    this.statusBar1.Name = "statusBar1";
    this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                        this.statusBarPanel1,
                        this.statusBarPanel2});
    this.statusBar1.ShowPanels = true;
    this.statusBar1.Size = new System.Drawing.Size(544, 96);
    this.statusBar1.TabIndex = 17;
    this.statusBar1.Text = "statusBar1";
    //
    // statusBarPanel1
    //
    this.statusBarPanel1.Text = "statusBarPanel1";
    this.statusBarPanel1.Width = 260;
    //
    // statusBarPanel2
    //
    this.statusBarPanel2.Text = "statusBarPanel2";
    this.statusBarPanel2.Width = 260;
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.BackColor = System.Drawing.Color.Black;
    this.ClientSize = new System.Drawing.Size(544, 525);
    this.Controls.Add(this.statusBar1);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.textBox4);
    this.Controls.Add(this.button4);
    this.Controls.Add(this.button3);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.listBox2);
    this.Controls.Add(this.listBox1);
    this.Controls.Add(this.textBox3);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label6);
    this.Controls.Add(this.label5);
    this.Controls.Add(this.label4);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.label7);
    this.Controls.Add(this.label8);
    this.Name = "Form1";
    this.Text = "点对点通讯程序";
    this.Load += new System.EventHandler(this.Form1_Load);
    ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
    this.ResumeLayout(false);

   }
   #endregion

   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main()
   {
    Application.Run(new Form1());
   }
   private void Listen()
   {
    string time = DateTime.Now.ToString();
    try
    {
     tlListen1 = new TcpListener(Int32.Parse(textBox3.Text));
     //侦听远程端口
     tlListen1.Start();
     statusBar1.Panels[1].Text = "正在监听......";
    
     skSocket = tlListen1.AcceptSocket();
    
     //获得远程计算机对应的网络远程终结点
     EndPoint tempRemoteEP = skSocket.RemoteEndPoint;
     //根据网络远程终结点获得远程计算机的名称
     IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;
     IPHostEntry host = Dns.GetHostByAddress(tempRemoteIP.Address);
     string HostName = host.HostName;
     statusBar1.Panels[1].Text = "'"+HostName+"'"+"远程计算机正确连接!";
     //循环侦听
     while(listenerRun)
     {
      //定义从远程计算机接收到的数据存放缓冲区
      Byte[]stream = new byte[80];
      //获取当前时间
   
      //接收数据并存放到定义的缓冲区,以指定的编码,从缓冲区中解析出内容
      int i = skSocket.ReceiveFrom(stream,ref tempRemoteEP);
      string sMessage = System.Text.Encoding.UTF8.GetString(stream);
    
      //显示接收的数据
      listBox2.Items.Add(time+""+HostName+":");
      listBox2.Items.Add(sMessage);

     }
    }
    catch(System.Security.SecurityException)
    {
     MessageBox.Show("防火墙安全错误!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
    }
   }

   private void button1_Click(object sender, System.EventArgs e)
   {
    //向远程主机提出申请,如果连接建立,则获得用以传送数据的数据流
    try
    {
     tcpc = new TcpClient(textBox1.Text,Int32.Parse(textBox2.Text));
   
     tcpStream = tcpc.GetStream();
     statusBar1.Panels[0].Text = "成功连接远程计算机!";
     button2.Enabled = true;
     button1.Enabled = false;
     button4.Enabled = true;
   
    }
    catch(Exception)
    {
     statusBar1.Panels[0].Text = "目标计算机拒绝连接请求!";

    }
   }

   private void button2_Click(object sender, System.EventArgs e)
   {
    th = new Thread(new ThreadStart(Listen));
    th.Start();
   }

   private void button3_Click(object sender, System.EventArgs e)
   {
    try
    {
     string sMsg= textBox4.Text;
     string MyName = Dns.GetHostName();
     reqStreamW = new StreamWriter(tcpStream);
     reqStreamW.Write(sMsg);
     reqStreamW.Flush();
     string time = DateTime.Now.ToString();
     listBox1.Items.Add(time+" " + MyName+":");
     listBox1.Items.Add(sMsg);
     textBox4.Clear();

    }
    catch(Exception)
    {
     statusBar1.Panels[0].Text= "无法发送到目标计算机!";

    }
   }

   private void button4_Click(object sender, System.EventArgs e)
   {
    listenerRun = false;
    tcpc.Close();
    statusBar1.Panels[0].Text = "断开连接!";
    button1.Enabled = true;
    button3.Enabled = false;
    button2.Enabled = true;
   }

   private void Form1_Load(object sender, System.EventArgs e)
   {
  
   }
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值