C#客户端服务器与多客户端服务器C#
有人能给我解释一下c#中单客户端服务器和多客户端服务器的区别吗 例如,如果我想同时与多个客户端建立聊天室,我不确定我是否理解正确,但我们是否只为每个客户端分配新的IP,然后服务器读取它们 这是客户端代码C#客户端服务器与多客户端服务器C#,c#,networking,server,client,C#,Networking,Server,Client,有人能给我解释一下c#中单客户端服务器和多客户端服务器的区别吗 例如,如果我想同时与多个客户端建立聊天室,我不确定我是否理解正确,但我们是否只为每个客户端分配新的IP,然后服务器读取它们 这是客户端代码 public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); read
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
read=new Thread(new ThreadStart(RunClient));
read.Start();
}
/// <summary>
/// Clean up any resources 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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
//this.axSkin1 = new AxACTIVESKINLib.AxSkin();
this.tmsg = new System.Windows.Forms.TextBox();
this.bsend = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.tchat = new System.Windows.Forms.RichTextBox();
this.Message = new System.Windows.Forms.GroupBox();
this.Box1 = new System.Windows.Forms.PictureBox();
//((System.ComponentModel.ISupportInitialize)(this.axSkin1)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// axSkin1
//
//this.axSkin1.Enabled = true;
//this.axSkin1.Location = new System.Drawing.Point(368, 272);
//this.axSkin1.Name = "axSkin1";
//this.axSkin1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSkin1.OcxState")));
//this.axSkin1.Size = new System.Drawing.Size(32, 32);
//this.axSkin1.TabIndex = 0;
////
// tmsg
//
this.tmsg.ForeColor = System.Drawing.Color.Coral;
this.tmsg.Location = new System.Drawing.Point(32, 268);
this.tmsg.Name = "tmsg";
this.tmsg.Size = new System.Drawing.Size(336, 20);
this.tmsg.TabIndex = 0;
this.tmsg.Text = "";
this.tmsg.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tmsg_KeyPress);
//
// bsend
//
this.bsend.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.bsend.ForeColor = System.Drawing.Color.OrangeRed;
this.bsend.Location = new System.Drawing.Point(168, 311);
this.bsend.Name = "bsend";
this.bsend.Size = new System.Drawing.Size(64, 24);
this.bsend.TabIndex = 3;
this.bsend.Text = "&send";
this.bsend.Click += new System.EventHandler(this.bsend_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.tchat);
this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.groupBox1.ForeColor = System.Drawing.Color.Black;
this.groupBox1.Location = new System.Drawing.Point(24, 56);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(352, 176);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Message Exchange";
//
// tchat
//
this.tchat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.tchat.Location = new System.Drawing.Point(16, 24);
this.tchat.Name = "tchat";
this.tchat.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
this.tchat.Size = new System.Drawing.Size(320, 136);
this.tchat.TabIndex = 0;
this.tchat.Text = "";
//
// Message
//
this.Message.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.Message.ForeColor = System.Drawing.Color.Black;
this.Message.Location = new System.Drawing.Point(24, 244);
this.Message.Name = "Message";
this.Message.Size = new System.Drawing.Size(352, 56);
this.Message.TabIndex = 5;
this.Message.TabStop = false;
this.Message.Text = "Message";
//
// Box1
//
this.Box1.Location = new System.Drawing.Point(299, 3);
this.Box1.Name = "Box1";
this.Box1.Size = new System.Drawing.Size(72, 51);
this.Box1.TabIndex = 6;
this.Box1.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.ClientSize = new System.Drawing.Size(400, 342);
this.Controls.Add(this.Box1);
this.Controls.Add(this.bsend);
this.Controls.Add(this.tmsg);
//this.Controls.Add(this.axSkin1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.Message);
this.Location = new System.Drawing.Point(432, 408);
this.MaximumSize = new System.Drawing.Size(408, 376);
this.MinimumSize = new System.Drawing.Size(408, 376);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Client";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
//((System.ComponentModel.ISupportInitialize)(this.axSkin1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
//axSkin1.ApplySkin((int)Handle);
}
//---------------------------------------------------------
public void RunClient()
{
try
{
this.Icon=new Icon("1.ico");
this.Box1.Image=Image.FromFile("1.gif");
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
try
{
tchat.Text="trying to connect to the server";
// Step 1: create TcpClient and connect to server
client=new TcpClient();
client.Connect("LocalHost",20000);
// Step 2: get NetworkStream associated with TcpClient
stream=client.GetStream();
// create objects for writing and reading across stream
reader=new BinaryReader(stream);
writer=new BinaryWriter(stream);
//send welcome to server
writer.Write("Welcome server");
tchat.Text+="\r\n Client :Welcome server";
string reply;
while(true)
{
//step 3: read message from server
try
{
reply=reader.ReadString();
int len=tchat.Text.Length;
tchat.AppendText("\r\nServer :"+reply);
tchat.Select(len,10+reply.Length);
tchat.SelectionColor=Color.Red;
tchat.ScrollToCaret();
}
//handle execption if the server terminate
catch(Exception)
{
//step 5:close connection
MessageBox.Show("Can not find server..!");
reader.Close();
writer.Close();
stream.Close();
client.Close();
}
}
}
catch(Exception)
{
MessageBox.Show(this,"Error while connecting to server",
this.Text,MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
private void tmsg_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar=='\r'&&client!=null)
{
writer.Write(tmsg.Text);
int len=tchat.TextLength;
tchat.AppendText("\r\nClient :"+tmsg.Text);
tchat.Select(len,10+tmsg.TextLength);
tchat.SelectionColor=Color.Blue;
tmsg.Clear();
}
}
private void bsend_Click(object sender, System.EventArgs e)
{
if(client!=null)
{
writer.Write(tmsg.Text);
int len=tchat.TextLength;
tchat.AppendText("\r\nClient :"+tmsg.Text);
tchat.Select(len,10+tmsg.TextLength);
tchat.SelectionColor=Color.Blue;
tmsg.Clear();
}
}
}
public Form1()
{
//
//需要Windows窗体设计器支持
//
初始化组件();
read=新线程(新线程开始(RunClient));
read.Start();
}
///
///清理所有正在使用的资源。
///
受保护的覆盖无效处置(布尔处置)
{
如果(处置)
{
if(组件!=null)
{
组件。Dispose();
}
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
System.Resources.ResourceManager Resources=新的System.Resources.ResourceManager(typeof(Form1));
//this.axSkin1=新的AxACTIVESKINLib.AxSkin();
this.tmsg=new System.Windows.Forms.TextBox();
this.bsend=new System.Windows.Forms.Button();
this.groupBox1=new System.Windows.Forms.GroupBox();
this.tchat=new System.Windows.Forms.RichTextBox();
this.Message=new System.Windows.Forms.GroupBox();
this.Box1=new System.Windows.Forms.PictureBox();
//((System.ComponentModel.ISupportInitialize)(this.axSkin1)).BeginInit();
this.groupBox1.SuspendLayout();
这个.SuspendLayout();
//
//axSkin1
//
//this.axSkin1.Enabled=true;
//this.axSkin1.Location=新系统图点(368272);
//this.axSkin1.Name=“axSkin1”;
//this.axSkin1.OcxState=((System.Windows.Forms.AxHost.State)(resources.GetObject(“axSkin1.OcxState”));
//this.axSkin1.Size=新系统.Drawing.Size(32,32);
//this.axSkin1.TabIndex=0;
////
//tmsg
//
this.tmsg.ForeColor=System.Drawing.Color.Coral;
this.tmsg.Location=新系统图纸点(32268);
this.tmsg.Name=“tmsg”;
this.tmsg.Size=新系统图纸尺寸(336,20);
this.tmsg.TabIndex=0;
this.tmsg.Text=“”;
this.tmsg.KeyPress+=新的System.Windows.Forms.KeyPressEventHandler(this.tmsg_KeyPress);
//
//B结束
//
this.bsend.FlatStyle=System.Windows.Forms.FlatStyle.Flat;
this.bsend.ForeColor=System.Drawing.Color.oranged;
this.bsend.Location=新系统图点(168,311);
this.bsend.Name=“bsend”;
this.bsend.Size=新系统图尺寸(64,24);
this.bsend.TabIndex=3;
this.bsend.Text=“&send”;
this.bsend.Click+=新建System.EventHandler(this.bsend\u Click);
//
//groupBox1
//
this.groupBox1.Controls.Add(this.tchat);
this.groupBox1.Font=new System.Drawing.Font(“Microsoft无衬线”,8.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((System.Byte)(178));
this.groupBox1.ForeColor=System.Drawing.Color.Black;
this.groupBox1.Location=新系统.图纸.点(24,56);
this.groupBox1.Name=“groupBox1”;
this.groupBox1.Size=新系统图尺寸(352176);
this.groupBox1.TabIndex=4;
this.groupBox1.TabStop=false;
this.groupBox1.Text=“消息交换”;
//
//tchat
//
this.tchat.Font=new System.Drawing.Font(“Microsoft Sans Serif”,8.25F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((System.Byte)(178));
this.tchat.Location=新系统图纸点(16,24);
this.tchat.Name=“tchat”;
this.tchat.ScrollBars=System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
this.tchat.Size=新系统图纸尺寸(320136);
this.tchat.TabIndex=0;
this.tchat.Text=“”;
//
//信息
//
this.Message.Font=new System.Drawing.Font(“Microsoft Sans Serif”,8.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((System.Byte)(178));
this.Message.ForeColor=System.Drawing.Color.Black;
this.Message.Location=新系统图点(244);
this.Message.Name=“Message”;
this.Message.Size=新系统.图纸.尺寸(352,56);
this.Message.TabIndex=5;
this.Message.TabStop=false;
this.Message.Text=“Message”;
//
//框1
//
this.Box1.Location=新系统图纸点(299,3);
this.Box1.Name=“Box1”;
this.Box1.Size=新系统图纸尺寸(72,51);
this.Box1.TabIndex=6;
this.Box1.TabStop=false;
//
//表格1
//
this.AutoScaleBaseSize=新系统.图纸.尺寸(5,13);
this.BackColor=System.Drawing.SystemColors.controllight;
this.ClientSize=新系统图尺寸(400342);
this.Controls.Add(this.Box1);
this.Controls.Add(this.bsend);
this.Controls.Add(this.tmsg);
//this.Controls.Add(this.axSkin1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.Message);
该位置=新系统图纸点(432408);
this.MaximumSize=新系统图纸尺寸(408376);
this.MinimumSize=新系统图纸尺寸(408376);
this.Name=“Form1”;
this.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text=“Client”;
this.Closing+=new System.ComponentModel.CancelEventHandler(this.Form1\u Closing);
此.Load+=新系统