| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 604 人关注过本帖
标题:[求助] 如果关闭它?
只看楼主 加入收藏
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
 问题点数:0 回复次数:1 
[求助] 如果关闭它?

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 hyServer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private int listenport = 6060;
private TcpListener listener;
private System.Windows.Forms.ListBox lbClients;
private ArrayList clients;
private Thread processor;
private Socket clientsocket;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbCommand oleDbCommand1;
private hyServer.DataSet1 dataSet11;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.MenuItem menuItem3;
private Thread clientservice;
/// <summary>
/// 必需的设计器变量。
/// </summary>


public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
clients = new ArrayList();
processor = new Thread(new ThreadStart(StartListening));
processor.Start();

//
// 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.lbClients = new System.Windows.Forms.ListBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataSet11 = new hyServer.DataSet1();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.menuItem3 = new System.Windows.Forms.MenuItem();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// lbClients
//
this.lbClients.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lbClients.ItemHeight = 16;
this.lbClients.Location = new System.Drawing.Point(24, 288);
this.lbClients.Name = "lbClients";
this.lbClients.Size = new System.Drawing.Size(373, 20);
this.lbClients.TabIndex = 0;
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.dataSet11.jiben;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(392, 256);
this.dataGrid1.TabIndex = 1;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// oleDbInsertCommand1
//
this.oleDbInsertCommand1.CommandText = "SELECT ID, 城市, 密码, 昵称, 年龄, 性别 FROM jiben";
this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\\\db2.mdb";
//
// oleDbDataAdapter1
//
this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
this.oleDbDataAdapter1.SelectCommand = this.oleDbCommand1;
this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "jiben", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("城市", "城市"),
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("密码", "密码"),
new System.Data.Common.DataColumnMapping("昵称", "昵称"),
new System.Data.Common.DataColumnMapping("年龄", "年龄"),
new System.Data.Common.DataColumnMapping("性别", "性别")})});
this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
//
// oleDbCommand1
//
this.oleDbCommand1.CommandText = "SELECT 城市, ID, 密码, 昵称, 年龄, 性别 FROM jiben";
this.oleDbCommand1.Connection = this.oleDbConnection1;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.menuItem1.Text = "服务器";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "数据管理";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(416, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 96);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(448, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "textBox1";
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "关闭服务器";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(488, 368);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.lbClients);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "ChatServer";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);

}
#endregion


private void StartListening()
{
listener = new TcpListener(listenport);
listener.Start();
while (true)
{
try
{
Socket s = listener.AcceptSocket(); // 挂起侦听到的连接
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
catch(Exception e)
{
Console.WriteLine(e.ToString() );
}
}
//listener.Stop();
}

private void ServiceClient()
{
Socket client = clientsocket;
bool keepalive = true;

while (keepalive)
{
Byte[] buffer = new Byte[1024];
client.Receive(buffer);
string clientcommand = System.Text.Encoding.UTF8.GetString(buffer);

string[] tokens = clientcommand.Split(new Char[]{'|'});
Console.WriteLine(clientcommand);

if (tokens[0] == "注册")
{
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
// SendToClient(cl, "JOIN|" + tokens[1]);
}


EndPoint ep = client.RemoteEndPoint;

Client c = new Client(tokens[1], ep, clientservice, client);
clients.Add(c);

string message = "LIST|" + GetChatterList() +"\r\n";
SendToClient(c, message);

lbClients.Items.Add(tokens[1]);


this.oleDbInsertCommand1.CommandText = "INSERT INTO jiben (昵称, 密码,城市,年龄,性别 ) VALUES " + "(" + tokens[1] ;
this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
textBox1.Text =" VALUES " + "(" + tokens[1] ;


}


if (tokens[0] == "GONE")
{
int c = clients.Count;
for(int n=0; n<c; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, clientcommand);

client.Close();
keepalive = false;
}
}
}
}


private void SendToClient(Client cl, string message)
{
try
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message.ToCharArray());
cl.Sock.Send(buffer,buffer.Length,0);
}
catch(Exception e)
{
cl.Sock.Close();
cl.CLThread.Abort();
clients.Remove(cl);

}
}
private string GetChatterList()
{
string chatters = "";
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
chatters += cl.Name;
chatters += "|";
}
chatters.Trim(new char[]{'|'});
return chatters;
}


/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)
{


}

private void menuItem2_Click(object sender, System.EventArgs e)
{
try
{
dataSet11.Clear ();
oleDbDataAdapter1.Fill (dataSet11,"jiben");
}
catch(Exception ex)
{ MessageBox.Show(ex.ToString()); }

}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
oleDbConnection1.Open ();
oleDbDataAdapter1.Update (dataSet11, "jiben");
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery ();
oleDbConnection1.Close ();
dataSet11.Clear ();
oleDbDataAdapter1.Fill (dataSet11,"jiben");

}
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }



}

private void menuItem3_Click(object sender, System.EventArgs e)
{
oleDbDataAdapter1.Dispose ();
dataSet11.Dispose ();
processor.Abort ();
Application.Exit ();

}
}
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
oleDbDataAdapter1.Dispose ();
dataSet11.Dispose ();
processor.Abort ();
Application.Exit ();

}
}
}
用这段 还是没办法让它关闭 始终在进程里有一个 hyServer.exe

搜索更多相关主题的帖子: null private public listen 
2007-04-02 18:52
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
得分:0 
谁能解答一下啊

2007-04-03 13:51
快速回复:[求助] 如果关闭它?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039696 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved