socket编程 cpu 100%
namespace ChatServer
{
public class ChatServer : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ListBox lbClients;
private ArrayList clients;
private int listenport = 4545;
private TcpListener listener;
private Thread processor;
private Socket clientsocket;
private Thread clientservice;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.NotifyIcon webMisChat;
private System.Windows.Forms.Label lblServer;
private static string str = "";
private bool isExitApp = false;
private const int WM_QUERYENDSESSION = 0x0011;
public ChatServer()
{
InitializeComponent();
clients = new ArrayList();
processor = new Thread(new ThreadStart(StartListening));
processor.Start();
}
private void StartListening()
{
try
{
listener = new TcpListener(listenport);
listener.Start();
while (true)
{
Socket s = listener.AcceptSocket();
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
}
catch(Exception ee)
{
String strFilePath = Application.StartupPath + "\\" + "/log.txt";
try
{
if(!File.Exists(strFilePath))
{
File.CreateText(strFilePath);
}
using (StreamWriter streamWriter = File.AppendText(strFilePath))
{
streamWriter.WriteLine( DateTime.Now.ToString() + ":StartListening() " + ee.Message);
}
}
catch{}
finally{}
}
}
private void ServiceClient()
{
try
{
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[]{'|'});
if (tokens[0] == "WEBPRIV")
{
string destclient = tokens[3];
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(tokens[3]) == 0)
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens[1]) == 0)
SendToClient(cl, clientcommand);
}
}
if (tokens[0] == "GONE")
{
bool b = false;
if(str.IndexOf(tokens[1].Trim() + ",") != -1)
b = true;
int remove = 0;
bool found = false;
int c = clients.Count;
string str1 = "";
for(int n=0; n<c; n++)
{
Client cl = (Client)clients[n];
if(b)
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens[1]) == 0)
{
remove = n;
found = true;
lbClients.Items.Remove(cl);
lbClients.Items.Remove(cl.Name + " : " + cl.Host.ToString());
str1 = cl.Name;
}
}
if(found)
{
clients.RemoveAt(remove);
str = str.Replace(str1 + ",","");
}
client.Close();
keepalive = false;
}
}
}
catch(Exception ee)
{
String strFilePath = Application.StartupPath + "\\" + "/log.txt";
try
{
if(!File.Exists(strFilePath))
{
File.CreateText(strFilePath);
}
using (StreamWriter streamWriter = File.AppendText(strFilePath))
{
streamWriter.WriteLine( DateTime.Now.ToString() + ":ServiceClient() " + ee.Message);
}
}
catch{}
finally{}
}
}
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 ee)
{
if(cl != null)
{
cl.Sock.Close();
cl.CLThread.Abort();
clients.Remove(cl);
}
lbClients.Items.Remove(cl.Name + " : " + cl.Host.ToString());
String strFilePath = Application.StartupPath + "\\" + "/log.txt";
try
{
if(!File.Exists(strFilePath))
{
File.CreateText(strFilePath);
}
using (StreamWriter streamWriter = File.AppendText(strFilePath))
{
streamWriter.WriteLine( DateTime.Now.ToString() + ":SendToClient(Client cl, string message) " + ee.Message);
}
}
catch{}
finally{}
}
}
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;
}
[STAThread]
static void Main()
{
Application.Run(new ChatServer());
}
private void menuItem1_Click_1(object sender, System.EventArgs e)
{
if(clientsocket != null)
clientsocket.Close();
if(listener != null)
listener.Stop();
if(clientservice != null)
clientservice.Abort();
if(processor != null)
processor.Abort();
this.Close();
this.Dispose();
Application.Exit();
}
}
}