代码拉取完成,页面将自动刷新
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("保存文件成功");
// }
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。