网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 论文素材 设计下载 最新论文 下载排行 论文上传 在线投稿 联系我们
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > C# > 正文
C# Socket实现简单的多人聊天
来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/14
下载{$ArticleTitle}原创论文样式
(true)
{
try
{

Socket s = listener.AcceptSocket();
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
catch(Exception ex)
{
MessageBox.Show("listening Error: "+ex.Message);
}
}
}
private void ServiceClient()
{
Socket client = clientsocket;
bool keepalive = true;

while (keepalive)
{
Byte buffer = new Byte[1024];
int bufLen = 0;
try
{
bufLen = client.Available ;

client.Receive(buffer,0,bufLen,SocketFlags.None);
if(bufLen==0)
continue;
}
catch(Exception ex)
{
MessageBox.Show("Receive Error:"+ex.Message);
return;
}

string clientcommand = System.Text.Encoding.ASCII.GetString(buffer).Substring(0,bufLen);

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

if (tokens[0] == "CONN")
{
for(int n=0; n<clients.Count;n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, "JOIN|" + tokens);
}
EndPoint ep = client.RemoteEndPoint;
Client c = new Client(tokens, ep, clientservice, client);

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

clients.Add(c);

lbClients.Items.Add(c);

}
if (tokens[0] == "CHAT")
{
for(int n=0; n<clients.Count;n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, clientcommand);
}
}
if (tokens[0] == "PRIV")
{
string destclient = tokens;
for(int n=0; n<clients.Count;n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(tokens) == 0)
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens) == 0)
SendToClient(cl, clientcommand);
}
}
if (tokens[0] == "GONE")
{
int remove = 0;
bool found = false;
int c = clients.Count;
for(int n=0; n<clients.Count;n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens) == 0)
{
remove = n;
found = true;
lbClients.Items.Remove(cl);
}
}
if(found)
clients.RemoveAt(remove);
client.Close();
keepalive = false;
}
}
}

private string GetChatterList()
{
string result = "";

for(int i=0;i<clients.Count;i++)
{
result += ((Client)clients[i]).Name+"|";
}
return result;

}

private void SendToClient(Client cl,string clientCommand)
{
Byte message = System.Text.Encoding.ASCII.GetBytes(clientCommand);
Socket s = cl.Sock;
if(s.Connected)
{
s.Send(message,message.Length,0);
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
clients = new ArrayList();
}

private void button1_Click(object sender, System.EventArgs e)
{
threadListen = new Thread(new ThreadStart(StartListening));
threadListen.Start();
}
}
}

 

 

/***************************** client类 ********************/

/************************** 放于 chatServer 项目中 *********/

using System;
using System.Threading;

namespace Chat_Server
{
using System.Net.Sockets;
using System.N

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号