Socket编写一个局域网通信聊天软件

 服务器端:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net;using System.Net.Sockets;using System.Thr
摘要由CSDN通过智能技术生成

 

服务器端:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ChatServer
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.TextBox txtHost;
  private System.Windows.Forms.TextBox txtPort;
  private System.Windows.Forms.Button btnStart;
  private System.Windows.Forms.Button btnExit;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.ComboBox curUserList;
  private System.Windows.Forms.ListBox lstInfo;

  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  //该服务器默认的监听端口号
  static int port=1234;
  private TcpListener listener;
  private Socket tmpSocket;
  //最多客户端连接数
  static int MaxNum=100;
  //clients数组保持当前在线的client对象
  static ArrayList clients=new ArrayList();

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

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

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

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.txtHost = new System.Windows.Forms.TextBox();
   this.txtPort = new System.Windows.Forms.TextBox();
   this.btnStart = new System.Windows.Forms.Button();
   this.btnExit = new System.Windows.Forms.Button();
   this.label3 = new System.Windows.Forms.Label();
   this.curUserList = new System.Windows.Forms.ComboBox();
   this.lstInfo = new System.Windows.Forms.ListBox();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(0, 16);
   this.label1.Name = "label1";
   this.label1.TabIndex = 0;
   this.label1.Text = "主机号:";
   this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(0, 56);
   this.label2.Name = "label2";
   this.label2.TabIndex = 1;
   this.label2.Text = "端口号:";
   this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   //
   // txtHost
   //
   this.txtHost.Location = new System.Drawing.Point(104, 16);
   this.txtHost.Name = "txtHost";
   this.txtHost.Size = new System.Drawing.Size(128, 21);
   this.txtHost.TabIndex = 2;
   this.txtHost.Text = "192.168.0.163";
   //
   // txtPort
   //
   this.txtPort.Location = new System.Drawing.Point(104, 56);
   this.txtPort.Name = "txtPort";
   this.txtPort.ReadOnly = true;
   this.txtPort.Size = new System.Drawing.Size(128, 21);
   this.txtPort.TabIndex = 3;
   this.txtPort.Text = "";
   //
   // btnStart
   //
   this.btnStart.Location = new System.Drawing.Point(264, 16);
   this.btnStart.Name = "btnStart";
   this.btnStart.TabIndex = 4;
   this.btnStart.Text = "启动";
   this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
   //
   // btnExit
   //
   this.btnExit.Location = new System.Drawing.Point(264, 56);
   this.btnExit.Name = "btnExit";
   this.btnExit.TabIndex = 5;
   this.btnExit.Text = "退出";
   this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(24, 136);
   this.label3.Name = "label3";
   this.label3.TabIndex = 6;
   this.label3.Text = "当前在线用户:";
   this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   //
   // curUserList
   //
   this.curUserList.Location = new System.Drawing.Point(160, 136);
   this.curUserList.Name = "curUserList";
   this.curUserList.Size = new System.Drawing.Size(121, 20);
   this.curUserList.TabIndex = 7;
   //
   // lstInfo
   //
   this.lstInfo.ItemHeight = 12;
   this.lstInfo.Location = new System.Drawing.Point(24, 184);
   this.lstInfo.Name = "lstInfo";
   this.lstInfo.Size = new System.Drawi

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值