里面是最新的代码。
新加:
Buffers 数据包组合类
BuffList 数据包缓冲类
ReadBytes 数据包 读取类
里面包含 一个客户端 和服务器端的 简单 聊天室 案例 就几行代码
下载地址:
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace MessageServer
{
class Program
{
static ZYSocketSuper Server;
static List<User> UserList;
static void Main(string[] args)
{
UserList = new List<User>();
Server = new ZYSocketSuper("127.0.0.1", 888, 1000, 4096);
Server.BinaryInput = new BinaryInputHandler(BinaryInputHandler);//设置数据包输入回调方法
Server.Connetions = new ConnectionFilter(ConnectionFilter);
Server.MessageInput = new MessageInputHandler(MessageInputHandler);
Server.MessageOut += new EventHandler<LogOutEventArgs>(Server_MessageOut); //日记输出事件
Server.Start();
while (true)
{
Console.ReadLine();
Console.WriteLine("当前用户数量:" + UserList.Count.ToString());
foreach (User user in UserList)
{
Console.WriteLine(user.Name+":"+user.socketAsync.AcceptSocket.RemoteEndPoint.ToString());
}
}
}
static void Server_MessageOut(object sender, LogOutEventArgs e)
{
Console.WriteLine(e.Mess);
}
/// <summary>
/// 数据包输入回调方法 http://www.my400800.cn
/// </summary>
/// <param name="data"></param>
/// <param name="socketAsync"></param>
static void BinaryInputHandler(byte[] data, SocketAsyncEventArgs socketAsync)
{
if (socketAsync.UserToken == null) //如果此连接还没有登入
{
ReadBytes read = new ReadBytes(data); //创建读取数据包类
int lengt;
int cmd;
if (read.ReadInt32(out lengt) &&
lengt == read.Length &&
read.ReadInt32(out cmd) &&
cmd==(int)ClientCmdType.SendMess
) //读取首4字节,并验证长度的有效性 以及判断消息类型
{
string name;
if (read.ReadString(out name)) //读取用户名字
{
User user = new User(); //创建用户对象
user.Name = name;
user.socketAsync = socketAsync; //将用户对象和SOCKET绑定
socketAsync.UserToken = user;
byte[] MessData = Buffers.MergeBytes(Buffers.GetSocketBytes((int)CmdType.ServerMessage), //构造数据包
Buffers.GetSocketBytes(name + " 登入聊天室"));
MessData = Buffers.MergeBytes(Buffers.GetSocketBytes(MessData.Length + 4), MessData);
foreach (User tuser in UserList) //发送给所有在线用户
{
Server.SendData(tuser.socketAsync.AcceptSocket, MessData);
}
UserList.Add(user); //将用户加入用户表
}
}
}
else
{
User user = socketAsync.UserToken as User; //获取用户对象
if (user != null)
{
user.BuffList.InsertByteArray(data); //插入数据包缓冲池
do
{
try
{
byte[] buff = user.BuffList.GetData(4); //枚举说有数据包缓冲
if (buff == null)
break;
DataHandler(buff, user); //将每个数据包指令进行处理
}
catch
{
user.BuffList.Clecr();
break;
}
} while (true);
}
}
}
//数据包指令处理
public static void DataHandler(byte[] data, User user)
{
ReadBytes read = new ReadBytes(data);
int lengt;
if (read.ReadInt32(out lengt) && lengt == read.Length) //读取首4字节,并验证长度的有效性
{
int cmd;
if (read.ReadInt32(out cmd)) //读取命令
{
switch ((ClientCmdType)cmd)
{
case ClientCmdType.SendMess: //公聊
string mess;
if (read.ReadString(out mess)) //读取用户说的东西
{
byte[] messdata = Buffers.MergeBytes(Buffers.GetSocketBytes((int)CmdType.Message), //组合数据包
Buffers.GetSocketBytes(user.Name + ": " + mess));
messdata = Buffers.MergeBytes(Buffers.GetSocketBytes(messdata.Length + 4), messdata);
foreach (User tuser in UserList) //发送给说有用户
{
if (tuser.Equals(user) == false)
Server.SendData(tuser.socketAsync.AcceptSocket, messdata);
}
}
break;
case ClientCmdType.PrivateMess: //私聊
string username;
string message;
if (read.ReadString(out username) && read.ReadString(out message))
{
byte[] messdata = Buffers.MergeBytes(Buffers.GetSocketBytes((int)CmdType.Message), //组合数据包
Buffers.GetSocketBytes(user.Name + " 对你说: " + message));
messdata = Buffers.MergeBytes(Buffers.GetSocketBytes(messdata.Length + 4), messdata);
User Puser = UserList.Find(o => String.Equals(o.Name, username, StringComparison.CurrentCultureIgnoreCase));
if (Puser != null)
{
Server.SendData(Puser.socketAsync.AcceptSocket, messdata);
}
else
{
messdata = Buffers.MergeBytes(Buffers.GetSocketBytes((int)CmdType.Message), //组合数据包
Buffers.GetSocketBytes("无法找到该用户:"+username));
messdata = Buffers.MergeBytes(Buffers.GetSocketBytes(messdata.Length + 4), messdata);
Server.SendData(user.socketAsync.AcceptSocket,messdata);
}
}
break;
}
}
}
}
/// <summary>
/// 用户连接回调方法
/// </summary>
/// <param name="socketAsync"></param>
/// <returns></returns>
static bool ConnectionFilter(SocketAsyncEventArgs socketAsync)
{
Console.WriteLine(socketAsync.AcceptSocket.RemoteEndPoint.ToString());
byte[] messdata = Buffers.MergeBytes(Buffers.GetSocketBytes((int)CmdType.Message), //组合数据包
Buffers.GetSocketBytes("[欢迎使用我的聊天室]\r\n请输入你的用户昵称:"));
messdata = Buffers.MergeBytes(Buffers.GetSocketBytes(messdata.Length + 4), messdata);
Server.SendData(socketAsync.AcceptSocket, messdata);
return true;
}
/// <summary>
/// 异常消息输出方法
/// </summary>
/// <param name="message"></param>
/// <param name="socketAsync"></param>
/// <param name="erorr"></param>
static void MessageInputHandler(string message, SocketAsyncEventArgs socketAsync, int erorr)
{
User user = socketAsync.UserToken as User; //获取用户对象
if (user != null)
{
UserList.Remove(user);
}
socketAsync.UserToken = null;
}
}
class User
{
public SocketAsyncEventArgs socketAsync { get; set; }
public string Name { get; set; }
public BuffList BuffList = new BuffList();
}
enum CmdType
{
ServerMessage=1000, //服务器消息
Message=1001,
}
enum ClientCmdType
{
SendMess = 5001,
PrivateMess=5002,
}
}