您当前的位置:中客资源站网络学院.NET专区综合应用 → 文章内容 退出登录 用户管理
本类热门文章
相关下载
简单的多人聊天(C#.Socket).
作者:中客资源  来源:中客资源  发布时间:2007-2-7 2:55:01

减小字体 增大字体

.Start();  
  }
 }
}


/***************************** client类 ********************/

/************************** 放于 chatServer 项目中 *********/

using System;
using System.Threading;

namespace Chat_Server
{
 using System.Net.Sockets;
 using System.Net;

 ///
 /// Client 的摘要说明。
 ///
 public class Client
 {
  private Thread clthread;
  private EndPoint endpoint;
  private string name;
  private Socket sock;

  public Client(string _name, EndPoint _endpoint, Thread _thread, Socket _sock)
  {
   // TODO: 在此处添加构造函数逻辑
   clthread = _thread;
   endpoint = _endpoint;
   name = _name;
   sock = _sock;
  }

  public override string ToString()
  {
   return endpoint.ToString()+ " : " + name;
  }

  public Thread CLThread
  {
   get{return clthread;}
   set{clthread = value;}
  }

  public EndPoint Host
  {
   get{return endpoint;}
   set{endpoint = value;}
  }

  public string Name
  {
   get{return name;}
   set{name = value;}
  }

  public Socket Sock
  {
   get{return sock;}
   set{sock = value;}
  }
 }
}

/***************************** chatClient ************************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Chat_Client
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.CheckBox checkBox1;
  private System.Windows.Forms.StatusBar statusBar1;

  NetworkStream ns;
  StreamReader sr;
  TcpClient clientsocket;
  bool connected;
  Thread receive;
  string serveraddress = "219.228.231.85";
  int serverport = 6666;

  private System.Windows.Forms.RichTextBox rtbChatIn;
  private System.Windows.Forms.ListBox lbChatters;
  private System.Windows.Forms.TextBox ChatOut;
  private System.Windows.Forms.Button btnDisconnect;
  private System.Windows.Forms.Button btnSend;
  private System.Windows.Forms.TextBox clientName;

  string clientname;
  private System.Windows.Forms.Button btnConnect;

  private System.ComponentModel.Container components = null;

  public Form1()
  {
   
   InitializeComponent();

  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(receive != null)
    {
     QuitChat();
    }
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.lbChatters = new System.Windows.Forms.ListBox();
   this.rtbChatIn = new System.Windows.Forms.RichTextBox();
   this.checkBox1 = new System.Windows.Forms.CheckBox();
   this.ChatOut = new System.Windows.Forms.TextBox();
   this.btnSend = new System.Windows.Forms.Button();
   this.statusBar1 = new System.Windows.Forms.StatusBar();
   this.btnDisconnect = new System.Windows.Forms.Button();
   this.clientName = new System.Windows.Forms.TextBox();
   this.btnConnect = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // lbChatters
   //
   this.lbChatters.ItemHeight = 12;
   this.lbChatters.Location = new System.Drawing.Point(32, 40);
   this.lbChatters.Name = "lbChatters";
   this.lbChatters.Size = new System.Drawing.Size(112, 172);
   this.lbChatters.TabIndex = 0;
   //
   // rtbChatIn
   //
   this.rtbChatIn.Location = new System.Drawing.Point(160, 40);
   this.rtbChatIn.Name = "rtbChatIn";
   this.rtbChatIn.Size = new System.Drawing.Size(208, 176);
   this.rtbChatIn.TabIndex = 2;
   this.rtbChatIn.Text = "";
   //
   // checkBox1
   //
   this.checkBox1.Location = new System.Drawing.Point(16, 248);
   this.checkBox1.Name = "checkBox1";
   this.checkBox1.TabIndex = 3;
   this.checkBox1.Text = "checkBox1";
   //
   // ChatOut
   //
   this.ChatOut.Location = new System.Drawing.Point(136, 248);
   this.ChatOut.Name = "ChatOut";
   this.ChatOut.Size = new System.Drawing.Size(136, 21);
   this.ChatOut.TabIndex = 4;
   this.ChatOut.Text = "message";
   //
   // btnSend
   //
   this.btnSend.Location = new System.Drawing.Point(336, 248);
   this.btnSend.Name = "btnSend";
   this.btnSend.TabIndex = 5;
   this.btnSend.Text = "send";
   this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
   //
   // statusBar1
   //
   this.statusBar1.Location = new System.Drawing.Point(0, 287);
   this.statusBar1.Name = "statusBar1";
   this.statusBar1.Size = new System.Drawing.Size(464, 22);
   this.statusBar1.TabIndex = 6;
   this.statusBar1.Text = "statusBar1";
   //
   // btnDisconnect
   //
   this.btnDisconnect.Enabled = false;
   this.btnDisconnect.Location = new System.Drawing.Point(392, 112);
   this.btnDisconnect.Name = "btnDisconnect";
   this.btnDisconnect.Size = new System.Drawing.Size(64, 32);
   this.btnDisconnect.TabIndex = 7;
   this.btnDisconnect.Text = "断开";
   this.btnDisconnect.Click += new System.EventHandler(this.btnDisconnect_Click);
   //
   // clientName
   //
   this.clientName.Location = new System.Drawing.Point(96, 8);
   this.clientName.Name = "clientName";
   this.clientName.TabIndex = 8;
   this.clientName.Text = "name";
   //
   // btnConnect
   //
   this.btnConnect.Location = new System.Drawing.Point(392, 56);
   this.btnConnect.Name = "btnConnect";
   this.btnConnect.Size = new System.Drawing.Size(64, 32);
   this.btnConnect.TabIndex = 9;
   this.btnConnect.Text = "连接";
   this.btnConnect.Click += new

上一页  [1] [2] [3]  下一页

[] [返回上一页] [打 印]
文章评论 (评论内容只代表网友观点,与本站立场无关!)

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码