T
topdoc
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个多人聊天的服务端程序,可是程序关闭后资源不释放,请帮助,谢谢,代码如下:
[STAThread]
static void Main()
{
Application.Run(new ServerMain());
}
private void StartListening()
{
IPAddress ipAddress = Dns.Resolve("topdoc").AddressList[0];
TcpListener listener = new TcpListener(ipAddress,listenport);
listener.Start();
UserstatusBar.Panels[0].Text="正在监听......";
while (true)
while (exitapp)
{
try
{
{
Socket s = listener.AcceptSocket();
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
}
catch(Exception ex)
{
MessageBox.Show("系统监听错误,返回信息:"+ex.Message);
}
}
}
private void ServiceClient()
{
Socket client = clientsocket;
bool keepalive = true;
while (keepalive)
{
string tmp;
string[] spltstr;
if (PubClass.PublicCls.ReceiveFromSocket(out tmp,client)==0)
continue;
switch (PubClass.PublicCls.SplitReceiveStr(tmp,out spltstr))
{
case 0:
{
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
PubClass.PublicCls.SendFmtMsg(3,"","",spltstr[0],cl.Sock);
}
EndPoint ep = client.RemoteEndPoint;
Client c = new Client(spltstr[0], ep, clientservice, client);
PubClass.PublicCls.SendFmtMsg(2,spltstr[0],"",GetChatterList(),c.Sock);
clients.Add(c);
UserList.Items.Add(c.ToString());
break;
}
case 1:
{
int remove = 0;
bool found = false;
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(spltstr[0]) == 0)
{
remove = n;
found = true;
UserList.Items.Remove(UserList.Items[remove]);
}
}
if(found)
clients.RemoveAt(remove);
client.Close();
keepalive = false;
break;
}
case 2:
{
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(spltstr[0]) == 0)
PubClass.PublicCls.SendFmtMsg(2,spltstr[0],"",GetChatterList(),cl.Sock);
}
break;
}
case 3:
{
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(spltstr[0]) == 0)
PubClass.PublicCls.SendFmtMsg(3,"","","",cl.Sock);
}
break;
}
case 4:
{
string destclient = spltstr[1];
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(spltstr[1]) == 0)
PubClass.PublicCls.SendFmtMsg(4,spltstr[0],spltstr[1],spltstr[2],cl.Sock);
}
break;
}
}
}
}
private string GetChatterList()
{
string result = "";
for(int i=0;i<clients.Count;i++)
{
result += ((Client)clients).Name+",";
}
return result;
}
private void ServerMain_Load(object sender, System.EventArgs e)
{
clients = new ArrayList();
threadListen = new Thread(new ThreadStart(StartListening));
threadListen.Start();
}
}
}
[STAThread]
static void Main()
{
Application.Run(new ServerMain());
}
private void StartListening()
{
IPAddress ipAddress = Dns.Resolve("topdoc").AddressList[0];
TcpListener listener = new TcpListener(ipAddress,listenport);
listener.Start();
UserstatusBar.Panels[0].Text="正在监听......";
while (true)
while (exitapp)
{
try
{
{
Socket s = listener.AcceptSocket();
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
}
catch(Exception ex)
{
MessageBox.Show("系统监听错误,返回信息:"+ex.Message);
}
}
}
private void ServiceClient()
{
Socket client = clientsocket;
bool keepalive = true;
while (keepalive)
{
string tmp;
string[] spltstr;
if (PubClass.PublicCls.ReceiveFromSocket(out tmp,client)==0)
continue;
switch (PubClass.PublicCls.SplitReceiveStr(tmp,out spltstr))
{
case 0:
{
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
PubClass.PublicCls.SendFmtMsg(3,"","",spltstr[0],cl.Sock);
}
EndPoint ep = client.RemoteEndPoint;
Client c = new Client(spltstr[0], ep, clientservice, client);
PubClass.PublicCls.SendFmtMsg(2,spltstr[0],"",GetChatterList(),c.Sock);
clients.Add(c);
UserList.Items.Add(c.ToString());
break;
}
case 1:
{
int remove = 0;
bool found = false;
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(spltstr[0]) == 0)
{
remove = n;
found = true;
UserList.Items.Remove(UserList.Items[remove]);
}
}
if(found)
clients.RemoveAt(remove);
client.Close();
keepalive = false;
break;
}
case 2:
{
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(spltstr[0]) == 0)
PubClass.PublicCls.SendFmtMsg(2,spltstr[0],"",GetChatterList(),cl.Sock);
}
break;
}
case 3:
{
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(spltstr[0]) == 0)
PubClass.PublicCls.SendFmtMsg(3,"","","",cl.Sock);
}
break;
}
case 4:
{
string destclient = spltstr[1];
for(int n=0;
n<clients.Count;n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(spltstr[1]) == 0)
PubClass.PublicCls.SendFmtMsg(4,spltstr[0],spltstr[1],spltstr[2],cl.Sock);
}
break;
}
}
}
}
private string GetChatterList()
{
string result = "";
for(int i=0;i<clients.Count;i++)
{
result += ((Client)clients).Name+",";
}
return result;
}
private void ServerMain_Load(object sender, System.EventArgs e)
{
clients = new ArrayList();
threadListen = new Thread(new ThreadStart(StartListening));
threadListen.Start();
}
}
}