1 Star 0 Fork 0

kylinmm/Bencode编码与解码.net实现

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SocketHelp.cs 5.42 KB
一键复制 编辑 原始数据 按行查看 历史
kylinmm 提交于 2021-03-16 13:33 . 初始化仓库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace socket自定义协议通讯实现
{
public class SocketHelp
{
/// <summary>
///
/// </summary>
/// <param name="IP">连接主机IP地址</param>
/// <param name="PORT">连接主机端口号</param>
/// <returns></returns>
public static Socket SocketConn(IPAddress IP,int PORT) {
Socket socketClient = null;
try
{
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Unspecified);
socketClient.Connect(IP, PORT);
}
catch (Exception e) {
Console.WriteLine("目标IP连接失败!错误信息:"+e.Message);
}
return socketClient;
}
/// <summary>
/// 断开socket连接
/// </summary>
/// <param name="socketClient"></param>
public static void SocketDisConn(Socket socketClient)
{
//关闭socket
socketClient.Close();
}
/// <summary>
/// 向指定连接端点发送报文数据
/// </summary>
/// <param name="Msg"></param>
/// <param name="socketClient"></param>
public static void SocketSendMsg(string Msg,Socket socketClient)
{
try
{
string EncodeMsg = EncodedHelper.Encode(Msg);
byte[] buffer = new byte[(UInt32)System.Text.Encoding.Default.GetBytes(EncodeMsg).Length];
buffer = Encoding.Default.GetBytes(EncodeMsg);
int receive = socketClient.Send(buffer);
Console.WriteLine("数据报文发送成功!内容为:"+ Msg);
Console.WriteLine("值1:" + (UInt32)System.Text.Encoding.Default.GetBytes(EncodeMsg).Length);
Console.WriteLine("值2:" + (UInt32)System.Text.Encoding.Default.GetBytes(Msg).Length);
}
catch (Exception ex)
{
Console.WriteLine("数据报文发送失败!错误信息:"+ ex.Message);
}
}
/// <summary>
/// 创建接收远端数据线程
/// </summary>
/// <returns></returns>
public static Thread CreateReciveThread() {
Thread ReciveThread = null;
ReciveThread = new Thread(new ParameterizedThreadStart(ReciveMain));
Console.WriteLine("接收进程创建成功!");
return ReciveThread;
}
/// <summary>
///
/// </summary>
/// <param name="socketClient">需要接受数据的socket句柄</param>
public static void ReciveMain(object socketClient) {
Socket socketC = socketClient as Socket;
try
{
while (true)
{
byte[] buffer = new byte[20480];
//实际接收到的字节数
int r = socketC.Receive(buffer);
if (r == 0)
{
// break;
}
else
{
string str = Encoding.Default.GetString(buffer, 1, r - 1);
Console.WriteLine("目标端点接收数据成功!数据如下:");
Console.WriteLine(str);
}
}
}
catch (Exception ex)
{
Console.WriteLine("接收目标数据信息失败!错误信息:"+ex.Message);
}
}
// 检查一个Socket是否可连接
public static bool IsSocketConnected(Socket client)
{
bool blockingState = client.Blocking;
try
{
byte[] tmp = new byte[1];
client.Blocking = false;
client.Send(tmp, 0, 0);
return false;
}
catch (SocketException e)
{
// 产生 10035 == WSAEWOULDBLOCK 错误,说明被阻止了,但是还是连接的
if (e.NativeErrorCode.Equals(10035))
return false;
else
return true;
}
finally
{
client.Blocking = blockingState; // 恢复状态
}
}
// //表示发送的是文件
// if (buffer[0] == 1)
// {
// SaveFileDialog sfd = new SaveFileDialog();
// sfd.InitialDirectory = @"";
// sfd.Title = "请选择要保存的文件";
// sfd.Filter = "所有文件|*.*";
// sfd.ShowDialog(this);
// string strPath = sfd.FileName;
// using (FileStream fsWrite = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write))
// {
// fsWrite.Write(buffer, 1, r - 1);
// }
//MessageBox.Show("保存文件成功");
// }
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kylinmm/Bencode.git
git@gitee.com:kylinmm/Bencode.git
kylinmm
Bencode
Bencode编码与解码.net实现
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385