简单的多人聊天(C#.Socket)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/********************************8chatserver:*************************************************/
using system;
using system.drawing;
using system.collections;
using ponentmodel;
using system.windows.forms;
using system.data;
using system.threading;
using .sockets;
using ;
namespace chat_server
{
///
/// form1 的摘要说明。
///
public class form1 : system.windows.forms.form
{
///
/// 必需的设计器变量。
///
private ponentmodel.container components = null;
static int listenport=6666;
socket clientsocket;
private system.windows.forms.listbox lbclients;
arraylist clients;
private system.windows.forms.button button1;
thread clientservice;
private bel label1;
thread threadlisten;
public form1()
{
initializecomponent();
}
///
/// 清理所有正在使用的资源。
///
protected override void dispose( bool disposing ) {
if( disposing )
{
if(clientservice != null)
{
clientservice.abort();
}
if(threadlisten != null)
{
try
{
threadlisten.abort();
}
catch(exception ex)
{
threadlisten = null;
}
}
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法- 不要使用代码编辑器修改
/// 此方法的内容。
///
private void initializecomponent()
{
this.lbclients = new system.windows.forms.listbox();
this.button1 = new system.windows.forms.button();
bel1 = new bel();
this.suspendlayout();
//
// lbclients
//
this.lbclients.itemheight = 12;
this.lbclients.location = new system.drawing.point(16, 24);
= "lbclients";
this.lbclients.size = new system.drawing.size(184, 268);
this.lbclients.tabindex = 0;
//
// button1
//
this.button1.location = new system.drawing.point(272, 56);
= "button1";
this.button1.tabindex = 1;
this.button1.text = "button1";
this.button1.click += new system.eventhandler(this.button1_click); //
// label1
//
bel1.location = new system.drawing.point(240, 136);
= "label1";
bel1.size = new system.drawing.size(120, 32);
bel1.tabindex = 2;
bel1.text = "label1";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.drawing.size(368, 309);
this.controls.add(bel1);
this.controls.add(this.button1);
this.controls.add(this.lbclients);
= "form1";
this.text = "form1";
this.load += new system.eventhandler(this.form1_load);
this.resumelayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[stathread]
static void main()
{
application.run(new form1());
}
private void startlistening()
{
tcplistener listener = new tcplistener(listenport);
listener.start();
label1.text = "listening....";
while (true)
{
try
{
socket s = listener.acceptsocket();
clientsocket = s;
clientservice = new thread(new threadstart(serviceclient)); clientservice.start();
}