socket编程 cpu 100%

C5662601 2008-07-16 08:20:14
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();
}
}
}
...全文
191 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
converf 2008-09-30
  • 打赏
  • 举报
回复
不得不顶。。。支持
学习一下
converf 2008-09-30
  • 打赏
  • 举报
回复
不得不顶。。。支持
学习一下
laxila 2008-09-30
  • 打赏
  • 举报
回复
最近正在研习中!!!
laxila 2008-09-30
  • 打赏
  • 举报
回复
楼主辛苦了
majiajun_no_13 2008-09-28
  • 打赏
  • 举报
回复
看贴是学习,顶贴是义务。呵呵
C5662601 2008-07-17
  • 打赏
  • 举报
回复
死循环解决了 可怎么判断客户端不在呢 因为捕获到的是个空值不知道那个客户端
C5662601 2008-07-17
  • 打赏
  • 举报
回复
解決了
int keepAlive = -1744830460; // SIO_KEEPALIVE_VALS
byte[] inValue = new byte[] { 1, 0, 0, 0, 0x20, 0x4e, 0, 0, 0xd0, 0x07, 0, 0 }; //True, 20 秒, 2 秒
client.IOControl(keepAlive, inValue, null);
C5662601 2008-07-16
  • 打赏
  • 举报
回复
测试了下 把客户端断线 结束任务 关机
都不会导致服务器端死循环了
mawering 2008-07-16
  • 打赏
  • 举报
回复
学习一下!
C5662601 2008-07-16
  • 打赏
  • 举报
回复
因为Socket s = listener.AcceptSocket(); 监听后
每有消息到达都会起一个
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
去处理
当客户端突然掉线 break 这个循环就可以

应该是这个原因吧
C5662601 2008-07-16
  • 打赏
  • 举报
回复
if(clientcommand.Trim('\0') == "")
break;

加上就OK了
lussnailatnet 2008-07-16
  • 打赏
  • 举报
回复
studying
C5662601 2008-07-16
  • 打赏
  • 举报
回复
up
C5662601 2008-07-16
  • 打赏
  • 举报
回复
while (keepalive)
{
Byte[] buffer = new Byte[1024];
client.Receive(buffer);
string clientcommand = System.Text.Encoding.UTF8.GetString(buffer);


断点发现 clientcommand = “” 死循环了 怎么处理呢
cnwolfs 2008-07-16
  • 打赏
  • 举报
回复
mark

110,960

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧