using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;using System.Net ;
using System.Net.Sockets ;namespace Chat_Clinet
{
/// <summary>
/// Login 的摘要说明。
/// </summary>
public class Login : System.Windows.Forms.Form
{
private System.Windows .Forms .Button btnCancel;
private System.Windows .Forms .Button btnLogin;
private System.Windows .Forms .TextBox txtAlias;
private System.Windows .Forms .Label label3;
private System.Windows .Forms .TextBox txtPort;
private System.Windows .Forms .Label label2;
private System.Windows .Forms .TextBox txtHost;
private System.Windows .Forms .Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; //tcpClient是Login的一个属性,它用于创建客户端套接字 public TcpClient tcpClient;
//Alias是Login的一个属性,它向CharClient窗体传送用户名
public string Alias=""; public Login()
{
//
//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.btnCancel=new System.Windows.Forms.Button();
this.btnLogin=new System.Windows.Forms.Button();
this.txtAlias=new System.Windows.Forms.TextBox();
this.label3=new System.Windows.Forms.Label();
this.txtPort=new System.Windows.Forms.TextBox();
this.label2=new System.Windows.Forms.Label();
this.txtHost=new System.Windows.Forms.TextBox();
this.label1=new System.Windows.Forms.Label();
this.SuspendLayout();
//
//btnCancel
//
this.btnCancel.Location=new System.Drawing.Point(160,168);
this.btnCancel.Name="btnCancel";
this.btnCancel.TabIndex=3;
this.btnCancel.Text="取消";
this.btnCancel+=new System.EventHandler(this.btnCancel_Click);
//
//btnLogin
//
this.btnLogin.DialogResult=System.Windows.Forms.DialogResult.OK;
this.btnLogin.Location=new System.Drawing.Point(48,168);
this.btnLogin.Name="btnLogin";
this.btnLogin.TabIndex=2;
this.btnLogin.Text="登录"; this.btnLogin.Click+=new Ststem.EventHander(this.btnLogin_Click);
//
//txtAlias
//
this.txtAlias.Location=new Ststem.Drawing.Point(136,112);
this.txtAlias.Name="txtAlias";
this.txtAlias.TabIndex=1;
this.txtAlias.Text="";
//
//label3
//
this.label3.Location=new System.Drawing.Point(32,112);
this.label3.Name="label3";
this.label3.TabIndex=12;
this.label3.Text="用户名";
this.label3.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//txtPort
//
this.txtPort.Location= new System.Drawing.Point(152,64);
this.txtPort.Name="txtPort";
this.txtPort.ReadOnly=true;
this.txtPort.Size= new System.Drawing.Point(56,21);
this.txtPort.TabIndex=11;
this.txtPort.Text="1234";
//
//label2
//
this.label2.Location=new System.Drawing.Point(32,64);
this.label2.Name="label2";
this.label2.TabIndex=10;
this.label2.Text="端口号";
this.label2.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//txtHost
//
this.txtHost.Location=new System.Drawing.Point(136,24);
this.txtHost.Name="txtHost";
this.txtHost.TabIndex=9;
this.txtHost.Text="210.77.26.37";
//
//label1
// this.label1.Location=new System.Drawing.Point(32,24);
this.label1.Name="label1";
this.label1.TabIndex=8;
this.label1.Text-"服务器地址";
this.label1.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//Login
//
this.AutoScaleBaseSize=new System.Drawing.Size(6,14);
this.ClientSize=new System.Drawing.Size(312,221);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.txtAlias);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.Label2);
this.Controls.Add(this.texHost);
this.Controls.Add(this.Label1);
this.Name="login";
this.Text="Login";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Login());
} private void btnLogin_Click(object sender,System.EventArgs e)
{
txtAlias.Text=txtAlias.Text.Trim();
if(txtAlias.Text.Length==0)
{
MessageBox.Show("请输入您的昵称!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
txtAlias.Focus();
return;
}
try
{//创建一个客户端套接字,它是Login的一个公共属性,将被传递给ChatClient窗体
tcpClient=new TcpClient();
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect(IPAddress.Parse(txtHost.Text));
Int32.Parse(txtPort.Text);
Alias=txtAlias.Text;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnCancel_Click(object sender,System.EventArgs e)
{
ChatClient.Login.ActiveForm.Close();
} }
} /// <summary>
/// The main point for the application.
/// </summary>
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;using System.Net ;
using System.Net.Sockets ;namespace Chat_Clinet
{
/// <summary>
/// Login 的摘要说明。
/// </summary>
public class Login : System.Windows.Forms.Form
{
private System.Windows .Forms .Button btnCancel;
private System.Windows .Forms .Button btnLogin;
private System.Windows .Forms .TextBox txtAlias;
private System.Windows .Forms .Label label3;
private System.Windows .Forms .TextBox txtPort;
private System.Windows .Forms .Label label2;
private System.Windows .Forms .TextBox txtHost;
private System.Windows .Forms .Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; //tcpClient是Login的一个属性,它用于创建客户端套接字 public TcpClient tcpClient;
//Alias是Login的一个属性,它向CharClient窗体传送用户名
public string Alias=""; public Login()
{
//
//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.btnCancel=new System.Windows.Forms.Button();
this.btnLogin=new System.Windows.Forms.Button();
this.txtAlias=new System.Windows.Forms.TextBox();
this.label3=new System.Windows.Forms.Label();
this.txtPort=new System.Windows.Forms.TextBox();
this.label2=new System.Windows.Forms.Label();
this.txtHost=new System.Windows.Forms.TextBox();
this.label1=new System.Windows.Forms.Label();
this.SuspendLayout();
//
//btnCancel
//
this.btnCancel.Location=new System.Drawing.Point(160,168);
this.btnCancel.Name="btnCancel";
this.btnCancel.TabIndex=3;
this.btnCancel.Text="取消";
this.btnCancel+=new System.EventHandler(this.btnCancel_Click);
//
//btnLogin
//
this.btnLogin.DialogResult=System.Windows.Forms.DialogResult.OK;
this.btnLogin.Location=new System.Drawing.Point(48,168);
this.btnLogin.Name="btnLogin";
this.btnLogin.TabIndex=2;
this.btnLogin.Text="登录"; this.btnLogin.Click+=new Ststem.EventHander(this.btnLogin_Click);
//
//txtAlias
//
this.txtAlias.Location=new Ststem.Drawing.Point(136,112);
this.txtAlias.Name="txtAlias";
this.txtAlias.TabIndex=1;
this.txtAlias.Text="";
//
//label3
//
this.label3.Location=new System.Drawing.Point(32,112);
this.label3.Name="label3";
this.label3.TabIndex=12;
this.label3.Text="用户名";
this.label3.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//txtPort
//
this.txtPort.Location= new System.Drawing.Point(152,64);
this.txtPort.Name="txtPort";
this.txtPort.ReadOnly=true;
this.txtPort.Size= new System.Drawing.Point(56,21);
this.txtPort.TabIndex=11;
this.txtPort.Text="1234";
//
//label2
//
this.label2.Location=new System.Drawing.Point(32,64);
this.label2.Name="label2";
this.label2.TabIndex=10;
this.label2.Text="端口号";
this.label2.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//txtHost
//
this.txtHost.Location=new System.Drawing.Point(136,24);
this.txtHost.Name="txtHost";
this.txtHost.TabIndex=9;
this.txtHost.Text="210.77.26.37";
//
//label1
// this.label1.Location=new System.Drawing.Point(32,24);
this.label1.Name="label1";
this.label1.TabIndex=8;
this.label1.Text-"服务器地址";
this.label1.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//Login
//
this.AutoScaleBaseSize=new System.Drawing.Size(6,14);
this.ClientSize=new System.Drawing.Size(312,221);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.txtAlias);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.Label2);
this.Controls.Add(this.texHost);
this.Controls.Add(this.Label1);
this.Name="login";
this.Text="Login";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Login());
} private void btnLogin_Click(object sender,System.EventArgs e)
{
txtAlias.Text=txtAlias.Text.Trim();
if(txtAlias.Text.Length==0)
{
MessageBox.Show("请输入您的昵称!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
txtAlias.Focus();
return;
}
try
{//创建一个客户端套接字,它是Login的一个公共属性,将被传递给ChatClient窗体
tcpClient=new TcpClient();
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect(IPAddress.Parse(txtHost.Text));
Int32.Parse(txtPort.Text);
Alias=txtAlias.Text;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnCancel_Click(object sender,System.EventArgs e)
{
ChatClient.Login.ActiveForm.Close();
} }
} /// <summary>
/// The main point for the application.
/// </summary>
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;using System.Net ;
using System.Net.Sockets ;namespace Chat_Clinet
{
/// <summary>
/// Login 的摘要说明。
/// </summary>
public class Login : System.Windows.Forms.Form
{
private System.Windows .Forms .Button btnCancel;
private System.Windows .Forms .Button btnLogin;
private System.Windows .Forms .TextBox txtAlias;
private System.Windows .Forms .Label label3;
private System.Windows .Forms .TextBox txtPort;
private System.Windows .Forms .Label label2;
private System.Windows .Forms .TextBox txtHost;
private System.Windows .Forms .Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; //tcpClient是Login的一个属性,它用于创建客户端套接字 public TcpClient tcpClient;
//Alias是Login的一个属性,它向CharClient窗体传送用户名
public string Alias=""; public Login()
{
//
//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.btnCancel=new System.Windows.Forms.Button();
this.btnLogin=new System.Windows.Forms.Button();
this.txtAlias=new System.Windows.Forms.TextBox();
this.label3=new System.Windows.Forms.Label();
this.txtPort=new System.Windows.Forms.TextBox();
this.label2=new System.Windows.Forms.Label();
this.txtHost=new System.Windows.Forms.TextBox();
this.label1=new System.Windows.Forms.Label();
this.SuspendLayout();
//
//btnCancel
//
this.btnCancel.Location=new System.Drawing.Point(160,168);
this.btnCancel.Name="btnCancel";
this.btnCancel.TabIndex=3;
this.btnCancel.Text="取消";
this.btnCancel.Click +=new System.EventHandler(this.btnCancel_Click);
//
//btnLogin
//
this.btnLogin.DialogResult=System.Windows.Forms.DialogResult.OK;
this.btnLogin.Location=new System.Drawing.Point(48,168);
this.btnLogin.Name="btnLogin";
this.btnLogin.TabIndex=2;
this.btnLogin.Text="登录"; this.btnLogin.Click+=new System.EventHandler(this.btnLogin_Click);
//
//txtAlias
//
this.txtAlias.Location=new System.Drawing.Point(136,112);
this.txtAlias.Name="txtAlias";
this.txtAlias.TabIndex=1;
this.txtAlias.Text="";
//
//label3
//
this.label3.Location=new System.Drawing.Point(32,112);
this.label3.Name="label3";
this.label3.TabIndex=12;
this.label3.Text="用户名";
this.label3.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//txtPort
//
this.txtPort.Location= new System.Drawing.Point(152,64);
this.txtPort.Name="txtPort";
this.txtPort.ReadOnly=true;
this.txtPort.TabIndex=11;
this.txtPort.Text="1234";
//
//label2
//
this.label2.Location=new System.Drawing.Point(32,64);
this.label2.Name="label2";
this.label2.TabIndex=10;
this.label2.Text="端口号";
this.label2.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//txtHost
//
this.txtHost.Location=new System.Drawing.Point(136,24);
this.txtHost.Name="txtHost";
this.txtHost.TabIndex=9;
this.txtHost.Text="210.77.26.37";
//
//label1
// this.label1.Location=new System.Drawing.Point(32,24);
this.label1.Name="label1";
this.label1.TabIndex=8;
this.label1.Text="服务器地址";
this.label1.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//Login
//
this.AutoScaleBaseSize=new System.Drawing.Size(6,14);
this.ClientSize=new System.Drawing.Size(312,221);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.txtAlias);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtHost);
this.Controls.Add(this.label1);
this.Name="login";
this.Text="Login";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Login());
} private void btnLogin_Click(object sender,System.EventArgs e)
{
txtAlias.Text=txtAlias.Text.Trim();
if(txtAlias.Text.Length==0)
{
MessageBox.Show("请输入您的昵称!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
txtAlias.Focus();
return;
}
try
{//创建一个客户端套接字,它是Login的一个公共属性,将被传递给ChatClient窗体
tcpClient=new TcpClient();
//向指定的IP地址的服务器发出连接请求
// tcpClient.Connect(IPAddress.Parse(txtHost.Text), Int32.Parse(txtPort.Text));
Alias=txtAlias.Text;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnCancel_Click(object sender,System.EventArgs e)
{
//ChatClient.Login.ActiveForm.Close();
} }
}
这又是为什么啊?而且界面上什么也没有
在Login.cs文件中为了使用Socket对象,在代码文件中加入名字空间System.Net和System.Net.Sockets的引用,它主要实现了创建客户端套接字同时连接到服务器指定端口把用户名和创建的客户端套接字传递给ChatClient窗体,具体实现代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;using System.Net ;
using System.Net .Sockets ;namespace CharClient
{
/// <summary>
/// Summary description for Login.
/// </summary>
public class Login : System.Windows.Forms.Form
{
private System.Windows .Forms .Button btnCancel;
private System.Windows .Forms .Button btnLogin;
private System.Windows .Forms .TextBox txtAlias;
private System.Windows .Forms .Label label3;
private System.Windows .Forms .TextBox txtPort;
private System.Windows .Forms .Label label2;
private System.Windows .Forms .TextBox txtHost;
private System.Windows .Forms .Label label1;
/// <summary>
/// Required designer variable
/// </summary>
private System.ComponentModel.Container components = null;
//tcpClient是Login的一个属性,它用于创建客户端套接字
public TopClient tcpClient;
//Alias是Login的一个属性,它向CharClient窗体传送用户名
public string Alias=""; public Login()
{
//
//Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any redources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support-do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnCancel=new System.Windows.Forms.Button();
this.btnLogin=new System.Windows.Forms.Button();
this.txtAlias=new System.Windows.Forms.TextBox();
this.label3=new System.Windows.Forms.Label();
this.txtPort=new System.Windows.Forms.TextBox();
this.label2=new System.Windows.Forms.Label();
this.txtHost=new System.Windows.Forms.TextBox();
this.label1=new System.Windows.Forms.Label();
this.SuspendLayout();
//
//btnCancel
//
this.btnCancel.Location=new System.Drawing.Point(160,168);
this.btnCancel.Name="btnCancel";
this.btnCancel.TabIndex=3;
this.btnCancel.Text="取消";
this.btnCancel+=new System.EventHandler(this.btnCancel_Click);
//
//btnLogin
//
this.btnLogin.DialogResult=System.Windows.Forms.DialogResult.OK;
this.btnLogin.Location=new System.Drawing.Point(48,168);
this.btnLogin.Name="btnLogin";
this.btnLogin.TabIndex=2;
this.btnLogin.Text="登录";this.btnLogin.Click+=new Ststem.EventHander(this.btnLogin_Click);
//
//txtAlias
//
this.txtAlias.Location=new Ststem..Drawing.Point(136,112);
this.txtAlias.Name=”txtAlias”;
this.txtAlias.TabIndex=1;
this.txtAlias.Text=””;
//
//label3
//
this.label3.Location=new System.Drawing.Point(32,112);
this.label3.Name=”label3”;
this.label3.TabIndex=12;
this.label3.Text=”用户名”;
this.label3.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//txtPort
//
this.txtPort.Location= new System.Drawing.Point(152,64);
this.txtPort.Name=”txtPort”;
this.txtPort.ReadOnly=true;
this.txtPort.Size= new System.Drawing.Point(56,21);
this.txtPort.TabIndex=11;
this.txtPort.Text=”1234”;
//
//label2
//
this.label2.Location=new System.Drawing.Point(32,64);
this.label2.Name=”label2”;
this.label2.TabIndex=10;
this.label2.Text=”端口号”;
this.label2.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//txtHost
//
this.txtHost.Location=new System.Drawing.Point(136,24);
this.txtHost.Name=”txtHost”;
this.txtHost.TabIndex=9;
this.txtHost.Text=”210.77.26.37”;
//
//label1
// this.label1.Location=new System.Drawing.Point(32,24);
this.label1.Name=”label1”;
this.label1.TabIndex=8;
this.label1.Text-“服务器地址”;
this.label1.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//
//Login
//
this.AutoScaleBaseSize=new System.Drawing.Size(6,14);
this.ClientSize=new System.Drawing.Size(312,221);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.txtAlias);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.Label2);
this.Controls.Add(this.texHost);
this.Controls.Add(this.Label1);
this.Name=”login”;
this.Text=”Login”;
this.ResumeLayout(false);
}
#endregion
在用户登陆窗口中,当单击“登陆”按钮时,便进入btnLogin_Click 处理程序。在btnLogin_Click处理程序中,对输入的用户名进行简单的判断,并且将正确的用户名保存,同时创建客户端的套接字并且连接到服务器,具体代码实现如下:
Private void btnLogin_Click(object sender,System.EventArgs e);
{
txtAlias.Text=txtAlias.Text.Trim();
if(txtAlias.Text.Length==0)
{
MessageBox.Show(“请输入您的昵称!”,”提示信息”,
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
txtAlias.Focus();
return;
}
try
{//创建一个客户端套接字,它是Login的一个公共属性,将被传递给ChatClient窗体
tcpClient=new TcpClient();
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect(IPAddress.Parse(txtHost.Text),
Int32.Parse(txtPort.Text);
Alias=txtAlias.Text;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnCancel_Click(object sender,System.EventArgs e)
{
ChatClient.Login.ActiveForm.Close();
}
}
}