C#聊天程序服务器
作者:网络转载 发布时间:[ 2013/8/14 13:40:22 ] 推荐标签:
/ /
/ / textBox1的,,
/ /
=新System.Drawing.Point this.textBox1.Location(88,16);
this.textBox1.Multiline = TRUE;
this.textBox1.Name =“textBox1的”;
this.textBox1.Size,=新System.Drawing.Size(104,24);
this.textBox1.TabIndex = 1;
this.textBox1.Text =“”;
/ /
/ / textBox2中,,
/ /
this.textBox2.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.textBox2.Location =:System.Drawing.Point新(8,64);
this.textBox2.Multiline = TRUE;
this.textBox2.Name =“textBox2中”;
this.textBox2.ReadOnly = TRUE;
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical的;
新System.Drawing.Size this.textBox2.Size =(320,80);
this.textBox2.TabIndex = 2;
this.textBox2.Text =“”;
/ /
/ /按钮
/ /
this.button1.Location =:System.Drawing.Point新(208,16);
this.button1.Name =“Button1”的;
this.button1.Size =新System.Drawing.Size(64,24);
this.button1.TabIndex = 3;
this.button1.Text =“登录”;
this.button1.Click + = System.EventHandler(this.LoginBtn_click)的;
/ /
/ /按钮
/ /
this.button2.Location =新System.Drawing.Point(48,280);
this.button2.Name =“button2的”;
this.button2.Size,=新System.Drawing.Size(72,24);
this.button2.TabIndex = 4;
this.button2.Text =“发送”;
this.button2.Click + = System.EventHandler(this.SendBtn_click)的;
/ /
/ /按钮3
/ /
this.button3.Location =新System.Drawing.Point(200,280);
this.button3.Name =“按钮3”;
this.button3.Size =新System.Drawing.Size(80,24);
this.button3.TabIndex = 5;
this.button3.Text =“断开”;
this.button3.Click + = System.EventHandler(this.DiscBtn_click)的;
/ /
/ / textBox3,
/ /
this.textBox3.Location =:System.Drawing.Point新(8,17??6);
this.textBox3.Multiline = TRUE;
this.textBox3.Name =“textBox3”;
新System.Drawing.Size this.textBox3.Size =(320,88);
this.textBox3.TabIndex = 6;
this.textBox3.Text =“”;
/ /
/ / label1的
/ /
this.label1.Location =:System.Drawing.Point新(48,24);
this.label1.Name =“label1的”;
新System.Drawing.Size this.label1.Size =(40,24);
this.label1.TabIndex = 7;
this.label1.Text =“姓名:”;
/ /
/ / LABEL2
/ /
this.label2.Location =:System.Drawing.Point新(8,48);
this.label2.Name =“label2的”;
this.label2.Size =新System.Drawing.Size(112,16);
this.label2.TabIndex = 8;
this.label2.Text =“收到的消息:”;
/ /
/ / LABEL3,
/ /
this.label3.Location =:System.Drawing.Point新(8,152);
this.label3.Name =“LABEL3”;
this.label3.Size =新System.Drawing.Size(128,16);
this.label3.TabIndex = 9;
this.label3.Text =“要发送的消息:”;
/ /
/ / StatusBar1中,,
/ /
this.statusBar1.Location =新System.Drawing.Point(0,318);
this.statusBar1.Name =“StatusBar1中”;
this.statusBar1.Size =新System.Drawing.Size(336,16);
this.statusBar1.TabIndex = 11;
this.statusBar1.Text =“状态”;
/ /
/ / Form1中
/ /
this.AutoScaleBaseSize =新System.Drawing.Size(6,14);
新System.Drawing.Size this.ClientSize =(336,334);
this.Controls.AddRange(新System.Windows.Forms.Control的[] {
,this.statusBar1,
,this.label3,
this.label2,
,this.label1,
,this.textBox3,
,this.button3,
this.button2,
this.button1,
this.textBox2,
this.textBox1});
this.Name =“Form1的”;
this.Text =“ChatServer”;的
this.ResumeLayout(假);
}
#endregion
/ / /公升; summarygt
/ / /应用程序的主入口点。
/ / / </摘要>
[STAThread]
静态无效的主要()
{
Application.Run(新Form1的());
}
私人的无效LoginBtn_click(对象发件人,发送System.EventArgs)
{
HOSTIP然后我们(“127.0.0.1”);
尝试
{
ChatServer =新的IPEndPoint(HOSTIP,Int32.Parse(“8080”));
=新的Socket ChatSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)的;
ChatSocket.Bind(ChatServer);
ChatSocket.Listen(50);
statusBar1.Text = textBox1.Text +“logoned on.Now开始听......”;
AcceptedSocket = ChatSocket.Accept();
线程线程=新的Thread(新的ThreadStart(ChatProcess)的);
thread.Start();
}
赶上(例外EE)
{statusBar1.Text ee.Message;}
}
私人无效ChatProcess()
{
如果(AcceptedSocket.Connected)
{
statusBar1.Text =“准备在聊天!”;
而(旗)
{
[字节] ReceivedByte =新的字节[64];
AcceptedSocket.Receive(ReceivedByte ReceivedByte.Length,0);
串ReceivedStr = System.Text.Encoding.BigEndianUnicode.GetString(ReceivedByte);
textBox2.AppendText(ReceivedStr +“ R N”);
}
}
}
私人的无效SendBtn_click(对象发件人,发送System.EventArgs)
{
尝试
{
[字节] SentByte =新的字节[64];
串SentStr = textBox1.Text +“............”+ textBox3.Text +“ R N”;
SentByte = System.Text.Encoding.BigEndianUnicode.GetBytes(SentStr.ToCharArray());
AcceptedSocket.Send(SentByte SentByte.Length,0);
textBox3.Clear();
}
抓
{statusBar1.Text =“连接不存在!”;}
}
私人的无效DiscBtn_click(对象发件人,发送System.EventArgs)
{
尝试
{
ChatSocket.Close();
AcceptedSocket.Close();
statusBar1.Text =“断开连接!”
}
{statusBar1.Text =“连接不存在!”;}
}
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11