根据对通讯协议的抓包模拟回复内容
This commit is contained in:
@@ -27,12 +27,12 @@ namespace HexcalMC.Hexcal
|
||||
|
||||
private readonly Dictionary<string, Socket> _dictSocket = new Dictionary<string, Socket>();
|
||||
private readonly Dictionary<string, Thread> _dictThread = new Dictionary<string, Thread>();
|
||||
private Socket _mWatchSocket;
|
||||
|
||||
private Thread _mWatchThread; // 负责监听客户端连接请求的线程;
|
||||
|
||||
private readonly string _strServerIp = "127.0.0.1"; //服务器的IP地址
|
||||
private readonly string _strServerPort = "8080"; //端口号
|
||||
private Socket _mWatchSocket;
|
||||
|
||||
private Thread _mWatchThread; // 负责监听客户端连接请求的线程;
|
||||
|
||||
/// <summary>
|
||||
/// 使用模式,默认=1,接收任意数据显示;2=前两个字节为数据长度
|
||||
@@ -134,7 +134,7 @@ namespace HexcalMC.Hexcal
|
||||
{
|
||||
if (_dictSocket.Values.ToArray()[i].Poll(10, SelectMode.SelectRead))
|
||||
{
|
||||
//DictSocket.Remove(DictSocket.Keys.ToArray()[i]);
|
||||
|
||||
RemoveSocketClient(_dictSocket.Keys.ToArray()[i]);
|
||||
}
|
||||
}
|
||||
@@ -159,6 +159,12 @@ namespace HexcalMC.Hexcal
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler<byte[]> DataReceived;
|
||||
protected virtual void OnDataReceivedByte(byte[] data)
|
||||
{
|
||||
// 触发事件
|
||||
DataReceived?.Invoke(this, data);
|
||||
}
|
||||
//接收线程
|
||||
private void ReceiveThread(object sokObj)
|
||||
{
|
||||
@@ -188,28 +194,39 @@ namespace HexcalMC.Hexcal
|
||||
|
||||
try
|
||||
{
|
||||
if (UseMode == 1)
|
||||
switch (UseMode)
|
||||
{
|
||||
if (length > 0)
|
||||
{
|
||||
// TcpIpDfn.SongLiang_arrMsgRec = arrMsgRec; //510
|
||||
string strData = Encoding.Default.GetString(arrMsgRec); // 将接受到的字节数据转化成字符串;
|
||||
//strData = strData.Substring(0, length);
|
||||
//RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData.Replace("\0", "<0>"));
|
||||
RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData.Replace("\0", "."));
|
||||
}
|
||||
}
|
||||
else if (UseMode == 2)
|
||||
{
|
||||
if (length > 0 && arrMsgRec.Length > 2)
|
||||
{
|
||||
// TcpIpDfn.SongLiang_arrMsgRec = arrMsgRec; //510
|
||||
string strData =
|
||||
Encoding.Default.GetString(arrMsgRec, 2,
|
||||
arrMsgRec.Length - 2); // 将接受到的字节数据转化成字符串;
|
||||
case 1:
|
||||
if (length > 0)
|
||||
{
|
||||
string strData = Encoding.Default.GetString(arrMsgRec); // 将接受到的字节数据转化成字符串;
|
||||
//strData = strData.Substring(0, length);
|
||||
//RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData.Replace("\0", "<0>"));
|
||||
RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData.Replace("\0", "."));
|
||||
}
|
||||
|
||||
RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData);
|
||||
}
|
||||
;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
if (length > 0 && arrMsgRec.Length > 2)
|
||||
{
|
||||
string strData =
|
||||
Encoding.Default.GetString(arrMsgRec, 2,
|
||||
arrMsgRec.Length - 2); // 将接受到的字节数据转化成字符串;
|
||||
|
||||
RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData);
|
||||
}
|
||||
|
||||
break;
|
||||
case 3:
|
||||
if (length > 0)
|
||||
{
|
||||
|
||||
OnDataReceivedByte(arrMsgRec);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user